首页 > 代码库 > Comparable与Comparator,java中的排序与比较

Comparable与Comparator,java中的排序与比较

1:比较和排序的概念

    比较:两个实体类之间按>,=,<进行比较。

    排序:在集合类中,对集合类中的实体进行排序。排序基于的算法基于实体类提供的比较函数。

    基本型别都提供了默认的比较算法,如string提供了按字母进行比较,int提供了按整数大小进行比较。

 

2:Comparable与Comparator

but,在软件开发的世界中,任何没有代码的堆概念都是耍流氓。所以,无论我们解释的多么完美,必须show me the code;

我们首先看这样一段代码:

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

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("a", 3));
        students.add(new Student("c", 2));
        students.add(new Student("b", 1));
        students.add(new Student("d", 4));
        for (Student student : students) {
            System.out.printf("name:%s, age: %d \n", student.name, student.age);
        }
    }
}

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

 

但是现在我们需要按年龄大小进行遍历,怎么办?这个时候排序接口就发挥作用了。可以为Student提供排序的接口Comparable,如下,

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

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("a", 3));
        students.add(new Student("c", 2));
        students.add(new Student("b", 1));
        students.add(new Student("d", 4));
        Collections.sort(students);
        for (Student student : students) {
            System.out.printf("name:%s, age: %d \n", student.name, student.age);
        }
    }
}

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

    @Override
    public int compareTo(Object o) {
        Student student = (Student)o;
        if (age> student.age)
        {
            return 1;
        }
        else if (age == student.age)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        //return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
    }
}

输出的结果为:

name:b, age: 1
name:c, age: 2
name:a, age: 3
name:d, age: 4

很棒。PS:注意排序方法中最后被注释掉的一行,排序方法中的逻辑可以被这一行代替,只不过为了给大家演示期间,我还原了这个Integer的compareTo方法。

现在问题来了,我不想用年龄进行排序了,我想用姓名进行排序,该怎么办?要知道我们只能实现一个排序接口,排序接口只有一个方法。这个时候,比较器就派上用场了!

我们实现一个name的比较器,如下:

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

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("a", 3));
        students.add(new Student("c", 2));
        students.add(new Student("b", 1));
        students.add(new Student("d", 4));
        //Collections.sort(students);
        students.sort(new NameSorter());
        for (Student student : students) {
            System.out.printf("name:%s, age: %d \n", student.name, student.age);
        }
    }
}

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

    @Override
    public int compareTo(Object o) {
        Student student = (Student)o;
        if (age> student.age)
        {
            return 1;
        }
        else if (age == student.age)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        //return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
    }
}

class NameSorter implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Student s1 =(Student) o1;
        Student s2 =(Student) o2;
        return s1.name.compareTo(s2.name);
    }
}

现在的结果:

name:a, age: 3
name:b, age: 1
name:c, age: 2
name:d, age: 4

将来我们要按照性别,按照学生成绩排序,可能还会有更多更多的排序的要求,那就去实现更多的比较器就行了。

 

3:Comparable与Comparator的泛型版本Comparable<T>与Comparator<T>

如果我们稍有经验,我们就会发现上面的代码的接口Comparable与Comparator都是已经不建议使用了。当泛型出来后,所有非泛型集合类和接口都已经建议尽量不使用了。至于原因,从上面的代码中我们也可以看出一点端倪。

注意查看compare函数,如:

Student s1 =(Student) o1;
Student s2 =(Student) o2;

我们发现这个函数进行了装箱和拆箱。而这是会影响性能的。如果我们的集合中有成千上万个复杂的实体对象,则在排序的时候所耗费掉的性能就是客观的。而泛型的出现,就可以避免掉拆箱和装箱。

故,我们就该实现Comparable<T>与Comparator<T>。最终的代码如下:

package com.zuikc.se.collectors;

/**
* 排序和比较器的用法,Comparable与Comparator,java中的排序与比较
* author:最课程(zuikc.com)
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("a", 3));
        students.add(new Student("c", 2));
        students.add(new Student("b", 1));
        students.add(new Student("d", 4));
        //Collections.sort(students);
        students.sort(new NameSorter());
        for (Student student : students) {
            System.out.printf("name:%s, age: %d \n", student.name, student.age);
        }
    }
}

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student student) {
        if (age> student.age)
        {
            return 1;
        }
        else if (age == student.age)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        //return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
    }
}

class NameSorter implements Comparator<Student>{

    @Override
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name);
    }
}

Comparable与Comparator,java中的排序与比较