IntelliJ Java IDE
The moose likes Beginning Java and the fly likes is there any way to insert duplicate key in HashMap? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "is there any way to insert duplicate key in HashMap?" Watch "is there any way to insert duplicate key in HashMap?" New topic
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
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
Yes Wendy... you are right.
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18370

somesh kanti wrote: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.

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>>.
 
IntelliJ Java IDE
 
subject: is there any way to insert duplicate key in HashMap?
 
Threads others viewed
how to access the servlet's context in a different file?
hashmap key values
ArrayList/HashMap - Sorting
Sorting Keys(Value Objects) in HashMap
Sorting of object
developer file tools