Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Lambda, filter HashMap, cannot resolve method

Tags:

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())); } 

However, my IDE (IntelliJ) says "Cannot resolve method 'getKey()'", thus unable to complile: enter image description here

Neither does this help: enter image description here
Can anyone help me to solve this issue? Thanks.

like image 834
Daniel Avatar asked Jan 08 '15 13:01

Daniel


1 Answers

The message is misleading but your code does not compile for another reason: collect returns a Map<String, Integer> not a HashMap.

If you use

Map<String, Integer> map = new HashMap<>(); 

it should work as expected (also make sure you have all the relevant imports).

like image 192
assylias Avatar answered Sep 22 '22 13:09

assylias