Comparable與Comparator的區別

原文

Comparable & Comparator 都是用來實現集合中元素的比較、排序的,只是 Comparable 是在集合內部定義的方法實現的排序,Comparator 是在集合外部實現的排序,所以,如想實現排序,就需要在集合外定義 Comparator 接口的方法或在集合內實現 Comparable 接口的方法。

Comparator位於包java.util下,而Comparable位於包 java.lang下

Comparable 是一個對象本身就已經支持自比較所需要實現的接口(如 String、Integer 自己就可以完成比較大小操作,已經實現了Comparable接口)

自定義的類要在加入list容器中後能夠排序,可以實現Comparable接口,在用Collections類的sort方法排序時,如果不指定Comparator,那麼就以自然順序排序,如API所說:
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface
這裏的自然順序就是實現Comparable接口設定的排序方式。

而 Comparator 是一個專用的比較器,當這個對象不支持自比較或者自比較函數不能滿足你的要求時,你可以寫一個比較器來完成兩個對象之間大小的比較。

可以說一個是自已完成比較,一個是外部程序實現比較的差別而已。

用 Comparator 是策略模式(strategy design pattern),就是不改變對象自身,而用一個策略對象(strategy object)來改變它的行爲。

比如:你想對整數採用絕對值大小來排序,Integer 是不符合要求的,你不需要去修改 Integer 類(實際上你也不能這麼做)去改變它的排序行爲,只要使用一個實現了 Comparator 接口的對象來實現控制它的排序就行了。

// AbsComparator.java     
import java.util.*;

public class AbsComparator implements Comparator   {     
    public int compare(Object o1, Object o2) {     
      int v1 = Math.abs(((Integer)o1).intValue());     
      int v2 = Math.abs(((Integer)o2).intValue());     
      return v1 > v2 ? 1 : (v1 == v2 ? 0 : -1);     
    }     
}

可以用下面這個類測試 AbsComparator:

// Test.java     
import java.util.*;     

public class Test {     
    public static void main(String[] args) {     
      //產生一個20個隨機整數的數組(有正有負)     
      Random rnd = new Random();     
      Integer[] integers = new Integer[20];     
      for(int i = 0; i < integers.length; i++)     
      integers[i] = new Integer(rnd.nextInt(100) * (rnd.nextBoolean() ? 1 : -1));     

      System.out.println("用Integer內置方法排序:");     
      Arrays.sort(integers);     
      System.out.println(Arrays.asList(integers));     

      System.out.println("用AbsComparator排序:");     
      Arrays.sort(integers,new AbsComparator());     
      System.out.println(Arrays.asList(integers));     
    }     
}

Collections.sort((List<T> list, Comparator<? super T> c)是用來對list排序的。

如果不是調用sort方法,相要直接比較兩個對象的大小,如下:
Comparator定義了倆個方法,分別是 int compare(T o1, T o2)boolean equals(Object obj)
用於比較兩個Comparator是否相等
true only if the specified object is also a comparator and it imposes the same ordering as this comparator.
有時在實現Comparator接口時,並沒有實現equals方法,可程序並沒有報錯,原因是實現該接口的類也是Object類的子類,而Object類已經實現了equals方法

Comparable接口只提供了 int compareTo(T o)方法,也就是說假如我定義了一個Person類,這個類實現了Comparable接口,那麼當我實例化Person類的person1後,我想比較person1和一個現有的Person對象person2的大小時,我就可以這樣來調用:person1.comparTo(person2),通過返回值就可以判斷了;而此時如果你定義了一個 PersonComparator(實現了Comparator接口)的話,那你就可以這樣:

PersonComparator comparator = new PersonComparator();
comparator.compare(person1,person2);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章