java Comparable與Comparator的區(qū)別

java中我們要實現(xiàn)集合元素的比較和排序有兩種方法。一種是讓集合中的元素去實現(xiàn)Comparable接口,另一種是則是創(chuàng)建一個Comparator比較器(創(chuàng)建比較器就是實現(xiàn)Comparator接口)。第一種我們是在集合內(nèi)部實現(xiàn)的,因為我們讓集合中的元素去實現(xiàn)Comparable接口,我們改變了元素的結(jié)構(gòu)。第二種,我們是在集合外部去實現(xiàn)的。

  • 實現(xiàn)Comparable接口,首先我們來看看Comparable接口有什么用,我們?yōu)槭裁匆崿F(xiàn)它,這里我覺得最好的說明就是jdk源碼中的這段注釋。

Compares this object with the specified object for order. Returns a
negative integer, zero, or a positive integer as this object is less
than, equal to, or greater than the specified object.

這段話的主要意思就是說,Comparable接口是為了將此元素與指定元素排序。將返回正數(shù),零,負(fù)數(shù),分別表示大于,等于,小于指定元素的含義。

下面就是jdk中Comparable接口的唯一一個方法。

     * @param   o the object to be compared.
     * @return  a negative integer, zero, or a positive integer as this object
     *          is less than, equal to, or greater than the specified object.
     *
     * @throws NullPointerException if the specified object is null
     * @throws ClassCastException if the specified object's type prevents it
     *         from being compared to this object.
     */
    public int compareTo(T o);

當(dāng)我們使用時只需要實現(xiàn)此接口,并實現(xiàn)CompareTo()方法。

class Student1 implements Comparable<Student1> {
    
    private int id;
    @Override
    public int compareTo(Student1 o) {
        
        
        return o.id>this.id?1:(o.id==this.id?0:-1);
    }
    
}

這里我采用了泛型的寫法,在java中使用泛型的好處,我就引用百度百科的這段話加以說明。

泛型是Java SE 1.5的新特性,泛型的本質(zhì)是參數(shù)化類型,也就是說所操作的數(shù)據(jù)類型被指定為一個參數(shù)。這種參數(shù)類型可以用在類、接口和方法的創(chuàng)建中,分別稱為泛型類、泛型接口、泛型方法。 Java語言引入泛型的好處是安全簡單。
在Java SE 1.5之前,沒有泛型的情況的下,通過對類型Object的引用來實現(xiàn)參數(shù)的“任意化”,“任意化”帶來的缺點是要做顯式的強(qiáng)制類型轉(zhuǎn)換,而這種轉(zhuǎn)換是要求開發(fā)者對實際參數(shù)類型可以預(yù)知的情況下進(jìn)行的。對于強(qiáng)制類型轉(zhuǎn)換錯誤的情況,編譯器可能不提示錯誤,在運行的時候才出現(xiàn)異常,這是一個安全隱患。
泛型的好處是在編譯的時候檢查類型安全,并且所有的強(qiáng)制轉(zhuǎn)換都是自動和隱式的,以提高代碼的重用率。

  • 使用Comparator比較器。其實就是去實現(xiàn)Comparator接口。jdk源碼中注釋如下。

Compares its two arguments for order. Returns a negative integer,
zero, or a positive integer as the first argument is less than, equal
to, or greater than the second.<p>

讓兩個元素有序,返回負(fù)整數(shù),零,正整數(shù)表示第一個參數(shù)小于,等于,大于第二個元素。這里說的兩個參數(shù),實際上是指Comparator接口中的這個方法的兩個參數(shù)。

       @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the
     *         first argument is less than, equal to, or greater than the
     *         second.
     * @throws NullPointerException if an argument is null and this
     *         comparator does not permit null arguments
     * @throws ClassCastException if the arguments' types prevent them from
     *         being compared by this comparator.
     */
    int compare(T o1, T o2);

代碼一:

class Student2 implements Comparator<Student2>{
    private int id;
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int compare(Student2 o1, Student2 o2) {
        
         return o1.id>o2.id?1:(o1.id==o2.id?0:-1);
    }
    
}

代碼二:

class MyComparator implements Comparator<Student2>{

    @Override
    public int compare(Student2 o1, Student2 o2) {
    
        return o1.getId()>o2.getId()?1:(o1.getId()==o2.getId()?0:-1);
    }
    
}

我們經(jīng)常認(rèn)為代碼一寫法和實現(xiàn)Comparable沒啥區(qū)別,但仔細(xì)看來還是有很大區(qū)別。首先我們代碼一中的方法是compare與compareTo()是不一樣,其次我們需要傳入兩個指定元素。最后雖然我們寫在Student2 的類中,但與Student2類沒有多大關(guān)系,我們還可以在方法中去比較Student1。
代碼三

class Student2 implements Comparator {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int compare(Object o1, Object o2) {
        if (o1 instanceof Student1 && o2 instanceof Student1) {
            return ((Student1) o1).getId() > ((Student1) o2).getId() ? 1
                    : (((Student1) o1).getId() == ((Student1) o2).getId() ? 0
                            : -1);
        } else if (o1 instanceof Student2 && o2 instanceof Student2) {
            return ((Student2) o1).getId() > ((Student2) o2).getId() ? 1
                    : (((Student2) o1).getId() == ((Student2) o2).getId() ? 0
                            : -1);
        } else
            return -1;

    }

}

其實Comparator 采用的是策略設(shè)計模式,即我會根據(jù)傳入對象的不同類型,來采用不同的比較策略。當(dāng)然我們加上泛型之后,也就是只為當(dāng)前元素服務(wù)了。所以如果你的比較器只是為一種元素做比較,你可以按照代碼一的寫法去寫,如果你想做一個通用的比較器,那就按照代碼二去寫,分離出來,讓你的程序的很有可讀性。第三種只是為了解決大家的疑惑,開發(fā)中應(yīng)該杜絕這種寫法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、基本數(shù)據(jù)類型 注釋 單行注釋:// 區(qū)域注釋:/* */ 文檔注釋:/** */ 數(shù)值 對于byte類型而言...
    龍貓小爺閱讀 4,450評論 0 16
  • 從三月份找實習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍(lán)閱讀 42,793評論 11 349
  • 對象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對象:僅僅是創(chuàng)建對象的方法,并非Fa...
    孫小磊閱讀 2,184評論 0 3
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,673評論 18 399
  • 慢慢的學(xué)著不在乎,你不是我的誰,我也不必要因時刻考慮你的感受,而活的提心吊膽。 慢慢的學(xué)著不在乎,你不是我的誰,沒...
    等待ye是一種幸福閱讀 878評論 0 0

友情鏈接更多精彩內(nèi)容