import java.util.*;
public class Test {
public static void main(String[] args) {
Employee Allan = new Employee("Allan");
Employee Bell = new Employee("Bell");
Employee Chris = new Employee("Chris");
Employee David = new Employee("David");
Employee Eric = new Employee("Eric");
//TreeSet<Employee> staff = new TreeSet<Employee>();
ArrayList<Employee> staff = new ArrayList<Employee>();
staff.add(David);
staff.add(Bell);
staff.add(Chris);
staff.add(Eric);
staff.add(Allan);
//Collections.shuffle(staff);
System.out.println(staff);
}
}
我已经在Emploee类里重写了compareTo方法,方法如下:
public int compareTo(Object o) {
if(o instanceof Employee) {
Employee p =(Employee)o;
return this.name.compareTo(p.name);
}
return 0;
}
在代码执行过程中发现,如果是TreeSet,就算打乱了Employee的顺序,输出结果仍然完成排序。
如果换成ArryList,就是没有拍过序的,可是我不是已经重写了compareTo方法了么。。。求解