• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there collection whcih does not allow duplicate elements

 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks
Farouk
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps look at java.util.Set.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic