给出以下代码:
@Test
public void test7() {
Map<String, Integer> sortedData = new HashMap<>();
sortedData.put("One", 1);
sortedData.put("Two", 2);
sortedData.put("Three", 3);
Stream<Map.Entry<String, Integer>> stream = sortedData.entrySet().stream();
List<String> sorted = stream
.sorted(Comparator.comparing(Map.Entry::getValue))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
编译成功,但是当我改变时
.sorted(Comparator.comparing(Map.Entry::getValue))
to
.sorted(Comparator.comparing(Map.Entry::getValue).reversed())
编译器抱怨说Non static method can't be referenced in static context
我可以想象这是因为getValue
不是静态方法Map.Entry
,但我无法在这里解释这个问题。
这真有趣,
.sorted(Comparator.comparing(Map.Entry<String,Integer>::getValue).reversed ())
works.
也许使用原始Map.Entry
使编译器感到困惑。
顺便说一句,这也有效:
.sorted(Collections.reverseOrder(Comparator.comparing(Map.Entry::getValue)))
(Collections.reverseOrder(this)
是什么reversed()
回报)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)