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方法進行排序,否則需要使用歸併排序。

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開發手冊(嵩山版)》最新發布,速速下載!

覺得不錯,別忘了隨手點贊+轉發哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章