I am wondering why the second map declaration (using the diamond operator) does not compile when the first one does. Compilation error:
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
Code:
Map<String, String> map1 = new HashMap<String, String>() { //compiles fine
{
put("abc", "abc");
}
};
Map<String, String> map2 = new HashMap<>() { //does not compile
{
put("abc", "abc");
}
};
EDIT
Thanks for your answers - I should have read the compilation error better.
I found the exaplanation in the JLS
It is a compile-time error if a class instance creation expression declares an anonymous class using the "<>" form for the class's type arguments.