• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any way to insert duplicate key in HashMap?  RSS feed

 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have data which contain duplicates keys. How can i achieve this with HashMap?. is there any way?
 
John Jai
Rancher
Posts: 1776
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch , somesh kanti
 
somesh kanti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch , somesh kanti


Thank you Ritchie
 
somesh kanti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Wendy... you are right.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you All. Problem Solved using Map<String,List<Object>>.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!