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)該杜絕這種寫法。