• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there collection whcih does not allow duplicate elements

 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps look at java.util.Set.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic