美文网首页码农的世界
跟 JDK 学英语(6)- Comparable

跟 JDK 学英语(6)- Comparable

作者: 码语生活 | 来源:发表于2018-10-31 00:27 被阅读0次

    一、原文与翻译

    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.

    这个接口对实现它的每个类的对象强制执行总排序。这个排序称为类的自然排序,类的 compareTo 方法称为自然比较方法。

    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.

    实现了该接口的对象列表和数组可以用 Collections.sort 和 Arrays.sort 方法进行自动排序。实现了该接口的对象可被用作有序 map 的 key,或者作为有序 set 的元素,而不需要定义一个比较器。

    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 as e1.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 a NullPointerException even though e.equals(null) returns false.

    类 C 的自然排序仅在以下情况下被认为是与 equals 方法一致的:对每一个类 C 的元素 e1 和 e2,e1.compareTo(e2) == 0 与 e1.equals(e2) 有一样的布尔值。注意 null 不是任何类的实例,尽管 e.equals(null) 返回 false,e.compareTo(null) 应该抛出 NullPointerException 异常。

    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 equals method.

    强烈推荐(尽管不是必需的)自然顺序与 equals 保持一致。因为如果对象元素或 key 的自然顺序与 equals 不保持一致,没有显式的比较器的有序 set 和有序 map 会产生很奇怪的结果。而且,这样的有序 set 或有序 map 破坏了 set 或 map 定义在 equals 方法中的常规约定。

    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.

    例如,如果要将两个元素 a 和 b 加到一个没有使用显示比较器的有序 set 中,其中 !a.equals(b) && a.compareTo(b) == 0 成立,那么第二个添加操作会返回 false (有序 set 的元素个数没有增加),因为从有序 set 的角度看,a 和 b 是等价的。

    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 BigDecimal objects with equal values and different precisions (such as 4.0 and 4.00).

    几乎所有的实现了 Comparable 的 Java 核心类的自然排序是与 equals 一致的。有一个例外就是 java.math.BigDecimal,有相同值和不同精度(例如4.0和4.00)的 BigDecimal 对象在自然排序上是等价的。

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

    从数学角度来看,对给定的类 C 定义自然排序的关系是:对于(x,y)有x.compareTo(y)<=0。

    The quotient for this total order is:

    {(x, y) such that x.compareTo(y) == 0}.

    总顺序的商是:对于(x,y)有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 is consistent 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)}.
    从 CompareTo 的约定可以得出商是在 C 上的等价关系,而自然排序是在 C 上的总顺序。当我们说一个类的自然排序是与 equals 一致的,我们是说自然排序的商是定义于该类的 equals(Object) 方法的等价关系。

    This interface is a member of the Java Collections Framework.

    本接口是 Java 集合框架的一个成员。
    Since: 1.2
    自:1.2

    二、词汇学习

    impose: 强加,施加影响
    consistent: 一致的
    violate: 违反
    in terms of: 依据,在...方面
    perspective: 观点,远景
    precision: 精度
    incline: 倾斜,倾向
    quotient: 商(IQ、EQ里的Q)

    三、句子分析

    理解句子无非就是找出主谓语,然后拆分对主谓宾等的修饰语句,抓住主干,再一个个短语理解清楚意思即可。

    四、技术要点

    实现了 Comparable 接口的类,就有了内在的排序能力,其 compareTo 方法通过返回负数,0,正数这三个值,来决定同一个类的对象的先后自然顺序。

    微信公众号.jpg

    相关文章

      网友评论

        本文标题:跟 JDK 学英语(6)- Comparable

        本文链接:https://www.haomeiwen.com/subject/jkxotqtx.html