首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >流中的LongAdder排序

流中的LongAdder排序
EN

Stack Overflow用户
提问于 2019-06-17 14:13:24
回答 1查看 132关注 0票数 1

我有过

代码语言:javascript
复制
Map<String,LongAdder>

我想在溪流中按价值排序--最好的方式。这比Long更难,因为LongAdder不能实现可比较的,所以我必须使用longValue (如果使用减法来生成比较器,则使用intValue )。

我知道我可以用

代码语言:javascript
复制
m.entrySet().stream().sorted((a, b) -> b.getValue().intValue() - a.getValue().intValue())

但实际上,我也想在键(字符串)上进行二次排序。我也在扭转这种情况。

我想做

代码语言:javascript
复制
m.entrySet().stream().sorted(
Comparator.comparing((a, b) -> b.getValue().intValue() - a.getValue().intValue()))

这样我就可以在以后用thenComparing()链接更多的比较器。

例外是

代码语言:javascript
复制
Lambda expression's signature does not match the signature of the functional interface method apply(T)

但是,即使声明一个独立的比较器,这也是行不通的:

代码语言:javascript
复制
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()。

EN

回答 1

Stack Overflow用户

发布于 2019-06-17 14:19:03

您的独立比较器可以修复为使用Function,并如下所示:

代码语言:javascript
复制
Comparator<Map.Entry<String,LongAdder>> byCount = Comparator.comparingInt(e -> e.getValue().intValue());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56633140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档