TreeSet<Teacher>ts = new TreeSet<>(new Comparator<Teacher>() {
@Override
public int compare(Teacher s1, Teacher s2) {
int num = s2.getSum()-s1.getSum();
int num2 = num ==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
Teacher t1 = new Teacher("cangjingkong",98,99);
Teacher t2 = new Teacher("xiaozemaliya",100,99);
Teacher t3 = new Teacher("qiaobenhuannai",98,100);
Teacher t4 = new Teacher("boduoyejieyi",99,98);
Teacher t5 = new Teacher("nongzeluola",85,95);
Teacher t6 = new Teacher("jizemingbu",100,100);
ts.add(t1);
ts.add(t2);
ts.add(t3);
ts.add(t4);
ts.add(t5);
ts.add(t6);
for (Teacher t : ts) {
System.out.println(t.getName()+","+t.getChinese()+","+t.getMath()+","+t.getSum());
}
学习了如果使用TreeSet集合Comparator的匿名内部类方法. |
|