使用Comparable和Comparator两个接口实现自定义排序操作

一、使用Comparable

   1、 封装学生类,实现Comparable接口。重写compareTo方法。自定义排序规则。

import java.util.Objects;

public class Student implements Comparable {
    private String name;
    private int age;
     private float grades;

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

    public Student() {
    }

    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;
    }

    public float getGrades() {
        return grades;
    }

    public void setGrades(float grades) {
        this.grades = grades;
    }

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

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grades=" + grades +
                '}';
    }
    // 定义排序规则
    @Override
    public int compareTo(Student o) {
        int n1=(int)(this.grades-o.grades);
        int n2=this.age-o.age;
        return n1==0?n2:n1;
    }
}

2、编写测试类

        四位同学的成绩做升序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。

public class Demo3 {
    public static void main(String[] args) {
        TreeSet treeSet=new TreeSet<>();
            Student s1=new Student("张三", 20, 90.0f);
            Student s2=new Student("李四", 22, 90.0f);
            Student s3=new Student("小王", 20, 99.0f);
            Student s4=new Student("小刘", 22, 100.0f);
               treeSet.add(s1);
               treeSet.add(s2);
               treeSet.add(s3);
               treeSet.add(s4);
             System.out.println(treeSet);
        System.out.println("-----------------------------");
        for (Student student : treeSet) {
            System.out.println(student);
        }


    }
}

二、使用Comparator实现排序。

  测试类:

public class Demo3 {
    public static void main(String[] args) {
        TreeSet treeSet=new TreeSet<>(new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1=(int) (o1.getGrades()-o2.getGrades());
                int n2=o1.getAge()-o2.getAge();
                return n1==0?n2:n1;
            }
        });
            Student s1=new Student("向三", 20, 90.0f);
            Student s2=new Student("李四", 22, 90.0f);
            Student s3=new Student("小王", 20, 99.0f);
            Student s4=new Student("小刘", 22, 100.0f);
               treeSet.add(s1);
               treeSet.add(s2);
               treeSet.add(s3);
               treeSet.add(s4);
             System.out.println(treeSet);
        System.out.println("-----------------------------");
        for (Student student : treeSet) {
            System.out.println(student);
        }



    }
}

 核心代码:

 TreeSet treeSet=new TreeSet<>(new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1=(int) (o1.getGrades()-o2.getGrades());
                int n2=o1.getAge()-o2.getAge();
                return n1==0?n2:n1;
            }
        });

 

你可能感兴趣的:(使用Comparable和Comparator两个接口实现自定义排序操作)