This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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 sscce.org
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.