【发布时间】:2021-07-23 13:02:33
【问题描述】:
Comparator 内部的返回值究竟是什么意思?
例如:
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”不是类型,而是值。
-
比较器的作用在其文档中有很好的阐述。在简单提问之前尝试阅读一下。
标签: java