Cannot infer type arguments for hashmap<>
Asked Answered
A

4

5

I am getting

   Cannot infer type arguments for java.util.HashMap<>

for the following code

class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(1, "x");
        map.put(2, "y");
        map.put(3, "x");
        map.put(4, "z");

  //the following line has error
        Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream()
                .collect(Collectors.toMap(item -> item.get(0).getValue(),
                        item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList())))); 
        System.out.println(reverseMap);

    }

}

What went wrong and Can anyone Explain me this ? I have checked for proper imports and found out that I am importing java.util.hashmap and none other. Still the pesky error is buging me.

The Error Persists

Algebraist answered 23/11, 2017 at 7:17 Comment(0)
G
10

That's a bug in ecj (eclipse compiler), you can work around it and add more type information :

item -> new ArrayList<Integer>(item.stream().map(Entry::getKey)

See how I've added ArrayList<Integer>.

It compiles fine with javac-8 and 9.

And btw seems like there is a simpler way to do things:

map.entrySet()
            .stream()
            .collect(Collectors.groupingBy(
                    Entry::getValue,
                    HashMap::new,
                    Collectors.mapping(Entry::getKey, Collectors.toList())));
Gravimeter answered 23/11, 2017 at 10:29 Comment(2)
Indeed, IntelliJ IDEA compiles the original code without errors.Warta
I filed a bug against ecj where you may follow the process towards a fix.Preparation
G
6

In my case, the error disappeared after adding import java.util.Map; :

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import com.fasterxml.jackson.databind.ObjectMapper;


    public void saveFooOrder(Foo foo, long orderId) {

         Map<String, Object> values = new HashMap<>();
                                         /*^^^^ Error was here: Cannot 
                                                infer type arguments for HashMap<>*/
            values.put("fooOrder", orderId);
            values.put("foo", foo.getId());
            orderFooInserter.execute(values);   
    }
Georgiannageorgianne answered 27/12, 2018 at 18:20 Comment(0)
S
0

your code is not completed, and you are missing a )

try doing:

import java.util.*;
import java.util.stream.Collectors;
public class HelloWorld{

     public static void main(String []args){
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "x");
        map.put(2, "y");
        map.put(3, "x");
        map.put(4, "z");

        Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream()
                .collect(Collectors.toMap(item -> item.get(0).getValue(),
                        item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList())))));
        System.out.println(reverseMap);
     }
}

this produce the output

{x=[1, 3], y=[2], z=[4]}
Southwards answered 23/11, 2017 at 7:27 Comment(0)
D
0

In my case the code is related to the Java version. I get the same error with this running locally on IntelliJ and deploying using Jenkins.

I had to add this to my pom.xml file to fix the issue

<properties>
        <java.version>11</java.version>
Deeannadeeanne answered 3/1 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.