• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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?
 
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)
 
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.
 
Marshal
Posts: 60136
188
  • 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.
 
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.
 
Sheriff
Posts: 21459
96
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>>.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!