Comparable and Comparator API
【0】README
0.1)文本全文翻譯于 java 8 JDK 官方文檔中關于 Comparable and Comparator 的描述;
0.2)能力有限,僅供參考;
【1】 Interface Comparable
1.0)類型參數: 與本對象進行比較的另一個對象的類型
1.1)這個接口對實現它的類對象進行整體排序。排序被稱為自然排序,類方法 compareTo 被稱為 自然比較方法;
1.2)實現了該接口的對象列表List 能夠通過 Colletions.sort( 或者 Arrays.sort) 自動排序。實現該接口的 Object 可以作為 SortedMap 中的 鍵值或者 SortSet中的元素, 不需要指定比較器 Comparator;
1.3)要知道, 對類C的自然排序等同于 equals 方法, 也就是說, e1.compareTo(e2) ==0 與 e1.equals(e2) 有同樣的結果。 注意到, null 不是 任何對象的實例, 所以 e.compareTo(null)
應該拋出 空指針異常, 即使 e.equals(null) 返回 false;
1.4)強烈推薦(即使不是硬性要求):自然排序應該與 equals 方法一致。 這樣做的原因是,沒有附帶顯式比較器 Comparator 的 SortedSet 和 SortedMap 執行效果很奇怪;特別地,這樣一種
SortSet 或者 SortedMap 違背了 set 或者 map 的 由equals 方法定義的常規約定;
1.5)舉個荔枝,向沒有附帶顯式比較器Comparator的SortSet添加兩個元素使得 (!a.equals(b) && a.compareTo(b) == 0),那么 第二個add 操作失敗(), 因為a 和 b 等同于 SortedSet的
觀點。
1.6)事實上, 所有實現了 Comparable 接口的java核心類 的 自然排序都和 equals 方法保持一致。其中一個例外是 java.math.BigDecimal, 它的自然排序是值相等,但精度不同
(如4.0 和 4.00);
1.7)從數學觀點看, 對給定類C定義自然排序的聯系是: {(x,y) 使得 x.compareTo(y) <= 0} , x 在 y 之前;
1.8)商(quotient )意味著 {(x,y) 使得 x.compareTo(y)}
1.9) int compareTo(T o) 方法
- 強烈建議使用: (x.compareTo(y)==0) == (x.equals(y))
【2】Interface Comparator
2.0)T類型參數T:此比較器比較的對象類型
2.1)對一些集合對象施加整體排序的比較函數。傳遞 比較器Comparator(下同) 給 一個排序方法(如 Collections.sort 或者 Arrays.sort 方法)以允許對排序序列進行精確控制。也可以使用比較器控制某個數據結構的排序(如SortedSet 或者 SortedMap), 或者為那些沒有自然排序的對象集合提供一個排序;
2.2)比較器C 施加給 元素集合S 的排序 被認為是和 equals 方法保持一致的。 當且僅當 c.compare(e1, e2) == 0 與 e1.equlas(e2) 返回同樣的 boolean 結果;
2.3)要注意的是,使用的是 一個 對 equals 方法 不一致,但卻對 集合 施加排序的這么一個比較器。 后面的敘述和 Comparable 差不多一樣;……
注意: 一個好的做法是:比較器也實現了 java.io.Serializable 接口, 因為它們可能被用在 serializable 可序列化數據結構中(如 TreeSet 和 TreeMap)。要對某些數據結構序列化成功的話,比較器必須實現 Serializable (序列化接口);
【3】initial desc in API
public interface Comparable<T> 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. The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value ase1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw aNullPointerException even though e.equals(null) returns false. It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equalsmethod.For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals. One exception is java.math.BigDecimal, whose natural ordering equates BigDecimalobjects with equal values and different precisions (such as 4.0 and 4.00).For the mathematically inclined, the relation that defines the natural ordering on a given class C is:{(x, y) such that x.compareTo(y) <= 0}.The quotient for this total order is:{(x, y) such that x.compareTo(y) == 0}.It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering isconsistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method:{(x, y) such that x.equals(y)}. This interface is a member of the Java Collections Framework.@FunctionalInterface public interface Comparator<T> 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 anatural ordering. The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 inS. Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals. For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add method. Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). In order for the data structure to serialize successfully, the comparator (if provided) must implement Serializable....... 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Comparable and Comparator API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java集合——树集(TreeSet)+
- 下一篇: 黎明个人资料 黎明个人资料有什么