• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we use put() for a HashMap(), does it update update the entry or add a new entry ?

For example, if I have a Map like
Map<String, String> m = new HashMap<String, String> ();
m.put("k1","George");
m.put("k2", "John");
m.put("k1", "Allan");

I thought, in this case k1 will point to both "George" and "Allan" and we will be able to retrieve both "Geroge" and "Allan" using key "k1". But it doesn't looks like that is not the case, After the above code is executed, I could see "k1" pointing to "Allan" only. Can any one explain the reason for this ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key in the map can only point to one value.

If you put it another entry where the key is already in the map, you will lose the first value.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic