java Comparable 和Comparator详解


前言

  Comparable 和Comparator分别对应我们所说的自然排序和比较器排序,Comparator相比于Comparable 要更加灵活,一个类实现Comparable接口并实现compareTo(T o)方法(该方法的返回值是 int 。如果返回值为正数,则表示当前对象(调用该方法的对象)比 obj 对象“大”;反之“小”;如果为零的话,则表示两对象相等)就可定义一种比较方式,但有时需要对同一对象进行多种不同方式的排序,这时,我们难道要去改掉compare(T o)里面的实现代码吗?答案是否定的,为了弥补这个不足,JDK 还为我们提供了另外一个排序方式,也就是比较器排序。

一、Comparable介绍

  若一个类实现了Comparable接口,就意味着该类支持排序。Comparable是一个接口,里面只有一个方法。

package java.lang;
import java.util.*;
public interface Comparable<T> 
{
    public int compareTo(T o);
}

实现了Comparable接口的类的对象的列表或数组可以通过Collections.sort或Arrays.sort进行自动排序。

代码例子:

Student类:

public class Student  implements Comparable<Student>{
    private String name;
    private int age;
    private int tall;//身高

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


    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 int getTall() {
        return tall;
    }

    public void setTall(int  tall) {
        this.tall = tall;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tall='" + tall + '\'' +
                '}';
    }
    @Override
    public int compareTo(Student o) {
        if(o.age > age){
            return -1;
        }else if(o.age < age){
            return 1;
        }
        return 0;
    }
}

测试代码:

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[10];
        for(int i=0;i<10;++i){
            students[i] = new Student(Integer.toString(i),new Random().nextInt(20),new Random().nextInt(160));
        }
        //Arrays.sort(students);
        //Collections.sort(Arrays.asList(students));
        for(Student stu : students){
            System.out.println(stu);
        }
    }
}

二、Comparator介绍

  Comparator是比较接口,我们如果需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口),那么我们就可以建立一个“该类的比较器”来进行排序,这个“比较器”只需要实现Comparator接口即可。也就是说,我们可以通过实现Comparator接口并实现compare(T o1, T o2) 方法来新建一个比较器,然后通过这个比较器对类进行排序。

现在有一个需求,要求我们按身高对学生进行一个排序,又不让我们去改动上面的代码怎么办?这时候,Comparator的优势就发挥出来了。

代码如下:

public class Test{
    public static void main(String[] args) {
        Student[] students = new Student[10];
        for(int i=0;i<10;++i){
            students[i] = new Student(Integer.toString(i),new Random().nextInt(20),new Random().nextInt(160));
        }
        myComparator mycomparator = new myComparator();
        //Arrays.sort(students,mycomparator);
        //Collections.sort(Arrays.asList(students),mycomparator);
        for(Student stu : students){
            System.out.println(stu);
        }
    }


}
class myComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getTall() > o2.getTall() ? 1 : o1.getTall() == o2.getTall() ? 0 : -1;
    }
}

总结

两者相比:
1、 用Comparable相对简单, 只要实现Comparable 接口的对象直接就成为一个可以比较的对象,但是需要修改类的源代码,对类的破坏性比较大。
2、用Comparator 的好处是不需要修改源代码, 而是另外实现一个比较器, 当某个自定义的对象需要作比较的时候,把比较器和对象一起传递过去就可以比大小了, 并且在Comparator 里面用户可以自己实现复杂的可以通用的逻辑,使其可以匹配一些比较简单的对象,相对于Comparable更加灵活,更加解耦。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值