Although this is valid code but it is not a good usage of generics because when you do new HashMap(), you create a raw HashMap and it will produce a warning at compile time. Note that to take advantage of automatic type inference during generic class instantiation, you must specify the diamond operator. i.e. You must do new HashMap<>() in this case.
Actually it is not clear for me because I can't see any difference apart from the compile warning between these two declaraiotn:
Map<String, String> myMap = new HashMap();
Map<String, String> myMap2 = new HashMap<>();
When I try to add an String - Integer entry, I get teh compile error in both cases:
myMap.put("2", 5);
myMap2.put("2", 5);
The method put(String, String) in the type Map<String,String> is not applicable for the arguments (String, int)
Based on this it seems that myMap is treated as generic as well which sounds reasonable for me bacause of the reference type. So the only difference is the warning?
Thanks!