• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Duplicate check in colection

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following requirement. I need to keep a set of key value pair in a collection and also against a particulat key no duplicate values should be added.

Could anyone help in doing the same?
 
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Rohit S", please check your private messages for an important administrative matter.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rohit S:
I have the following requirement. I need to keep a set of key value pair in a collection and also against a particulat key no duplicate values should be added.

Could anyone help in doing the same?



You require to throw an excpetion if a value is already present for the key?
If not then you can very well use a HashMap.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i understand, the map implementations by itself do not prevent duplicates. You may also want to try out one of the set implementations.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by karthik vijayarajan:
As i understand, the map implementations by itself do not prevent duplicates. You may also want to try out one of the set implementations.



Maps do, indeed, by definition, prevent duplicate keys. They don't prevent duplicate values. If you're using a Set, then there are no values, of course, only keys, so it's no different.

Note that all the Set implementations that are part of the standard API are actually implemented in terms of a Map; it's just a Map that stores a key against itself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!