Java 中的 Comparable 接口

為何需要實現(xiàn)Comparable接口

我們知道Collections類中包含很多對實現(xiàn)Collection接口的容器各種操作的靜態(tài)方法.
當(dāng)然, 其中最長用的莫過于排序了(Collections.sort(List l).
下面是1個簡單例子

public class Compare1{
    public static void f(){
        ArrayList arr = new ArrayList();
        arr.add(10);
        arr.add(23);
        arr.add(7);

        System.out.println(arr);

        Collections.sort(arr);
        
        System.out.println(arr);
    } 
}

邏輯很簡單, 就是在1個list容器中添加3個int數(shù)值(注意實際被自動裝箱成Interger對象).
正常輸出容器元素一次, 利用Collections.sort()方法排序后, 再輸出1次.

[java] [10, 23, 7]  
[java] [7, 10, 23]  

但是當(dāng)List容器添加的元素對象是屬于自己寫的類時, 就可能出問題了.

例子:

import java.util.ArrayList;
import java.util.Collections;

class Student{
    private String name;
    private int ranking;

    public Student(String name, int ranking){
        this.name = name;
        this.ranking = ranking;
    } 

    public String toString(){
        return this.name + ":" + this.ranking;
    }
}

public class Compare2{
    public static void f(){
        ArrayList arr = new ArrayList();
        arr.add(new Student("Jack",10));
        arr.add(new Student("Bill",23));
        arr.add(new Student("Rudy",7));

        System.out.println(arr);
    } 
}

上面定義了1個Student類, 它只有兩個成員, 名字和排名.
在f()方法內(nèi), 添加3個Student的對象到1個list容器中, 然后輸出

 [Jack:10, Bill:23, Rudy:7]  

到此為止, 是沒有問題的. 但是當(dāng)對這個容器進(jìn)行排序時就有問題了.

例如將上面的f()方法改成:

public class Compare2{
    public static void f(){
        ArrayList arr = new ArrayList();
        arr.add(new Student("Jack",10));
        arr.add(new Student("Bill",23));
        arr.add(new Student("Rudy",7));

        System.out.println(arr);
        Collections.sort(arr);
        System.out.println(arr);
    } 
}

編譯時就會出錯:

提示這個類Student沒有實現(xiàn)Comparable接口.
原因也很簡單, 因為Java不知道應(yīng)該怎樣為Student對象排序, 是應(yīng)該按名字排序? 還是按ranking來排序?

為什么本文第1個例子就排序成功? 是因為Java本身提供的類Integer已經(jīng)實現(xiàn)了Comparable接口. 也表明Integer這個類的對象是可以比較的.

而Student類的對象默認(rèn)是不可以比較的. 除非它實現(xiàn)了Comparable接口.

總而言之, 如果你想1個類的對象支持比較(排序), 就必須實現(xiàn)Comparable接口.

Comparable接口簡介.

Comparable 接口內(nèi)部只有1個要重寫的關(guān)鍵的方法.

就是

int compareTo(T o)

這個方法返回1個Int數(shù)值,

例如 i = x.compareTo(y)

如果i=0, 也表明對象x與y排位上是相等的

如果返回數(shù)值i>0 則意味者, x > y啦,

反之若i<0則 意味x < y

Comparable接口的實現(xiàn)及用法.

用回上面的例子, 我們修改Student類, 令其實現(xiàn)Comparable接口并重寫compareTo方法.

import java.util.ArrayList;
import java.util.Collections;

class Student implements Comparable{
    private String name;
    private int ranking;

    public Student(String name, int ranking){
        this.name = name;
        this.ranking = ranking;
    } 

    public String toString(){
        return this.name + ":" + this.ranking;
    }

    public int compareTo(Object o){
        Student s = (Student)(o);
        return this.ranking - s.ranking;
    }
}

public class Compare2{
    public static void f(){
        ArrayList arr = new ArrayList();
        arr.add(new Student("Jack",10));
        arr.add(new Student("Bill",23));
        arr.add(new Student("Rudy",7));

        System.out.println(arr);
        Collections.sort(arr);
        System.out.println(arr);
    } 
}

注意重寫的compareTo(Object o)方法內(nèi). 根據(jù)Student的ranking成員來比較的, 也就是說跟姓名無關(guān)了.

這時再編譯執(zhí)行, 就能見到List容器內(nèi)的Student對象已經(jīng)根據(jù)ranking來排序了.

輸出:

[java] [Jack:10, Bill:23, Rudy:7]
[java] [Rudy:7, Jack:10, Bill:23]
?著作權(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)容

  • 為何需要實現(xiàn)Comparable接口 我們知道Collections類中包含很多對實現(xiàn)Collection接口的容...
    __豆約翰__閱讀 816評論 0 4
  • 最近Algorithms 4 課上提到了排序。趁著這個機會,梳理一下。 1. 介紹 Comparable<T>接口...
    深海__閱讀 767評論 0 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,706評論 18 399
  • 從三月份找實習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍(lán)閱讀 42,803評論 11 349
  • 知道你要走 我在七寶路頭 抬頭看天 看掠過頭頂?shù)娘w機 猜測著你會在哪一架中 你是否也會向下俯視 回味著 昨日留下的腳??!
    一個小瘋子閱讀 235評論 4 2

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