比较器中的返回值实际上是什么意思?比较器的返回值
例如:
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t=new TreeSet(new MyComparator());
t.add(new Integer(20));
t.add(new Integer(10));
t.add(new Integer(30));
t.add(new Integer(100));
System.out.println(t);
}
class MyComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
return 0;
}
}
}
如果返回类型为1,则其实际上返回
[20,10,30,100]
如果返回类型是-1,则其实际返回
[100,30,10,20]
如果返回类型为0,那么它实际上返回
[20]
请告诉我这说明什么?
通常“0”表示“==”,“1”表示“>”,“-1”表示“<” –
“1”,“-1”和“0”不是类型,而是值。 –
比较器的功能在其文档中有很好的阐述。尝试阅读一点之前,只是问一个问题。 – Dunes