commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Price <>
Subject Re: Removing duplicates from a Collection
Date Mon, 12 May 2003 15:26:25 GMT

Adam Sherman wrote:

> Heh. That looks too simple. Actually, my needs are slightly complicated 
> in how I define "duplicate" is wierd.
> The items have three properties but only one of them is important and 
> defines uniqueness of the item. So I suppose that I could implement 
> Object.equals() on the item and get the desired result, yes?
> Also, this is a collection with upwards of 50K items in it. Will this be 
> performant?
> Thanks very much for your suggestions,

If you're absolutely positive that those other properties aren't 
important, then implementing equals() to consider the significant 
property is the quickest way to do it.

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.


View raw message