2

Possible Duplicate:
Java - HashMap vs Map objects

What's the difference between :

Map <String,Integer>myMap = new HashMap<String,Integer>();

VS

HashMap <String,Integer> map = new HashMap<String,Integer>();  

Regards,Ron

Community
  • 1
  • 1
JAN
  • 21,236
  • 66
  • 181
  • 318

3 Answers3

9

There is no difference between the objects. There is a difference in the interface you have to the object. In the first case, the interface is HashMap<String, Object>, whereas in the second it's Map<String, Object>. The underlying object, though, is the same.

The advantage to using Map<String, Object> is that you can change the underlying object to be a different kind of map without breaking your contract with any code that's using it. If you declare it as HashMap<String, Object>, you have to change your contract if you want to change the underlying implementation...


Also Map is the static type of map, while HashMap is the dynamic type of map. This means that the compiler will treat your map object as being one of type Map, even though at runtime, it may point to any subtype of it...

This practice of programming against interfaces instead of implementations has the added benefit of remaining flexible: You can for instance replace the dynamic type of map at runtime, as long as it is a subtype of Map (e.g. LinkedHashMap), and change the map's behavior on the fly.

A good rule of thumb is to remain as abstract as possible on the API level: If for instance a method you are programming must work on maps, then it's sufficient to declare a parameter as Map instead of the stricter (because less abstract) HashMap type. That way, the consumer of your API can be flexible about what kind of Map implementation they want to pass to your method..

Ramandeep Singh
  • 5,063
  • 3
  • 28
  • 34
  • Thank you very much for the help ! I do not understand why people voted against the question ,I think it's very important to know the differences between the data structures that we use. Again , thanks! – JAN Mar 02 '12 at 06:41
  • So the benefit is that you can switch from Map myVar = new HashMap<>(); to something like Map myVar = new HashTree<>();? Or something like that? It just allows you to rewrite code easier if you decide you want to change things? Well... it is just weird that you even need to declare the types to begin with. It is kind of like a T myVar situation? Where the Map is just abstract and allows you to just declare the types without the object structure and then worry about that later? Am I on the correct path? – Evan Erickson Dec 20 '21 at 23:26
3

Difference is:

when you use Map as a type you can switch implemenation (HashMap) to other!

its a good thing!

EDIT: read this - What does it mean to "program to an interface"?

Community
  • 1
  • 1
dantuch
  • 9,123
  • 6
  • 45
  • 68
2

One additional point.

Declaring the variable as a Map prevents you from using the clone() method provided by the HashMap class.

so, if you use:

Map <String,Integer>myMap = new HashMap<String,Integer>();

then you can't use:

Map<String,Integer> myMap2 = myMap.clone();

Other than that, they're pretty interchangeable.

Nishant Sharma
  • 360
  • 1
  • 5