File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes HashMap() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap()" Watch "HashMap()" New topic


Reghuram Variyam

Joined: Oct 31, 2006
Posts: 17
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("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 ?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
It is sorta covered in the JavaRanch Style Guide.
subject: HashMap()
It's not a secret anymore!