在这个程序中它没有识别compartor
,我不想使用compareTo
.
class TestTreeSet
{
public static void main(String args[])
{
BuildObject build = new BuildObject();
TreeSet treeSet = new TreeSet( new Compared());
treeSet = build.sendAsTreeSet();
Iterator itr = treeSet.iterator();
while(itr.hasNext())
{
Obj object = (Obj) itr.next();
System.out.println(object.getName() + "\n " + object.getAge() + "\n "
+ object.getSalary());
}
}
}
这是比较器
class Compared implements Comparator
{
public int compare(Object a , Object b)
{
Obj one = (Obj) a;
Obj two = (Obj) b;
double salaryOne = one.getSalary();
double salaryTwo = two.getSalary();
int ageOne = one.getAge();
int ageTwo = two.getAge();
if( ageOne == ageTwo)
{
if(salaryOne > salaryTwo)
{
return -1;
}
else if(salaryOne < salaryTwo)
{
return 1;
}
else return 0;
}
else if(ageOne > ageTwo)
return -1;
else return 1;
}
}
问题是什么 ?它显示不能转换为java.lang.comparable
异常