Comparable和Comparator接口比较

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36564655/article/details/81362684

Java提供了只包含一个compareTo()方法的Comparable接口。这个方法可以个给两个对象排序。具体来说,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象。
Java提供了包含compare()和equals()两个方法的Comparator接口。compare()方法用来给两个输入参数排序,返回负数,0,正数表明第一个参数是小于,等于,大于第二个参数。equals()方法需要一个对象作为参数,它用来决定输入参数是否和comparator相等。只有当输入参数也是一个comparator并且输入参数和当前comparator的排序结果是相同的时候,这个方法才返回true。

链接:https://www.nowcoder.com/questionTerminal/99f7d1f4f8374e419a6d6924d35d9530
来源:牛客网

Comparator位于包java.util下,而Comparable位于包 java.lang下 Comparable 是一个对象本身就已经支持自比较所需要实现的接口(如 String、Integer 自己就可以完成比较大小操作,已经实现了Comparable接口) 自定义的类要在加入list容器中后能够排序,可以实现Comparable接口,在用Collections类的sort方法排序时,如果不指定Comparator,那么就以自然顺序排序, 这里的自然顺序就是实现Comparable接口设定的排序方式。 而 Comparator 是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足你的要求时,你可以写一个比较器来完成两个对象之间大小的比较。 可以说一个是自已完成比较,一个是外部程序实现比较的差别而已。 用 Comparator 是策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。 比如:你想对整数采用绝对值大小来排序,Integer 是不符合要求的,你不需要去修改 Integer 类(实际上你也不能这么做)去改变它的排序行为,只要使用一个实现了 Comparator 接口的对象来实现控制它的排序就行了。

Comparable的使用例子:

import java.util.Arrays;
public class ComparableSample {
    public static void main(String[] args) throws Exception{
        Student stu[] = {new Student("张三",22,80f)
                ,new Student("李四",23,83f)
                ,new Student("王五",21,80f)};

        Arrays.sort(stu);   //进行排序操作
        for (int i = 0; i < stu.length; i++) {
            Student s = stu[i];
            System.out.println(s);
        }
    }
}
class Student implements Comparable<Student>{
    private String name;
    private int age;
    private float score;

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

    @Override
    public int compareTo(Student stu) {  //覆写compareTo方法实现排序规则的应用
        if(this.score>stu.score){
            return -1;
        }else if(this.score<stu.score){
            return 1;
        }else{
            if(this.age>stu.age){
                return 1;
            }else if(this.age<stu.age){
                return -1;
            }else{
                return 0;
            }
        }
    }

    public String toString(){
        return "姓名:"+this.name+", 年龄:"+this.age+", 成绩:"+this.score;
    }
}

Comparator的列子:

import java.util.Iterator;
//Comparable是一个类部比较器而 Comparator是一个外部比较器
//comparator接口:
//元素的排序依据时刻变的,所以可以通过定义多个外部类的方式来实现不同的排序。使用时按照需求选取。
//创建外部类的代价太大。
public class ComparatorSample {
    public static void main(String[] args) {
        //ArrayList
        ArrayList arrayList = new ArrayList();
        arrayList.add(3);
        arrayList.add(2);
        arrayList.add(4);
        Collections.sort(arrayList,new cmp());
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("================");
        Collections.sort(arrayList,new cmp2());
        Iterator it2 = arrayList.iterator();
        while (it2.hasNext()){
            System.out.println(it2.next());
        }
    }
    //升序比较器
    public static class cmp implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1-o2;
        }
    }
    //降序比较器
    public static class cmp2 implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2-o1;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_36564655/article/details/81362684