From: Tom Anderson on 1 Mar 2010 18:24 On Mon, 1 Mar 2010, laredotornado wrote: > I"m using Java 1.5. Given a java.util.List that I know to have at least > one element, what is the best way to check that all elements in the list > are unique ? List<T> l; boolean allUnique = l.size() == new HashSet<T>(l).size(); tom -- Virtually everything you touch has been mined. -- Prof Keith Atkinson
From: Mike Schilling on 1 Mar 2010 18:30 John B. Matthews wrote: > In article > <95bd0b1b-e372-4981-a6cf-eed5a58e4461(a)u19g2000prh.googlegroups.com>, > laredotornado <laredotornado(a)zipmail.com> wrote: > >> I'm using Java 1.5. Given a java.util.List that I know to have at >> least one element, what is the best way to check that all elements in >> the list are unique ? > > You should be able to construct a Set, say TreeSet, of the List > elements and see if the sizes match. TreeSet requires the elements be comparable. HashSet will work for any element type.
From: Tom Anderson on 1 Mar 2010 18:36 On Mon, 1 Mar 2010, John B. Matthews wrote: > In article > <95bd0b1b-e372-4981-a6cf-eed5a58e4461(a)u19g2000prh.googlegroups.com>, > laredotornado <laredotornado(a)zipmail.com> wrote: > >> I'm using Java 1.5. Given a java.util.List that I know to have at >> least one element, what is the best way to check that all elements in >> the list are unique ? > > You should be able to construct a Set, say TreeSet, of the List elements > and see if the sizes match. I am tickled that we've all thought of exactly the same solution! Are there any other interesting ways to do this? The only improvement, of sorts, i'd offer is: boolean areAllUnique(List<T> l) { Set<T> seen = new HashSet<T>(); for (T o: l) if (!seen.add(0)) return false; return true; } Which doesn't need to examine any more elements of the list than it absolutely has to to decide if the list is all unique. Except for zero- and one-element lists, that is. The way i'd do this in shell script is (not tested!): l="space separated list" [[ $(echo $l | wc -w) -eq $(echo $l | tr ' ' '\n' | sort -u | wc -w) ]] Which suggests the following java: boolean areAllUnique(List<T> l) { l = new ArrayList<T>(l); Collections.sort(l); T prev = null; for (T cur: l) { if (cur.equals(prev)) return false; prev = cur; } return true; } Which is pretty different, but still built around the idea of sorting and then detecting duplicates by comparing adjacent elements, as sort -u does. tom -- Virtually everything you touch has been mined. -- Prof Keith Atkinson
From: Joshua Cranmer on 1 Mar 2010 18:53 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
From: Mike Schilling on 1 Mar 2010 19:36
Tom Anderson wrote: > > boolean areAllUnique(List<T> l) { > l = new ArrayList<T>(l); > Collections.sort(l); > T prev = null; > for (T cur: l) { > if (cur.equals(prev)) return false; > prev = cur; > } > return true; > } > > Which is pretty different, but still built around the idea of sorting > and then detecting duplicates by comparing adjacent elements, as sort > -u does. This assumes the elements can be ordered, so it fails to be general for the same reason that the TreeSort solution does. |