為何需要實現(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]