• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Duplicate check in colection

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic