This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicate check in colection

 
Rohit Sajan
Ranch Hand
Posts: 34
  • 0
  • 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: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic