As of now UniqueList requires a Comparator (or a source of Comparables) to
However, in order to identify duplicates and remove them, I think the concept of
equality (without the need of ordering) should be enough.
So, I think it would be useful if UniqueList could actually be used to get a set
from a source list, even when this is made of non comparable objects.
A first attempt is to internally use a comparator like the following:
public class EqualityComparator implements Comparator<Object>
public int compare(Object o1, Object o2)
- returns 0 if and only if two objects are the same or equal
- returns a positive or negative integer when two objects are different and not
equal (see note)
So, it satisfies the contract of java.util.Comparator, although defining an
ordering that is not meaningful for our purposes.
Note: I would however make an extra check while performing the grouping
operation using Object.equals method, because I'm not a VM expert and I don't
know what can happen in 64 bit systems... I mean, AFAIK the calculation of the
hash code based on identity, maps to the memory address of an object, but on 64
bit systems addresses can be 64-bit long, while the hashcode is a 32-bit number.
So, using the criteria "EqualityComparator.compare(o1,o2)==0" to identify
duplicates might fail in recognizing two actually different objects if they have
the same identity hash code (I know this is very unlikely to happen, but maybe
So, this request enhancement is to enable UniqueList to explicitely and
appropriately support this scenario.