package com.cn2;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//比较总分 把总分变成一个num进行比较
int num=(s1.getMathnum()+s1.getYunum())-(s2.getYunum()+s2.getMathnum());
//是否num==0? 如果是的话 进行姓名的比较 返回姓名or返回num(返回num其实就是不会添加了)
int sum2=num==0?s1.getName().compareTo(s2.getName()):num;
return sum2;
}
});
Student s1 = new Student("yyy",13,12);
Student s2 = new Student("ttt",12,13);
Student s3 = new Student("ttt",13,13);
students.add(s1);
students.add(s2);
students.add(s3);
for(Student s : students){
System.out.println(s.getName()+"--"+s.getMathnum()+"--"+s.getYunum());
}
}
}
老师的: