Re: Best way to check if all elements in a List are unique
- From: Joshua Cranmer <Pidgeot18@xxxxxxxxxxxxxxx>
- Date: Mon, 01 Mar 2010 18:53:11 -0500
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
.
- Follow-Ups:
- Re: Best way to check if all elements in a List are unique
- From: Tom Anderson
- Re: Best way to check if all elements in a List are unique
- References:
- Best way to check if all elements in a List are unique
- From: laredotornado
- Re: Best way to check if all elements in a List are unique
- From: John B. Matthews
- Re: Best way to check if all elements in a List are unique
- From: Tom Anderson
- Best way to check if all elements in a List are unique
- Prev by Date: Re: EL and regular expression
- Next by Date: Re: [OT] Secure storage
- Previous by thread: Re: Best way to check if all elements in a List are unique
- Next by thread: Re: Best way to check if all elements in a List are unique
- Index(es):
Relevant Pages
|