package J20250722;

import java.util.TreeSet;

/**
 * @author WanJl
 * @version 1.0
 * @title Demo06_TreeSet
 * @description
 * @create 2025/7/22
 */
public class Demo06_TreeSet {
    public static void main(String[] args) {
        //TreeSet创建对象使用的是无参构造方法----自然排序
        TreeSet<Student> set=new TreeSet<>();
        Student s1=new Student(1,"张三",200);
        Student s2=new Student(2,"李四",15);
        Student s3=new Student(3,"王五",225);
        Student s4=new Student(4,"赵六",5);
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);

        //遍历集合
        for(Student s:set){
            System.out.println(s);
        }

        //如果集合中存入的对象没有实现Comparable接口，没有重写CompareTo方法
        //但是还想存入到TreeSet集合中，就需要使用有参构造方法

        Teacher t1=new Teacher(1,"张老师");
        Teacher t2=new Teacher(2,"刘老师");
        Teacher t3=new Teacher(3,"李老师");
        Teacher t4=new Teacher(4,"赵老师");
        Teacher t5=new Teacher(5,"王老师");
        //按照ID从大到小排序--自定义排序规则
        TreeSet<Teacher> set1=new TreeSet<>((o1,o2)->{
            int result=o2.getId()-o1.getId();
            result=result==0?o1.getName().compareTo(o2.getName()):result;
            return result;
        });
        set1.add(t1);
        set1.add(t2);
        set1.add(t3);
        set1.add(t4);
        set1.add(t5);
        for(Teacher t:set1){
            System.out.println(t);
        }



    }
}
