此篇博客整理自網絡
Collections and Collection
Collection是一個集合接口,Collections是一個包裝類(或幫助類),這是二者最明顯的區別。Collections提供了一 些static方法來對 Collection對象進行處理,比如:對Collection對象的復制、添加元素、修改元素、對元素進行排序、交換倆個元素的位置、取 Collection的子集等等操作。另外Collection是j2sdk中集合框架的根接口,所有的其他特殊類型的結合接口或者類都直接或間接的實現 了這個接口。
java.util.Collections API >>
1 public interface Collection<E> extends Iterable<E> { 2 int size(); 3 boolean isEmpty(); 4 boolean contains(Object o); 5 Iterator<E> iterator(); 6 Object[] toArray(); 7 <T> T[] toArray(T[] a); 8 boolean add(E e); 9 boolean remove(Object o); 10 boolean containsAll(Collection<?> c); 11 boolean addAll(Collection<? extends E> c); 12 boolean removeAll(Collection<?> c); 13 boolean retainAll(Collection<?> c); 14 void clear(); 15 boolean equals(Object o); 16 int hashCode(); 17 }
java.util.Collections API >>
1 public class Collections { 2 //注意Collections只是Collection集合接口的一個幫助類,並沒有implements Collection 3 ... ... 4 }
Comparator and Comparable
Comparator和Comparable都是接口,倆者之間可以說沒什么關系。Comarator位於包java.util下,而Comparable位於包 java.lang下。通俗的說:Comparator是一個比較器,相當於一種工具,它定義了倆個方法,分別是 int compare(T o1, T o2)和 boolean (Object obj),所以你可以通過實現這個接口來定義你所有需要的比較器,如比較倆個人的大小,對象人有姓名、性別、年齡等屬性,你在定義這個比較器的時候就可以在compare方法中對傳入的倆個參數(person1,person2)的年齡進行大小的比較,從而確定這倆個人的大小。
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);。
java.lang.Comparable API >>
1 public interface Comparable<T> { 2 public int compareTo(T o); 3 }
此接口強行對實現它的每個類的對象進行整體排序。此排序被稱為該類的自然排序,類的 compareTo 方法被稱為它的自然比較方法。
實現此接口的對象列表(和數組)可以通過 Collections.sort(和 Arrays.sort)進行自動排序。實現此接口的對象可以用作有序映射表中的鍵或有序集
合中的元素,無需指定比較器。
java.util.Comparator API >>
1 public interface Comparator<T> { 2 int compare(T o1, T o2); 3 boolean equals(Object obj); 4 }
比較函數強行對某些對象 collection 進行整體排序。可以將 Comparator 傳遞給 sort 方法(如 Collections.sort),從而允許在排序順序上實現精確控制。還可以使用 Comparator 來控制某些數據結構(如 TreeSet 或 TreeMap)的順序。
當需要排序的集合或數組不是單純的數字類型的時候,通常可以使用Comparator或Comparable,以簡單的方式實現對象排序或自定義排序。
Comparator和Comparable的區別如下:
Comparable用在對象本身,說明這個對象是可以被比較的,也就是說可以被排序的。(String和Integer之所以可以比較大小,是因為它們都實現了Comparable接口,並實現了compareTo()方法)。
Comparator用在對象外面,相當於定義了一套排序算法來排序。
java.lang.String API >>
1 public final class String implements java.io.Serializable, Comparable<String>, CharSequence{
... ...
//String和Integer之所以可以比較大小,是因為它們都實現了Comparable接口,並實現了compareTo()方法 2 public int compareTo(String anotherString) { 3 int len1 = count; 4 int len2 = anotherString.count; 5 int n = Math.min(len1, len2); 6 char v1[] = value; 7 char v2[] = anotherString.value; 8 int i = offset; 9 int j = anotherString.offset; 10 11 if (i == j) { 12 int k = i; 13 int lim = n + i; 14 while (k < lim) { 15 char c1 = v1[k]; 16 char c2 = v2[k]; 17 if (c1 != c2) { 18 return c1 - c2; 19 } 20 k++; 21 } 22 } else { 23 while (n-- != 0) { 24 char c1 = v1[i++]; 25 char c2 = v2[j++]; 26 if (c1 != c2) { 27 return c1 - c2; 28 } 29 } 30 } 31 return len1 - len2; 32 } 33 }
下面通過具體的例子來理解Comparator和Comparable的區別:
Comparator實例如下:
1 class Name implements Comparable<Name>{ 2 public String firstName,lastName; 3 public Name(String firstName,String lastName){ 4 this.firstName=firstName; 5 this.lastName=lastName; 6 } 7 //實現接口 8 public int compareTo(Name o) { 9 int lastCmp=lastName.compareTo(o.lastName); 10 return (lastCmp!=0?lastCmp:firstName.compareTo(o.firstName)); 11 } 12 //便於輸出測試 13 public String toString(){ 14 return firstName+" "+lastName; 15 } 16 }
1 public class NameSort { 2 public static void main(String[] args) { 3 Name nameArray[] = { 4 new Name("John", "Lennon"), 5 new Name("Karl", "Marx"), 6 new Name("Groucho", "Marx"), 7 new Name("Oscar", "Grouch") 8 }; 9 Arrays.sort(nameArray); 10 System.out.println(Arrays.toString(nameArray));; 11 } 12 }
運行結果:[Oscar Grouch, John Lennon, Groucho Marx, Karl Marx]
Comparable實例如下:
1 public class User implements Comparable<Object> { 2 private String id; 3 private int age; 4 5 public User(String id, int age) { 6 this .id = id; 7 this .age = age; 8 } 9 10 public int getAge() { 11 return age; 12 } 13 public void setAge(int age) { 14 this .age = age; 15 } 16 public String getId() { 17 return id; 18 } 19 public void setId(String id) { 20 this .id = id; 21 } 22 23 public int compareTo(Object o) { 24 return this.age-((User)o).getAge(); 25 } 26 27 /** 28 * 測試方法 29 */ 30 public static void main(String[] args){ 31 User[] users = new User[]{new User("a", 30),new User("b", 20 )}; 32 Arrays.sort(users); 33 for (int i = 0 ; i < users.length; i++) { 34 User user = users[i]; 35 System.out.println(user.getId() + " " + user.getAge()); 36 } 37 } 38 }
運行結果:b 20
a 30