【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.......