【发布时间】:2017-10-24 07:16:25
【问题描述】:
给定以下代码:
@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))
到
.sorted(Comparator.comparing(Map.Entry::getValue).reversed())
编译器抱怨Non static method can't be referenced in static context
我可以想象是因为getValue不是Map.Entry的静态方法,但是我在这里无法解释问题。
【问题讨论】:
标签: java java-8 java-stream