Java   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Comparable 和 Comparator 接口的区别?还傻傻分不清?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Comparable 简介

Comparable 是排序接口。

若一个类实现了Comparable接口,就意味着“该类支持排序”。此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。接口中通过x.compareTo(y)来比较x和y的大小。若返回负数,意味着x比y小;返回零,意味着x等于y;返回正数,意味着x大于y。

Comparator 简介

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

int compare(T o1, T o2)和上面的x.compareTo(y)类似,定义排序规则后返回正数,零和负数分别代表大于,等于和小于。

两者的联系

Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。

代码实现

package com.github.compare;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @                           _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *@DESCRIPTION Comparable是排序接口;若一个类实现了Comparable接口,就意味着“该类支持排序”。
 *             Comparable相当于“内部比较器”
 *@AUTHOR SongHongWei
 *@PACKAGE_NAME com.github.compare
 **/
public class ComparableAndCompartor
{
    public static void main(String[] args)
    {
        List<House> houses = new ArrayList();
        House h1 = new House(95.0, 12000);
        House h2 = new House(110.0, 12160);
        House h3 = new House(80.0, 16300);
        House h4 = new House(150.3, 10690);
        houses.add(h1);
        houses.add(h2);
        houses.add(h3);
        houses.add(h4);
        comparable(houses);
        comparator(houses);
    }

    /**
     *@DESCRIPTION House类实现类Comparable接口, 并重写了compareTo方法, 所以执行Collections.sort方法时会去调用我们重写的compareTo方法
     *@AUTHOR SongHongWei
     *@TIME 2018/12/14-16:46
     *@CLASS_NAME ComparableAndCompartor
     **/
    private static void comparable(List houses)
    {

        System.out.printf("未排序前的顺序,%s\n", houses);
        Collections.sort(houses);
        System.out.printf("按面积大小排序后的顺序,%s\n", houses);
    }

    private static void comparator(List houses)
    {

        System.out.printf("未排序前的顺序,%s\n", houses);
        Collections.sort(houses, new ComparatorDetail());
        System.out.printf("按单价大小排序后的顺序,%s\n", houses);
    }

    /**
     *@DESCRIPTION 实现Compatator接口, 并重写compare方法, 根据单价倒序排序
     *@AUTHOR SongHongWei
     *@TIME 2018/12/14-16:49
     *@CLASS_NAME ComparableAndCompartor
     **/
    static class ComparatorDetail implements Comparator<House>
    {

        @Override
        public int compare(House o1, House o2)
        {
            if (o1.price < o2.pricE)
                return 1;
            else if (o1.price > o2.pricE)
                return -1;
            return 0;
        }
    }
}
package com.github.compare;

/**
 * @                           _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *@DESCRIPTION 一个房子对象, 有面积和单价两个属性
 *@AUTHOR SongHongWei
 *@PACKAGE_NAME com.github.compare
 **/
public class House implements Comparable<House>
{
    /*房子的面积*/
    protected double proportion;

    /*房子每平米的售价*/
    protected double price;

    public House(double proportion, double pricE)
    {
        this.proportion = proportion;
        this.price = price;
    }

    /**
     *@DESCRIPTION 重写compareTo方法, 利用房子的面积来进行大小比较
     *@AUTHOR SongHongWei
     *@TIME 2018/12/14-16:18
     *@CLASS_NAME House
     **/
    @Override
    public int compareTo(House o)
    {
        /*当前对象的面积大,返回正数*/
        if (this.proportion > o.proportion)
            return 1;
            /*当前面积小,返回负数*/
        else if (this.proportion < o.proportion)
            return -1;
        /*相等返回0*/
        return 0;
    }

    @Override
    public String toString()
    {
        return "面积为" + proportion + "\t价格为" + price;
    }
}

附注

Collection与Collections的区别

Collection是集合类的上级接口,继承与他有关的接口主要有List和Set

Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全等操作

public static void main(String args[]) {   
       //注意List是实现Collection接口的   
       List list = new ArrayList();   
       double arraY[] = { 112, 111, 23, 456, 231 };   
       for (int i = 0; i < array.length; i++) {   
           list.add(new Double(arraY[i]));   
       }   
       Collections.sort(list);   //把list按从小到大排序
       for (int i = 0; i < array.length; i++) {   
           System.out.println(list.get(i));   
       }   
       // 结果:23.0 111.0 112.0 231.0 456.0   
 }   

Collections如何调用重写的compareTo方法的

集合框架中,Collections工具类支持两种排序方法:

Collections.sort(List<T> list); 
Collections.sort(List<T> list, Comparator<? super T> C)

如果待排序的列表中是数字或者字符,可以直接使用Collections.sort(list);当需要排序的集合或数组不是单纯的数字型时,需要自己定义排序规则,实现一个Comparator比较器。

Collections调用Collections.sort(list)方法,方法传递一个List集合,这里要求,List泛型里面装的元素必须实现Compareable接口此外,列表中的所有元素都必须是可相互比较的(也就是说,对于列表中的任何 e1 和 e2 元素,e1.compareTo(e2) 不得抛出 ClassCastException)。

Java源码里是这样写的

All elements in the list must implement the {@link ComparablE}interface.Furthermore, all elements in the list must be <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a {@code ClassCastException} for any elements

Collections.sort源码

 public static <T extends Comparable<? super T>> void sort(List<T> list) {
        Object[] a = list.toArray();
        Arrays.sort(a);
        ListIterator<T> i = list.listIterator();
        for (int j=0; j<a.length; j++) {
            i.next();
            i.set((T)a[j]);
        }
    }

由源码可以看出来,sort内部调用了Arrays.sort的方法,继续向下看

Arrays.sort源码

public static void sort(Object[] a) {
        if (LegacymergeSort.userrequested)
            legacymergeSort(a);
        else
            ComparableTimSort.sort(a);
    }

源码里首先判断是否采用传统的排序方法,LegacymergeSort.userrequested属性默认为false,也就是说默认选中 ComparableTimSort.sort(a)方法(传统归并排序在1.5及之前是默认排序方法,1.5之后默认执行ComparableTimSort.sort()方法。除非程序中强制要求使用传统归并排序,语句如下:System.setProperty("java.util.ArrayS.USELEgacymergeSort", "true"))

继续看 ComparableTimSort.sort(a)源码

ComparableTimSort.sort(a)源码

static void sort(Object[] a) {
          sort(a, 0, a.length);
    }

    static void sort(Object[] a, int lo, int hi) {
        rangecheck(a.length, lo, hi);
        int nRemaining  = hi - lo;
        if (nRemaining < 2)
            return;  // Arrays of size 0 and 1 are always sorted

        // If array is small, do a "mini-TimSort" with no merges
        if (nRemaining < MIN_MERGE) {
            int initRunLen = countRunAndMakeAscending(a, lo, hi);
            binarySort(a, lo, hi, lo + initRunLen);
            return;
        }

        /**
         * March over the array once, left to right, finding natural runs,
         * extending short natural runs to minRun elements, and merging runs
         * to maintain stack invariant.
         */
        ComparableTimSort ts = new ComparableTimSort(a);
        int minRun = minRunLength(nRemaining);
        do {
            // Identify next run
            int runLen = countRunAndMakeAscending(a, lo, hi);
            
            // If run is short, extend to min(minRun, nRemaining)
            if (runLen < minRun) {
                int force = nRemaining <= minRun ? nRemaining : minRun;
                binarySort(a, lo, lo + force, lo + runLen);
                runLen = force;
            }

            // Push run onto pending-run stack, and maybe merge
            ts.pushRun(lo, runLen);
            ts.mergeCollapse();
            
            // Advance to find next run
            lo += runLen;
            nRemaining -= runLen;
        } while (nRemaining != 0);

        // Merge all remaining runs to complete sort
        assert lo == hi;
        ts.mergeForceCollapse();
        assert ts.stackSize == 1;
    }

nRemaining表示没有排序的对象个数,方法执行前,如果这个数小于2,就不需要排序了。

如果2<= nRemaining <=32,即MIN_MERGE的初始值,表示需要排序的数组是小数组,可以使用mini-TimSort方法进行排序,否则需要使用归并排序。

@H_359_4@mini-TimSort排序方法:先找出数组中从下标为0开始的第一个升序序列,或者找出降序序列后转换为升序重新放入数组,将这段升序数组作为初始数组,将之后的每一个元素通过二分法排序插入到初始数组中。注意,这里就调用到了我们重写的compareTo()方法了。

  private static int countRunAndMakeAscending(Object[] a, int lo, int hi) {
        assert lo < hi;
        int runHi = lo + 1;
        if (runHi == hi)
            return 1;

        // Find end of run, and reverse range if Descending
        if (((ComparablE) a[runHi++]).compareTo(a[lo]) < 0) { // Descending
            while (runHi < hi && ((ComparablE) a[runHi]).compareTo(a[runHi - 1]) < 0)
                runHi++;
            reverseRange(a, lo, runHi);
        } else {                              // Ascending
            while (runHi < hi && ((ComparablE) a[runHi]).compareTo(a[runHi - 1]) >= 0)
                runHi++;
        }

        return runHi - lo;
    }

来源:blog.csdn.net/u010859650**/article/details/85009595

近期热文推荐:

1.1,000+ 道 Java面试题及答案整理(2021最新版)

2.劲爆!Java 协程要来了。。。

3.玩大了!Log4j 2.x 再爆雷。。。

4.Spring Boot 2.6 正式发布,一大波新特性。。

5.《Java开发手册(嵩山版)》最新发布,速速下载!

觉得不错,别忘了随手点赞+转发哦!

大佬总结

以上是大佬教程为你收集整理的Comparable 和 Comparator 接口的区别?还傻傻分不清?全部内容,希望文章能够帮你解决Comparable 和 Comparator 接口的区别?还傻傻分不清?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: