Java 8 Lambda,筛选器HashMap,无法解析方法 [英] Java 8 Lambda, filter HashMap, cannot resolve method

查看:384
本文介绍了Java 8 Lambda,筛选器HashMap,无法解析方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆