PDA

View Full Version : Unique in Java?



abraxas334
July 6th, 2011, 02:44 PM
Simple problem really:
I have a Vector definied like this:
Vector<int []> v;

now the int array passed is always of length 2 so I end up with vectors looking like this:
0 15
0 15
12 18
15 12
15 12
15 12
18 19
18 19
19 21
19 21
19 21

I just want all single occurances of each set of numbers. In C++ I 'd just sort my 2-d vector and then use unique on it. Is there an equivalent in java?
I guess it is already sorted in this case, but how do I get rid of the duplicates?
Thanks,

PaulM1985
July 6th, 2011, 03:10 PM
You could use a hash set. http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html

This will only contain unique entries.

Paul

cgroza
July 6th, 2011, 03:38 PM
Use a set. It will take care of that for you.