You forgot to mention the class. Map
here is the reference type and is an Interface. HashMap
on the other side of equals specifies the actual type of the Object created and assigned to the reference foo
.
Map<String, List<String>> foo = new HashMap<String, List<String>>();
The actual type specified (HashMap
here) must be assignable to the reference type (Map
here) i.e. if the type of reference is an Interface, the Object's type must implement it. And, if the type of the reference is a Class, the Object's type must either be the same class or its subtype i.e. it extends from it.
From Java 7 onwards, you can use a shorthand like
Map<String, List<String>> foo = new HashMap<>();
Your second way of instantiation is not recommended. Stick to using List
which is an Interface.
// Don't bind your Map to ArrayList
new TreeMap<String, ArrayList<String>>();
// Use List interface type instead
new TreeMap<String, List<String>>();