新接觸java沒(méi)多久,前幾天用到排序的問(wèn)題,看到Comparator和Comparable兩個(gè)接口類有點(diǎn)迷惑,研究了兩天也沒(méi)理解有什么區(qū)別,今天在看《Java核心編程》時(shí),才恍然大悟。在這里表達(dá)一下自己的想法。
當(dāng)需要排序的集合或數(shù)組時(shí)可以使用Comparator或Comparable,它們都可以實(shí)現(xiàn)排序,但是它們的區(qū)別是Comparator從外部定義了對(duì)象的比較規(guī)則,而Comparable則是從內(nèi)部定義了對(duì)象是可比較的。下面將詳細(xì)解這句話。
一、 Comparator
Comparator從外部定義了對(duì)象的比較規(guī)則
比如,你要使用某人寫(xiě)的一個(gè)矩形類Rect。現(xiàn)在你有一個(gè)Rect的集合(或數(shù)組),你想實(shí)現(xiàn)對(duì)Rect的排序,現(xiàn)在有一個(gè)問(wèn)題,某人在實(shí)現(xiàn)Rect的時(shí)候沒(méi)有考慮到會(huì)有人將會(huì)比較Rect對(duì)象。這個(gè)時(shí)候你必須根據(jù)需要對(duì)Rect進(jìn)行排序(比如,根據(jù)矩形的長(zhǎng)進(jìn)行排序),在這個(gè)場(chǎng)景下使用Comparator,因?yàn)镽ect類已經(jīng)存在,你不能對(duì)其進(jìn)行改變。
import java.util.*;
public class Rectangle {
public static void main(String[] args)
{
Rect[] rectArrays = new Rect[] {new Rect(3, 4), new Rect(5, 2), new Rect(4, 5)};
// 排序,將定義的RectComparator作為參數(shù)
Arrays.sort(rectArrays, new RectComparator());
for (int i=0; i != rectArrays.length; ++i)
System.out.println(rectArrays[i]);
}
// 定義一個(gè)Rect比較方式:根據(jù)Rect的長(zhǎng)比較
public static class RectComparator implements Comparator<Rect>
{
public int compare(Rect o1, Rect o2)
{
return o1.getLength() - o2.getLength();
}
}
public static class Rect
{
Rect(int l, int w)
{
this.length = l;
this.width = w;
}
public int getLength()
{
return this.length;
}
public int getWidth()
{
return this.width;
}
public int getArea()
{
return this.length * this.width;
}
public String toString()
{
return "length: " + length + " width: " + width;
}
private int length;
private int width;
}
}
輸出:
length: 3 width: 4
length: 4 width: 5
length: 5 width: 2
二、 Comparable
Comparable則是從內(nèi)部定義了對(duì)象的是可比較的
還是以Rect為例,假如你是Rect的實(shí)現(xiàn)者,在你定義Rect時(shí),你覺(jué)得有必要定義一個(gè)比較方式,這個(gè)時(shí)候就應(yīng)該使Rect繼承Comparable接口。如果你覺(jué)得較合理的排序方式是根據(jù)Rect的面積進(jìn)行排序,那么可以這樣實(shí)現(xiàn)
import java.util.*;
public class Rectangle {
public static void main(String[] args)
{
Rect[] rectArrays = new Rect[] {new Rect(3, 4), new Rect(5, 2), new Rect(4, 5)};
Arrays.sort(rectArrays);
for (int i=0; i != rectArrays.length; ++i)
System.out.println(rectArrays[i]);
}
// 定義了Comparable接口
public static class Rect implements Comparable<Rect>
{
Rect(int l, int w)
{
this.length = l;
this.width = w;
}
public int getLength()
{
return this.length;
}
public int getWidth()
{
return this.width;
}
public int getArea()
{
return this.length * this.width;
}
public String toString()
{
return "length: " + length + " width: " + width;
}
// 重載compareTo函數(shù),按面積比較
@Override
public int compareTo(Rect that)
{
return this.getArea() - that.getArea();
}
private int length;
private int width;
}
}
輸出:
length: 5 width: 2
length: 3 width: 4
length: 4 width: 5
三、總結(jié)
通過(guò)Comparator和Comparable的意思我們也可以看出兩者的區(qū)別
Comparable意為“可比較的”,一個(gè)類繼承了Camparable接口則表明這個(gè)類的對(duì)象之間是可以相互比較的,這個(gè)類對(duì)象組成的集合就可以直接使用sort方法排序。
Comparator意為“比較算子”,因此Comparator可以看成一種算法的實(shí)現(xiàn),將算法和數(shù)據(jù)分離。
另外,通過(guò)定義方式,我們可以發(fā)現(xiàn)如果一個(gè)類繼承了Comparable接口,則表明這個(gè)類的對(duì)象之間是可以比較的,且比較的方式只有一種。但是Comparator可以定義多種比較方式。在第二個(gè)程序中,Rect定義了按面積進(jìn)行比較,如果我們想按長(zhǎng)對(duì)Rect進(jìn)行排序,那么也可以通過(guò)Comparator來(lái)實(shí)現(xiàn)。
最后,再次強(qiáng)調(diào)
Comparator從外部定義了對(duì)象的比較規(guī)則,而Comparable則是從內(nèi)部定義了對(duì)象是可比較的。
參考資料
http://www.blogjava.net/fastunit/archive/2008/04/08/191533.html
《Java核心編程》第7版,p91-p92