Comparable与Comparator的区别

1.理论知识

  1. Comparable
    实现Comparable接口,重写compareTo方法。一般在实体类定义的时候就可以选择实现该接口,提供一个默认的排序方式,供Arrays.sort和Collections.sort使用,只有一种排序方式,很难满足复杂的排序要求。

compareTo方法的返回值是int,有三种情况:
a、当前对象大于被比较对象(compareTo方法里面的形参),返回正整数
b、当前对象等于被比较对象,返回0
c、当前对象小于被比较对象,返回负整数

  1. Comparator
    实现Comparator接口,重写compare方法,一般在实体类中未定义排序方法或实体类中的排序方法不满足需求的情况下来实现该接口。实现方法可以是写一个比较器类,也可是匿名内部类。实现方式灵活,可以提供多种排序选择。

Ccompare方法有两个参数o1和o2,分别表示待比较的两个对象,方法返回值也是int,有三种情况:
a、o1大于o2,返回正整数
b、o1等于o2,返回0
c、o1小于o2,返回负整数

2.代码验证

上代码

实体类Student,实现Comparable接口

public class Student implements Comparable<Student> {
    private int age;
    private String address;
    private int score;

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

    public int getAge() {
        return age;
    }

    public String getAddress() {
        return address;
    }

    public int getScore() {
        return score;
    }

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

    public void setAddress(String address) {
        this.address = address;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", address='" + address + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
//        return this.age - o.age;
        return o.age - this.age;
    }
}

测试类

public class SortTest {
    public static void main(String[] args) {
        Student s1 = new Student(18, "西安", 98);
        Student s2 = new Student(6, "广州", 68);
        Student s3 = new Student(29, "天水", 84);
        Student s4 = new Student(18, "广州", 84);
        Student[] students = new Student[4];
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        students[3] = s4;
        Arrays.sort(students);
//        Arrays.sort(students,new StudentScoreComparator());
//        Arrays.sort(students, new Comparator() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getScore() - o2.getScore();
//            }
//        });
//        Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());
//        Arrays.sort(students, Comparator.comparingInt(Student::getScore));
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }

    }
}

比较器类

public class StudentScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getScore() == o2.getScore()) {
            return o1.getAge() - o2.getAge();
        }
        return o2.getScore() - o1.getScore();
    }
}
  1. 使用默认排序(前提是实体类实现Comparable接口,否则会报错)

Arrays.sort(students);
compareTo方法:用age字段排序
正序:this.age - o.age;
逆序:o.age - this.age;

  1. 使用比较器类排序
    Arrays.sort(students,new StudentScoreComparator());
    compare方法:score逆序,age正序
    正序:o1-o2
    逆序:o2-o1

  2. 匿名内部类排序:score正序
    Arrays.sort(students, new Comparator() {
    @Override
    public int compare(Student o1, Student o2) {
    return o1.getScore() - o2.getScore();
    }
    });

lambda简化:Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());

正序可以再简化,逆序不行
Arrays.sort(students, Comparator.comparingInt(Student::getScore));

你可能感兴趣的:(#,Java基础,java,Comparable,Comparator)