• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

HashMap and Map

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the advantage of using
Map map = new HashMap()
vs
HashMap map = new HashMap()
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic