In JDK 1.7 I can create a Collection
lets for e.g. say a HashMap
like this:
private HashMap<String, String> map = new HashMap<>();
With the diamond <>
at the end.
But if I am creating a map like this:
private static final HashMap<String, String> MAP = new HashMap<>() {{
put("something", "something");
}};
On the diamond compiler says that:
Cannot use ''<>'' with anonymous inner classes
I have to use: ... new HashMap<String, String>() {{....
in order the code to compile.
Why is it so? Why I can create a map and use diamond if I am creating just a new instance but the code doesn't compile if I am creating a map through an anonymous class?