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()

 
Reghuram Variyam
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic