排序Map< String,Long>按价值反转 [英] Sort Map<String, Long> by value reversed

查看:209
本文介绍了排序Map< String,Long>按价值反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Map<String, Long> map,我想使用Java 8的功能按Long值反向排序.在Google上,我找到了

I have a Map<String, Long> map which I want to sort by the Long value in reversed order using the features of Java 8. With Google I found this thread which provides this solution

Map<String, Long> sortedMap = map.entrySet().stream()
           .sorted(comparing(Entry::getValue))
                     .collect(toMap(Entry::getKey, Entry::getValue,
                              (e1,e2) -> e1, LinkedHashMap::new));

如果我想在注释中颠倒顺序,它说使用comparing(Entry::getValue).reversed()而不是comparing(Entry::getValue).

If I want to have the order reversed in the comments it says to use comparing(Entry::getValue).reversed() instead of comparing(Entry::getValue).

但是,该代码无法正常工作.但是有了一点点的适应,它就能做到:

However, the code doesn't work. But with this little adaption it does:

Map<String, Long> sortedMap = map.entrySet().stream()
          .sorted(Comparator.comparing(Entry::getValue))
                .collect(Collectors.toMap(Entry::getKey, Entry::getValue,
                      (e1, e2) -> e1, LinkedHashMap::new));

我必须先进行一些导入才能运行原始代码吗?

Do I have to do some imports first to be able to run the original code?

自从开始以来,仍然可以得到相反的顺序

What still remains to get the reversed order, since

Map<String, Long> sortedMap = map.entrySet().stream()
          .sorted(Comparator.comparing(Entry::getValue).reversed())
                .collect(Collectors.toMap(Entry::getKey, Entry::getValue,
                      (e1, e2) -> e1, LinkedHashMap::new));

给我一​​条错误消息:

gives my an error message:

The type Map.Entry does not define getValue(Object) that is applicable here

推荐答案

此答案中所述,类型推断当您 chain 调用Comparator.comparing(Entry::getValue).reversed()中的方法时,Java 8的数量达到了极限.

As explained in this answer, the type inference of Java 8 hit its limit when you chain method invocations like in Comparator.comparing(Entry::getValue).reversed().

相反,当像Collections.reverseOrder(Comparator.comparing(Entry::getValue))那样使用嵌套调用时,它将起作用.

In contrast, when using nested invocations like in Collections.reverseOrder(Comparator.comparing(Entry::getValue)) it will work.

当然,您可以使用static import s:

Of course, you can use static imports:

Map<String, Long> sortedMap = map.entrySet().stream()
    .sorted(reverseOrder(comparing(Entry::getValue)))
    .collect(toMap(Entry::getKey, Entry::getValue,
          (e1, e2) -> e1, LinkedHashMap::new));

,但应注意,当您忘记了import static语句(即找不到该方法)并将其与lambda表达式或方法引用结合使用时,编译器喜欢提供误导性的错误消息.

but it should be noted that the compiler likes to provide misleading error messages when you forget an import static statement (i.e. the method can’t be found) and combine it with lambda expressions or method references.

最后一点,还有现有的比较器实现

As a final note, there are also the existing comparator implementations Map.Entry.comparingByValue() and Map.Entry.comparingByValue(Comparator) which allow you to use

Map<String, Long> sortedMap = map.entrySet().stream()
    .sorted(reverseOrder(comparingByValue()))
    .collect(toMap(Entry::getKey, Entry::getValue,
          (e1, e2) -> e1, LinkedHashMap::new));

Map<String, Long> sortedMap = map.entrySet().stream()
    .sorted(comparingByValue(reverseOrder()))
    .collect(toMap(Entry::getKey, Entry::getValue,
          (e1, e2) -> e1, LinkedHashMap::new));

这篇关于排序Map&lt; String,Long&gt;按价值反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆