I want to have some pairs of int in a TreeSet, and sort them by the first number.
Code test here:
public static void main (String[] args) throws java.lang.Exception
{
SortedSet<int[]> s = new TreeSet<int[]>(new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return b[0] - a[0];
}
});
int[] a = new int[]{1, 2};
int[] b = new int[]{1, 3};
s.add(a);
s.add(b);
System.out.println(s.size());
}
I don't know why the size of TreeSet turns out to be 1. Seems like the hashCode of a
and b
are same, but why?
Thanks for any help.
BTW: in fact, I was trying to put duplicate numbers in a set, which is not possible. Then I tried to have int pairs in a set. The first number is the actual number I want, the second number was there to prevent duplication. But I run into this problem.