创建student的对象,并按方法一实现Comparable接口
public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }

    @Override
    public int compareTo(Student o) {
        return this.getAge()-o.getAge();
    }
}
-----------------------------------------
public class TreeSetDemo1 {
    public static void main(String[] args) {
        //TreeSet是可以对元素进行排序的集合
        //TreeSet底层是红黑树结构

        TreeSet<Integer> ts1=new TreeSet<>();
        ts1.add(4);
        ts1.add(1);
        ts1.add(3);
        ts1.add(2);
        System.out.println(ts1);
        //遍历
        //1.
        Iterator<Integer> it=ts1.iterator();
        while(it.hasNext()){
            int i=it.next();
            System.out.println(i);
        }
        System.out.println("-----------------");
        //2.
        for (Integer i : ts1) {
            System.out.println(i);
        }
        System.out.println("-----------------");
        //3.
        ts1.forEach(i-> System.out.println(i));
        System.out.println("-----------------");


        TreeSet<Student> ts=new TreeSet<>();
        Student s1 = new Student("zhangsan", 18);
        Student s2 = new Student("zhangsan", 18);
        Student s3 = new Student("lisi", 19);
        Student s4 = new Student("wangwu", 17);
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        //方式一:Student类实现Comparable接口,重写其中的方法
        //顺序遍历this.getAge()-o.getAge()
        //逆序遍历o.getAge()-this.getAge()
        //this表示当前要添加的元素
        //o表示已经存在的元素
        System.out.println(ts);
        //方式二:TreeSet集合的构造器中传递Comparator比较器
        TreeSet<Student> ts3=new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i = o1.getAge() - o2.getAge();
                i= i==0?o1.getName().compareTo(o2.getName()):i;//先按年龄从小到大排序,再按姓名排序
                return i;
            }
        });
        //简化
        TreeSet<Student> ts2=new TreeSet<>((o1, o2) -> {
            int i = o1.getAge() - o2.getAge();
            i= i==0?o1.getName().compareTo(o2.getName()):i;//先按年龄从小到大排序,再按姓名排序
            return i;//compareTo是默认的比较方法
        });
        ts2.add(s1);
        ts2.add(s2);
        ts2.add(s3);
        ts2.add(s4);
        System.out.println(ts2);//[Student{name = wangwu, age = 17}, Student{name = zhangsan, age = 18}, Student{name = lisi, age = 19}]

    }
}
--------------------------------------------------------------------------------
public class Student1 {
    private String name;
    private int age;
    private double chinese;
    private double math;
    private double english;

    public Student1() {
    }

    public Student1(String name, int age, double chinese, double math, double english) {
        this.name = name;
        this.age = age;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return chinese
     */
    public double getChinese() {
        return chinese;
    }

    /**
     * 设置
     * @param chinese
     */
    public void setChinese(double chinese) {
        this.chinese = chinese;
    }

    /**
     * 获取
     * @return math
     */
    public double getMath() {
        return math;
    }

    /**
     * 设置
     * @param math
     */
    public void setMath(double math) {
        this.math = math;
    }

    /**
     * 获取
     * @return english
     */
    public double getEnglish() {
        return english;
    }

    /**
     * 设置
     * @param english
     */
    public void setEnglish(double english) {
        this.english = english;
    }

    public String toString() {
        return "Student1{name = " + name + ", age = " + age + ", chinese = " + chinese + ", math = " + math + ", english = " + english +", 总分"+(chinese+math+english)+ "}";
    }
}
------------------------------------------------------
public class TreeSetTest1 {
    public static void main(String[] args) {
        TreeSet<Student1> treeSet = new TreeSet<>((o1,o2)->{
            //按总分高到低排序
            double i=(o2.getChinese()+ o2.getMath()+ o2.getEnglish())
                    -(o1.getChinese()+ o1.getMath()+ o1.getEnglish());
            //总分相同,按语文成绩高到低排序
            i=i==0?o2.getChinese()-o1.getChinese():i;
            //总分和语文成绩相同,按数学成绩高到低排序
            i=i==0?o2.getMath()-o1.getMath():i;
            //总分和语文成绩和数学成绩相同,按英语成绩高到低排序
            i=i==0?o2.getEnglish()-o1.getEnglish():i;
            //总分和语文成绩和数学成绩和英语成绩相同,按年龄高到低排序
            i=i==0?o2.getAge()- o1.getAge():i;
            //总分和语文成绩和数学成绩和英语成绩和年龄相同,按姓名排序
            i=i==0?o1.getName().compareTo(o2.getName()):i;
            //对i的正负判断,以免强转时出现问题
            if(i<0){
                i=-1;
            }else if(i>0){
                i=1;
            }else{
                i=0;
            }
            return (int)i;
        });

        Student1 s1=new Student1("haha",18,100,99,88);
        Student1 s2=new Student1("xiangxiang",19,99,99,99);
        Student1 s3=new Student1("baby",19,99,99,98);
        treeSet.add(s1);
        treeSet.add(s2);
        treeSet.add(s3);
        treeSet.forEach((Student1 s)-> System.out.println(s));
    }
}