javaTreeSet排序

文章目录

  • TreeSet排序
    • 自然排序
      • 看代码理解
        • Student类
        • ComparableDemo1 类
    • 比较器排序
      • 看代码理解
        • Teacher类
        • ComparatorDemo类

TreeSet排序

自然排序

  ·自定义实现Comparable接口,重写compareTo方法,根据返回值进行排序。

看代码理解

Student类

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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //根据年龄的升序排序
        int result = this.age - o.age;

        //如果年龄一样,根据名字的升序排序

        return result == 0? this.name.compareTo(o.name) : result;
    }
}

ComparableDemo1 类

public class ComparableDemo1 {
    public static void main(String[] args) {

        TreeSet<Student> ts = new TreeSet<>();

        Student s1 = new Student("张三",23);
        Student s2 = new Student("王五",25);
        Student s3 = new Student("李四",24);
        Student s4 = new Student("Curry",25);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);

        for(Student t : ts){
            System.out.println(t);
        }
    }
}

比较器排序

  ·TreeSet的代餐构造方法使用的是"比较器排序"对元素进行排序。

  ·比较器排序,就是让TreeSet集合构造方法接受Comparator接口的实现类对象。

  ·重写Comparator接口中的compare(T o1,T o2)方法,指定排序规则。

  ·o1代表的是当前往集合中添加的元素,o2代表的是集合中已经存在的元素,排序原理与自然排序相同!

看代码理解

Teacher类

public class Teacher {
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(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 String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

ComparatorDemo类

public class ComparatorDemo {
    public static void main(String[] args) {
        //接受的是Comparator接口的实现类对象
        TreeSet<Teacher> ts = new TreeSet<>(new ComparatorImpl());

        ts.add(new Teacher("zhangsan",23));
        ts.add(new Teacher("lisi",25));
        ts.add(new Teacher("wangwu",24));

        for(Teacher t : ts){
            System.out.println(t);
        }

    }
}

class ComparatorImpl implements Comparator<Teacher>{

    @Override
    public int compare(Teacher o1, Teacher o2) {
        //根据年龄的升序排序
        int result = o1.getAge()-o2.getAge();

        //年龄一样,根据姓名升序

        return result==0 ? o1.getName().compareTo(o2.getName()) : result;
    }
}

学的不是技术,更是梦想!!!

你可能感兴趣的:(Java,java,算法,开发语言)