1.Comparable簡介java
此接口對實現它的每一個類的對象強加一個總排序。這種排序被稱爲類的天然排序,類的compareTo方法被稱爲其天然比較方法。能夠經過數組
Collections.sort(和Arrays.sort)自動對實現此接口的對象的列表(和數組)進行排序。實現此接口的對象可用做有序映射中的鍵或有序數據結構
集中的元素,而無需指定比較器。ide
注:若一個類實現了該接口,說明該類自己是支持排序的。在java中倡導全部實現Comparable接口的類都應該保持與函數
equals()一致的排序順序,所以還須要重寫equals方法和hashcode方法。this
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method. Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.spa
2.Comparable定義code
實現Comparable接口僅需實現compareTo方法。對象
經過x.compareTo(y)來比較x與y的大小:1)返回負數,說明x小於y;2)返回0,說明x與y相等;3)返回正數,說明x大於y。blog
1 package java.lang; 2 import java.util.*; 3 4 public interface Comparable<T> { 5 public int compareTo(T o); 6 }
3.Comparator簡介
該接口內部是一個比較函數,它對某些對象集合施加總排序。能夠將比較器傳遞給排序方法(例如Collections.sort或Arrays.sort),以便
精確控制排序順序。比較器還可用於控制某些數據結構的順序(例若有序集或有序映射),或者爲不具備天然順序的對象集合提供排序。
注:若一個類沒有實現Comparable接口,則該類自身沒法排序;此時能夠使用Comparator幫助這個類進行排序。
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.
4. Comparator定義
1 package java.util; 2 3 public interface Comparator<T> { 4 5 int compare(T o1, T o2); 6 7 boolean equals(Object obj); 8 }
5. 示例
5.1 Customer.java
該類實現了Comparable接口,即該類的成員對象自身是支持排序的。
1 import java.util.Objects; 2 3 public class Customer implements Comparable<Customer>{ 4 5 private int customerId; 6 private String customerName; 7 8 public Customer(Integer customerId, String customerName) { 9 this.customerId = customerId; 10 this.customerName = customerName; 11 } 12 13 public int getCustomerId() { 14 return customerId; 15 } 16 public String getCustomerName() { 17 return customerName; 18 } 19 20 @Override 21 public String toString() { 22 return "Customer:[Id=" + customerId + ", Name=" + customerName + "]"; 23 } 24 25 /* 26 * 重寫compareTo方法 27 * 按Id或者name排序 28 * 能夠對總體添加負號決定升降序 29 * */ 30 @Override 31 public int compareTo(Customer o) { 32 // return this.customerId - o.customerId; 33 return this.customerName.compareTo(o.customerName); 34 } 35 36 /* 37 * 重寫equals和hashcode方法 38 * 這裏id和name相同則爲同一對象 39 * */ 40 @Override 41 public boolean equals(Object o) { 42 if (this == o) return true; 43 if (!(o instanceof Customer)) return false; 44 Customer customer = (Customer) o; 45 return customerId == customer.customerId && 46 Objects.equals(customerName, customer.customerName); 47 } 48 49 @Override 50 public int hashCode() { 51 return Objects.hash(customerId, customerName); 52 } 53 54 }
5.2 CustomerComparator.java
該類實現了Comparator接口,幫助Customer對象按Id排序。
1 import java.util.Comparator; 2 3 public class CustomerComparator implements Comparator<Customer> { 4 5 @Override 6 public int compare(Customer c1, Customer c2) { 7 // 按Id排序 8 return c1.getCustomerId() - c2.getCustomerId(); 9 } 10 }
5.3 SortFunc.java
使用兩種接口的排序方法對list進行排序。
1 import java.util.*; 2 3 public class SortFunc { 4 public static void main(String[] args){ 5 List<Customer> list = new ArrayList<>(); 6 list.add(new Customer(1, "A")); 7 list.add(new Customer(2, "C")); 8 list.add(new Customer(3, "D")); 9 list.add(new Customer(4, "B")); 10 11 // 原始排序 12 System.out.println("原始的排序:"+list); 13 14 // 使用compare接口按name排序 15 Collections.sort(list); 16 System.out.println("使用compare接口按name排序:"+list); 17 18 // 使用comparator接口按id排序 19 // Collections.sort(list, new CustomerComparator()); // 兩個方式 20 list.sort(new CustomerComparator()); 21 System.out.println("使用comparator接口按id排序:"+list); 22 23 // 判斷c1和c2是否引用同一個對象;判斷c1和c2是否等價 24 Customer c1 = new Customer(6,"c1"); 25 Customer c2 = new Customer(6,"c1"); 26 System.out.println(c1==c2); 27 System.out.println(c1.equals(c2)); 28 } 29 }
顯示結果以下所示,附帶==與equals的說明:
對於引用類型,== 判斷兩個變量是否引用同一個對象,而 equals() 判斷引用的對象是否等價,即數據是否一致。
1 原始的排序:[Customer:[Id=1, Name=A], Customer:[Id=2, Name=C], Customer:[Id=3, Name=D], Customer:[Id=4, Name=B]] 2 使用compare接口按name排序:[Customer:[Id=1, Name=A], Customer:[Id=4, Name=B], Customer:[Id=2, Name=C], Customer:[Id=3, Name=D]] 3 使用comparator接口按id排序:[Customer:[Id=1, Name=A], Customer:[Id=2, Name=C], Customer:[Id=3, Name=D], Customer:[Id=4, Name=B]] 4 false 5 true
!!!