• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicate check in colection

 
Rohit Sajan
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Rohit S", please check your private messages for an important administrative matter.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 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.
 
Karthik Vijayarajan
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • 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