-2
public Test
{
int a;
int b;
Test(int a,int b) {
this.a=a;
this.b=b
}
public static void main(String args[]) {
Test test1 = new Test(1,2);
Test test2 = new Test(2,1);
SortedSet<Test> set = new TreeSet<Test>();
set.add(test1);
set.add(test2);
}
}
现在,如果我通过名为set
的SortedSet
迭代,其中test1
或test2
会更早遇到,为什么?
从文档中的第二句话:“这些元素是使用它们的自然顺序进行排序的,或者是通过在有序集创建时提供的比较器进行排序的。”在*请求堆栈溢出帮助之前,请执行研究*。 –
您的'Test'类不是'Comparable'(它不'执行'),也没有向'TreeSet'构造函数提供'Comparator '的实例。不确定你的意思,或者你的问题是什么。 –
你有没有在询问之前运行你的代码?我不认为你的代码工作。你可以在问之前谷歌搜索吗?! –