Re: Best way to check if all elements in a List are unique



On 03/01/2010 06:36 PM, Tom Anderson wrote:
if (cur.equals(prev)) return false;

What if there are null entries in the list?

This snippet also, of course, assumes that T is a comparable object (I believe Collections.sort fails if there's no comparator and T can't be cast to Comparable).

The HashSets also assume that you're checking equality via .equals (as opposed to using ==), and that .hashCode() is correctly implemented, i.e., consistent with equals.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.