File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap and Map

 
Saumya Kiran
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the advantage of using
Map map = new HashMap()
vs
HashMap map = new HashMap()
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may decide later to use a TreeMap instead of a HashMap. By referreing to the interface instead of the implementation, you can make that change with one line of code and not break any code dependent on that Map.
 
Jon Dornback
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saumya Kiran:
What is the advantage of using
Map map = new HashMap()
vs
HashMap map = new HashMap()

by using the interface Map as a reference to your instance of HashMap, it is easier to rewrite your code later on to use a different type of map. since every method call is specified by the interface, you could change HashMap to TreeMap, and not have to rewrite your code.
If there is some specific function in HashMap that you need, and will never need to change the Map type, then it shouldn't be a problem to just declare it as HashMap.
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is known as the "program to the interface, not to the implementation" rule. The advantage is to swap your object implementations without affecting the code where this object is used. For example, if you have code like this:

The way it is coded, the caller of that method must pass an instance of a HashMap to it, otherwise the compiler will generate an error. Now, suppose that you have 100 methods like this, and you decided not to use a HashMap anymore, but a TreeMap (for optimization purposes). You now must change your code in 100 different places.
Now compare it with this code:

Notice that if you want to replace your HashMap with a TreeMap, you only need to make a change in only one place, where a map is instantiated.
Isn't this beautiful?
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn Jon and Eugene don't it feel good when we're all on the same page with a simulpost!
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn Jon and Eugene don't it feel good when we're all on the same page with a simulpost!
Fu**ing amazing!
 
Saumya Kiran
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the replies. I wanted to know why I had to make the changes before I changed my code
--Saumya.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to making future changes easier, you can make current transformaions possible. This example was from a JDC Tech Tips that loads up a low-overhead HashMap and then converts it to a sorted TreeMap.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic