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 接口的對象來實現控制它的排序就好了。
java
// 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);。
數組