What is the difference between the following maps I create (in another question, people answered using them seemingly interchangeably and I\'m wondering if/how they are diff
In your second example the "map" reference is of type Map
, which is an interface implemented by HashMap
(and other types of Map
). This interface is a contract saying that the object maps keys to values and supports various operations (e.g. put
, get
). It says nothing about the implementation of the Map
(in this case a HashMap
).
The second approach is generally preferred as you typically wouldn't want to expose the specific map implementation to methods using the Map
or via an API definition.