我有过
Map<String,LongAdder>我想在溪流中按价值排序--最好的方式。这比Long更难,因为LongAdder不能实现可比较的,所以我必须使用longValue (如果使用减法来生成比较器,则使用intValue )。
我知道我可以用
m.entrySet().stream().sorted((a, b) -> b.getValue().intValue() - a.getValue().intValue())但实际上,我也想在键(字符串)上进行二次排序。我也在扭转这种情况。
我想做
m.entrySet().stream().sorted(
Comparator.comparing((a, b) -> b.getValue().intValue() - a.getValue().intValue()))这样我就可以在以后用thenComparing()链接更多的比较器。
例外是
Lambda expression's signature does not match the signature of the functional interface method apply(T)但是,即使声明一个独立的比较器,这也是行不通的:
Comparator<Map.Entry<String,LongAdder>> byCount = Comparator.comparing((a,b) ->
(b.getValue().intValue() - a.getValue().intValue()));
Lambda expression's signature does not match the signature of the functional interface method apply(T)我不能使用函数引用"::“,因为它有太多的部分: Map.Entry.getValue().intValue()。
发布于 2019-06-17 14:19:03
您的独立比较器可以修复为使用Function,并如下所示:
Comparator<Map.Entry<String,LongAdder>> byCount = Comparator.comparingInt(e -> e.getValue().intValue());https://stackoverflow.com/questions/56633140
复制相似问题