wood burning stoves 2.0*
The moose likes Java in General and the fly likes Is there collection whcih does not allow duplicate elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there collection whcih does not allow duplicate elements" Watch "Is there collection whcih does not allow duplicate elements" New topic

Is there collection whcih does not allow duplicate elements

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 247
Hello Friends
IS there any collections available in jakarta commons or in java sdk which
will help me do validation as we enter values into it. I am looking to store account no/sortcode in a collection and if are trying to insert the same accountno/sortcode combination again i should get validation errors.
Wer can have duplicate sortcodes but account numbers is unique and entering
this combinationa again the collection should do validation and instruct errors.
Please help me.

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Perhaps look at java.util.Set.
marc weber

Joined: Aug 31, 2004
Posts: 11343

A Set will not allow duplicates, but it's fairly "quiet" about rejecting duplicates, so you would need to write your own "red flag" code around it. If you try to add a duplicate to a Set, the add method will return false, so maybe you could use something like...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
java.util.Set is a collections type that you might consider. It has the limitation that the notion of "duplicate" is associated with the type that you are storing. In a similar way that order is intrinsic to a type (by implementing Comparable), Sun recognised that there was a limitation - but didn't identify it since ultimately it invalidates OO - and so created the Comparator interface. Unfortunately, the same has not been done for equals - since this method exists on java.lang.Object.

You might consider net.tmorris.adt.set.Set which remedies this situation.

Tony Morris
Java Q&A (FAQ, Trivia)
Don't get me started about those stupid light bulbs.
subject: Is there collection whcih does not allow duplicate elements
Similar Threads
jFormattedTextField issue
CMP EJB Finder Method for Primary Key Collection
Validation and binding in Spring3
Finding ActionForm errors in Action
Problem with Log in to the prometric site ?