关于java:Comparable-和-Comparator-接口的区别还傻傻分不清

4次阅读

共计 6898 个字符,预计需要花费 18 分钟才能阅读完成。

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 办法进行排序,否则须要应用归并排序。

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 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0