我有困难理解如何在Java7中“迁移”一个简单的比较器。
我在Java8中使用的实际版本是:
private static final Comparator<Entry> ENTRY_COMPARATOR = Comparator.comparing(new Function<Entry, EntryType>() {
@Override
public EntryType apply(Entry t) {
return t.type;
}
})
.thenComparing(Comparator.comparingLong(new ToLongFunction<Entry>() {
@Override
public long applyAsLong(Entry value) {
return value.count;
}
}).reversed());
但在构建阶段,我遇到了这个错误:
static interface method invocations are not supported in -source 7
我该如何将同样的比较器迁移到Java7?我正在搜索解决方案,但唯一能想到的是将自己的类实现为Comparator接口的实现。
但是如果我这样做,如何在同一个“compare”方法中应用“comparing”、“then comparing”和“reverse”呢?
提前致谢。
guava
,它有ComparisonChain
,我认为它可以为您处理这个。 - Eugene