我对 Java 8 的新功能有点陌生。我正在学习如何按条目过滤地图。我看过本教程 http://www.leveluplunch.com/java/examples/filter-map-by-value/ and 这个帖子 https://stackoverflow.com/questions/22840170/in-java-8-functional-style-how-can-i-map-the-values-over-a-java-util-map对于我的问题,但我无法解决。
@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()));
}
However, my IDE (IntelliJ) says "Cannot resolve method 'getKey()'", thus unable to complile:
Neither does this help:
Can anyone help me to solve this issue?
Thanks.
该消息具有误导性,但您的代码由于其他原因而无法编译:collect
返回一个Map<String, Integer>
not a HashMap
.
如果你使用
Map<String, Integer> map = new HashMap<>();
它应该按预期工作(还要确保您拥有所有相关的导入)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)