Author
is there any way to insert duplicate key in HashMap?
Neeraj Dhiman
Ranch Hand
Joined: Dec 19, 2011
Posts: 61
I have data which contain duplicates keys. How can i achieve this with HashMap ?. is there any way?
Correct Me if i am wrong
John Jai
Rancher
Joined: May 31, 2011
Posts: 1372
posted Feb 02, 2012 05:45:39
2
Either use Map<String ,List<Object>> where the key refers to a List of values or Google Guava (I read in this thread)
somesh kanti
Greenhorn
Joined: Feb 02, 2012
Posts: 3
Neeraj Dhiman wrote: I have data which contain duplicates keys. How can i achieve this with
HashMap ?. is there any way?
you can add duplicates keys but , already present value of the same key will be changed and the new value will be updated there self.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
And welcome to the Ranch , somesh kanti
somesh kanti
Greenhorn
Joined: Feb 02, 2012
Posts: 3
Campbell Ritchie wrote: And welcome to the Ranch
, somesh kanti
Thank you Ritchie
somesh kanti
Greenhorn
Joined: Feb 02, 2012
Posts: 3
You can use multimap instead of that one.
http://www.sgi.com/tech/stl/Multimap.html
this is the link where you can find the details about multimap.
Wendy Gibbons
Bartender
Joined: Oct 21, 2008
Posts: 774
John Jai wrote: Either use Map<String,List<Object>> where the key refers to a List of values or Google Guava (I read in
this thread)
good comment from the thread you directed us to, use a map<String,Set<object>> to remove real duplicates
John Jai
Rancher
Joined: May 31, 2011
Posts: 1372
posted Feb 02, 2012 08:47:24
0
Yes Wendy... you are right.
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18370
Although you're right in using a multimap (Google Guava has an implementation), are you aware that the page you linked to is about the C++ STL multimap?
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Neeraj Dhiman
Ranch Hand
Joined: Dec 19, 2011
Posts: 61
Thanks you All. Problem Solved using Map<String,List<Object>>.
subject: is there any way to insert duplicate key in HashMap?