Cannot infer type arguments for hashmap<>

cannot infer type arguments for typetoken
cannot infer type arguments for page
cannot infer type-variable(s) t
cannot infer type argument(s) for <r flatmap(function super t extends stream extends r)
collect was inferred to r
how to convert string into set in java

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.


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

[SOLVED] Cannot infer type arguments for ArrayList<>, 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). error: cannot infer type arguments for HashMap; Map map2 = new HashMap<>() { reason: cannot use '<>' with anonymous inner classes where K,V are type-variables: K extends Object declared in class HashMap V extends Object declared in class HashMap


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

Cannot infer type arguments for ArrayList<>, SOLVED ***** Let me start by saying I did try to Google this but my interpretation is either not correct or I'm way off base, either way I need some guidance. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Cannot infer type arguments for Hashmap


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]}

Type Inference (The Java™ Tutorials > Learning the Java Language , SOLVED *** Let me start by saying I did try to Google this but my interpretation is either not correct or I'm way off base, either way I need some. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! Cannot infer type arguments


497193 – incorrect compile error "Cannot infer type arguments", The inference algorithm determines the types of the arguments and, if available, the set of type parameters ( <> ) as long as the compiler can infer the type arguments from the context. warning because the HashMap() constructor refers to the HashMap raw type, not the List<Object> cannot be converted to List<​String>. You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond. For example, consider the following variable declaration:


Compilation error: Cannot infer type arguments for , The following class compiles with jdk 1.8.0-91, but fails in eclipse neon with compile error "Cannot infer type arguments for Class1<>". import java.util.function​. Leave unconstrained type arguments raw (rather than inferring ). If there are no constraints on the elements of e.g. ArrayList a, uncheck this box will cause Eclipse to still provide a wildcard parameter, replacing the reference with ArrayList.


Vaadin 14 Error: cannot infer type arguments for com.vaadin.flow , Compiling within Eclipse, I get the following error at commit 49e54bd: Compilation error: Cannot infer type arguments for  I have a problem with my sorting-test app, where I use a comparator. I got a message: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Cannot infer type argum