commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Price <epr...@ptc.com>
Subject Re: Removing duplicates from a Collection
Date Mon, 12 May 2003 16:40:33 GMT


Adam Sherman wrote:

> 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.

This doesn't sound like it will cause you problem; this sounds like 
instructions for those who wish to implement their own Set.  If you use 
TreeSet or HashSet or something, this shouldn't be an issue.  (I think?)

Here's a quote from TreeSet:


TreeSet

public TreeSet(Comparator c)

     Constructs a new, empty set, sorted according to the specified 
comparator. All elements inserted into the set must be mutually 
comparable by the specified comparator: comparator.compare(e1, e2) must 
not throw a ClassCastException for any elements e1 and e2 in the set. If 
the user attempts to add an element to the set that violates this 
constraint, the add(Object) call will throw a ClassCastException.



If you find that performance is not great, you might find better (or 
worse) performance by converting to an Array and using Arrays.sort, or 
Collections.sort, etc, using the Comparator.



Erik



Erik


Mime
View raw message