I came across a piece of code:
public static <K,V> HashMap<K,V> newHashMap() { return new HashMap<K,V>(); }
and we can use it to create a HashMap instance, for example:
Map<String, List<String>> anagrams = newHashMap();
Now the question is that the newHashMap method newHashMap called without passing the required type (in this case ( String, List<String> ), but still java creates the correct type. How?
I got confused here how K,V becomes the restricted type that is mentioned on the left side of the code:
Map<String, List<String>>
without even going through:
newHashMap();
source share