commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitry Sklyut <Dmitry_Skl...@orcom.com>
Subject RE: Removing duplicates from a Collection
Date Mon, 12 May 2003 16:10:17 GMT
You can benchmark it very easily:

for (int i=0; i < 5000; i++) {
	Object o = new Object();
	list.add(o);
}
System.out.println(System.currentTimeMillis());
Set s = new HashSet();
s.addAll(list);
System.out.println(System.currentTimeMillis());

Something like that.  System.out takes some time also.  This will give you a
rough idea of the performance.

D


-----Original Message-----
From: Adam Sherman [mailto:adam@tritus.ca]
Sent: Monday, May 12, 2003 12:00 PM
To: Jakarta Commons Users List
Cc: Erik Price
Subject: Re: Removing duplicates from a Collection


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?

Thanks,

A.

--
Adam Sherman
Tritus CG Inc.
http://www.tritus.ca/
+1 (613) 797-6819

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message