【发布时间】:2015-01-08 21:40:59
【问题描述】:
我对 Java 8 的新功能有点陌生。我正在学习如何按条目过滤地图。我已经查看了this tutorial 和this post 的问题,但我无法解决。
@Test
public void testSomething() throws Exception {
HashMap<String, Integer> map = new HashMap<>();
map.put("1", 1);
map.put("2", 2);
map = map.entrySet()
.parallelStream()
.filter(e -> e.getValue()>1)
.collect(Collectors.toMap(e->e.getKey(), e->e.getValue()));
}
但是,我的 IDE (IntelliJ) 说“无法解析方法 'getKey()'”,因此无法编译:
这也无济于事:
谁能帮我解决这个问题?
谢谢。
【问题讨论】:
-
如果我可以问.. e 到底是什么.. 没有看到它在任何地方声明..
-
@tobias_k,关于泛型应该不是问题。它应该根据这个leveluplunch.com/java/examples/filter-map-by-value工作
-
@FlorentBayle 添加强制转换 (HashMap
) 有帮助!