aspose file tools*
The moose likes Java in General and the fly likes Duplicate key insertion in a Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Duplicate key insertion in a Hashmap " Watch "Duplicate key insertion in a Hashmap " New topic
Author

Duplicate key insertion in a Hashmap

Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
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 ??


Thanks
Gaurav.... Cheers....
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
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

Joined: Nov 18, 2008
Posts: 433
    
    2
What about the JavaDocs java.util.HashMap#put(K, V)?
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 110

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.


"Learning is a Culture where your Eagerness & Curiosity plays a major Role".
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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)


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
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

Joined: Mar 07, 2010
Posts: 33
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

Joined: Mar 20, 2007
Posts: 317
No problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Duplicate key insertion in a Hashmap