程序员英语-Comparable类解读

acad2018 2023-04-14 15:44:11

内容头部广告位(手机)

virtually adv. 事实上,几乎;实质上

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.

Comparable 接口对实现它的每个类的对象强制执行总排序。这种排序被称为是类的自然排序,其中,类方法 compareTo() 是用来执行自然比较的方法。

实现该接口的对象的列表(和数组集)可以通过 Collections.sort(和Arrays.sort) 方法自动排序。实现该接口的对象可以用作有序 map 中的键或有序 set 中的元素,而不需要指定 具体的

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

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.

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.

对于类C的每一个对象e1,e2,当且仅当e1. compareTo (e2) == 0具有与e1.equals(e2)相同的布尔值时,类C的自然顺序才可认为与equals 保持一致。需要注意的是,null不是任何类的实例,并且 e.compareTo (null)应该抛出一个NullPointerException,即使e.equals (null)返回false。

强烈建议(尽管不是必需的)自然排序与 equals 保持一致。这是因为没有指定显式比较器的有序 set(和有序 map)在使用自然排序与 equals 不一致的元素(或键)时会表现得很“奇怪”(奇怪表

现在,如两个对象 equals 相同,但是排序比较却不同)。特别是,这样的排序集(或排序映射)违反了set(或map)的通用契约,而该契约是用 equals 方法定义的。

例如,当添加两个键a和b (!a.equals(b) && a.compareTo(b) == 0)到一个没有指定显式比较器的有序set时,第二个 add 操作将返回 false (有序 set 的大小也不会增加),这是因为从有序set 的角度来看,a 和b 是等价的。

Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals. One exception isjava.math.BigDecimal, whose natural ordering equates BigDecimal objects 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}.

几乎所有实现 Comparable 的 Java 核心类都具有与 equals 一致的自然排序。

java.math.BigDecimal 是一个例外,其自然排序等同于具有相同的值和不同的精度(如4.0和4.00)的BigDecimal对象。

从数学的角度来看,对于一个给定的类 C,它的自然排序可以用以下数学关系定义:

{(x, y) such that x.compareTo(y) <= 0}.The quotient forthis 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 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)}.

This interface is a member of the Java Collections Framework.

从 compareTo 的方法定义中可以直接得出,商(quotient)在C上是等价关系,自然排序是C上的总顺序。当我们说一个类的自然顺序与 equals 保持一致时,我们的意思是自然顺序的商是由类的equals(Object)方法定义的等价关系:

{(x, y) such that x.equals(y)}.

Comparable 接口是 Java 集合框架中的一员。
内容底部广告位(手机)
上一篇:移民日本孩子就不能学英语了吗 日本国际学校帮你完美解决!
下一篇:空中守护,线上育苗——博野县南邑中学网课教研活动系列(初中英语)
相关文章
留言与评论(共有 0 条评论)
   
验证码:
返回顶部小火箭