22

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.

3
  • if i may ask.. what is e exactly.. dont see it declared anywhere.. Commented Jan 8, 2015 at 13:28
  • @tobias_k, should not a problem regarding generics. It should work according to this leveluplunch.com/java/examples/filter-map-by-value Commented Jan 8, 2015 at 13:31
  • 1
    @FlorentBayle adding casting (HashMap<String, Integer>) helps! Commented Jan 8, 2015 at 13:34

2 Answers 2

34

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Apparently it will take some time for the compiler to get smart enough about the true cause of an error. Quite frustrating at the moment...
If we down cast what is returned by .collect by (HashMap<String, Integer>), it appears to work. But is there any issue regarding casting?
I for one always prefer to generate data structures from the interfaces, List, Map e.t.c.
@Daniel The collector does not give you any guarantee on the type returned - so it may work now and break in the future.
The message is misleading: This saved my day. I was using toSet(), where I should have used toList(). The message is indeed very confusing.
5

Your are returning Map not hashMap so you need to change map type to java.util.Map. Moreover you can use method reference rather then calling getKey, getValue. E.g.

Map<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(Map.Entry::getKey, Map.Entry::getValue));

You could solve it by using some intellij help as well for e.g. if you press ctrl+alt+v in front of

new HashMap<>();
            map.put("1", 1);
            map.put("2", 2);
            map = map.entrySet()
                    .parallelStream()
                    .filter(e -> e.getValue() > 1)
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

The variable created by intellij will be of exact type and you will get.

Map<String, Integer> collect = map.entrySet()
        .parallelStream()
        .filter(e -> e.getValue() > 1)
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

1 Comment

ctrl+alt+v is awesome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.