It's not a secret anymore!*
The moose likes Java in General and the fly likes Duplicate check in colection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Duplicate check in colection" Watch "Duplicate check in colection" New topic
Author

Duplicate check in colection

Rohit Sajan
Ranch Hand

Joined: Jun 15, 2006
Posts: 34
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

Joined: Jan 10, 2002
Posts: 61310
    
  66

"Rohit S", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Karthik Vijayarajan
Ranch Hand

Joined: Mar 13, 2005
Posts: 43
As i understand, the map implementations by itself do not prevent duplicates. You may also want to try out one of the set implementations.


Karthik <br />SCJP 1.4 SCWCD 1.4 SCBCD 1.3
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
GeeCON Prague 2014
 
subject: Duplicate check in colection