commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Sherman <>
Subject Re: Removing duplicates from a Collection
Date Mon, 12 May 2003 16:00:06 GMT
On 05/12/03 11:26:25 -0400 Erik Price wrote:

> If you aren't sure that you want to commit to making your objects
> dependent on that single property for equals, my recommendation is that
> you implement a Comparator that "knows" whether two instances of your
> class are "equivalent"  for the purposes of your List, and then pass your
> List to a Set that uses the Comparator that you've specified.
> This lets you make your equals() a "real" equals, and still get the
> effect you want where only one property is significant.

This is the best solution, thanks!

I note, however:

"Note that the ordering maintained by a set (whether or not an explicit 
comparator is provided) must be consistent with equals if it is to 
correctly implement the Set interface. (See Comparable  or Comparator for a 
precise definition of consistent with equals.) This is so because the Set 
interface is defined in terms of the equals operation, but a TreeSet 
instance performs all key comparisons using its compareTo (or compare) 
method, so two keys that are deemed equal by this method are, from the 
standpoint of the set, equal. The behavior of a set is well-defined even if 
its ordering is inconsistent with equals; it just fails to obey the general 
contract of the Set interface."

I guess this the price to pay.

Does anyone have an idea about performance?



Adam Sherman
Tritus CG Inc.
+1 (613) 797-6819

View raw message