Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicate key insertion in a Hashmap

 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to know what happens when we try to insert a duplicate key in a Hashmap. It will not insert it for sure. But do it throws any compile time or run time exception ??

 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Kshatriya wrote:Hi

I want to know what happens when we try to insert a duplicate key in a Hashmap. It will not insert it for sure. But do it throws any compile time or run time exception ??



Why do you think the insertion would fail? Did you try checking it with a small snippet?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the JavaDocs java.util.HashMap#put(K, V)?
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav,
For every time you insert a key-value pair in HashMap , If the key is repeated ,it will replace the previous value with the current one. But if you really need to know more , make use of TreeSet, ArrayList, Vector combinations along with this. Try out all the methods availble in these and learn what you can do more. Understanding the basic requirement where Maps are needed will definetely help you. Just try it out.
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know what happens when we try to insert a duplicate key in a Hashmap. It will not insert it for sure. But do it throws any compile time or run time exception ??


Did you tried to write a code before making such jugdement ?

No. Map will insert the duplicate key also, but will replace the existing object. See the sample application below:-




The following code above will not give any error on compilation. The map will take the duplicate key also. How ever it is replacing the existing object in map (i.e. !!! will replace vinod)
 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for clearing the doubt. Cheers to all the members of javaranch family who makes it great. Thank you all !!!

 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridhar Santhanakrishnan wrote:
Gaurav Kshatriya wrote:Hi

I want to know what happens when we try to insert a duplicate key in a Hashmap. It will not insert it for sure. But do it throws any compile time or run time exception ??



Why do you think the insertion would fail? Did you try checking it with a small snippet?


Hi Sridhar

I tried checking with a small snippet. But the actual mistake i was doing is I was putting same value ??
Thats why i was unable to see it. Thanks Sridhar
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic