This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion 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: 2398
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.
I agree. Here's the link:
subject: HashMap()
It's not a secret anymore!