From: laredotornado on
On Mar 1, 3:44 pm, "Mike Schilling" <mscottschill...(a)hotmail.com>
wrote:
> laredotornado wrote:
> > Hi,
>
> > 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 ?
>
> > Thanks, - Dave
>
> boolean areListElementsUnique(List<?> l)
> {
>     return l.size() == new HashSet<Object>(l).size();
>
> }
>
>

Winner! -
From: Lew on
Mike Schilling wrote:
>> boolean areListElementsUnique(List<?> l)
>> {
>> return l.size() == new HashSet<Object>(l).size();
>>
>> }

laredotornado wrote:
> Winner! -

Don't forget to null-check the argument!

And please do answer the question of what constitutes "best".

--
Lew
From: Tom Anderson on
On Tue, 2 Mar 2010, Lew wrote:

> Mike Schilling wrote:
>>> boolean areListElementsUnique(List<?> l)
>>> {
>>> return l.size() == new HashSet<Object>(l).size();
>>>
>>> }
>
> laredotornado wrote:
>> Winner! -
>
> Don't forget to null-check the argument!

The method already does that - if it's null, you get a
NullPointerException.

tom

--
This sig kills fascists.
From: John B. Matthews on
In article <alpine.DEB.1.10.1003021900150.13870(a)urchin.earth.li>,
Tom Anderson <twic(a)urchin.earth.li> wrote:

> On Tue, 2 Mar 2010, Lew wrote:
>
> > Mike Schilling wrote:
> >>> boolean areListElementsUnique(List<?> l)
> >>> {
> >>> return l.size() == new HashSet<Object>(l).size();
> >>>
> >>> }
> >
> > laredotornado wrote:
> >> Winner! -
> >
> > Don't forget to null-check the argument!
>
> The method already does that - if it's null, you get a
> NullPointerException.

I thought the idea was to treat empty and unitary lists as unique, not
to mention skirting the size() of null.

Here's a primitive test:

import java.util.*;

public class SetTest {

private static final int MAX = 10000;

public static void main(String[] args) {
List<String> names = new ArrayList<String>();
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < MAX; ++i) {
names.add(String.valueOf(i + MAX));
numbers.add(Integer.valueOf(i + MAX));
}
for (int i = 0; i < 3; i++) {
test(names);
test(numbers);
}
}

private static <T> void test(List<T> list) {
if (list == null) return;
int s1 = list.size();
long start = System.nanoTime();
int s2 = new HashSet<T>(list).size();
long stop = System.nanoTime();
System.out.println("HashSet: " + s1 + " " + s2
+ " " + (s1 == s2) + dt(stop - start));
start = System.nanoTime();
s2 = new TreeSet<T>(list).size();
stop = System.nanoTime();
System.out.println("TreeSet: " + s1 + " " + s2
+ " " + (s1 == s2) + dt(stop - start));
}

private static String dt(long nanos) {
return String.format("%7.3f ms.", nanos / 1000000d);
}
}

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: Volker Borchert on
Lew wrote:
> <T> boolean areListElementsUnique( List<T> list )
> {
> if (list == null || list.size() == 1 )
<=
> {
> return true;
> }
> return list.size() == new HashSet <T> (list).size();
> }

And, I'd prefer to call list.size() only once and reuse the result,
for there might be lists out there whose size() is not O(1).

Anyway, the question sounds awfully like a homework assignment...

--

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy(a)ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert <v_borchert(a)despammed.com>
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8
Prev: Reflection accessing subclass
Next: java Date to c# ticks