Java 8 Lambda,筛选器HashMap,无法解析方法 [英] Java 8 Lambda, filter HashMap, cannot resolve method
问题描述
我对Java 8的新功能有点陌生.我正在学习如何按条目过滤地图.我看过本教程和
I'm kinda new to Java 8's new features. I am learning how to filter a map by entries. I have looked at this tutorial and this post for my problem, but I am unable to solve.
@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()'",因此无法执行:
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>
而不是HashMap
.
The message is misleading but your code does not compile for another reason: collect
returns a Map<String, Integer>
not a HashMap
.
如果您使用
Map<String, Integer> map = new HashMap<>();
它应能按预期工作(还请确保您具有所有相关的进口件).
it should work as expected (also make sure you have all the relevant imports).
这篇关于Java 8 Lambda,筛选器HashMap,无法解析方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!