简体   繁体   English

覆盖compareTo有什么含义?

[英]What are the implications for overriding compareTo?

I am aware that if one overrides equals, hashCode should also be overridden. 我知道,如果一个覆盖等于,则hashCode也应该被覆盖。 Are there any similar rules that would apply to overriding compareTo? 是否有类似的规则适用于覆盖compareTo?

This is a Java question. 这是一个Java问题。

The expectations of it can be read here: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html 可以在这里阅读对它的期望: http : //docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

The part that will be of the most interest to you is probably: 您最感兴趣的部分可能是:

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方法定义的集合(或图)的一般约定。

It is explained in the JavaDocs : JavaDocs中对此进行了解释:

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 一类的自然排序C被认为是与equals一致当且仅当e1.compareTo(e2) == 0具有相同的布尔值e1.equals(e2)对每个e1e2级的C

Note that it is not required , ie If two classes are equal according to the compareTo() , they don't have to hold equals() . 请注意,这不是必需的 ,即,如果根据compareTo()两个类相等,则它们不必保留equals() This is fine because you can for instance sort people by age, so two people with the same age are considered equal with regards to Comparator<Person> , but they obviously don't have to be equal. 这很好,因为您可以例如按年龄对人进行排序,因此在Comparator<Person>方面,两个具有相同年龄的人被认为是相等的,但是显然他们不必相等。

However in this particular case you might want to add secondary attributes to comparator if ages are equal (so sorting is always stable and predictable across same-aged people) so after all including the same attributes in compareTo() might be a good idea in some cases. 但是,在这种特殊情况下,如果年龄相等(您在同龄人中排序总是稳定且可预测的),则可能需要向比较器添加辅助属性,因此在某些情况下毕竟在compareTo()包含相同属性可能是个好主意案件。

The documentation for Comparator has this cautionary note: Comparator文档具有以下警告说明:

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 in S. 当且仅当c.compare(e1,e2)== 0的布尔值与e1.equals(e2)的布尔值相等时,比较器c才对元素S的集合排序等于equals。和e2在S中。

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." 假设带有显式比较器c的排序集(或排序图)与从集合S提取的元素(或键)一起使用。如果c对S施加的排序与等式不一致,则排序集(或排序图)将表现“奇怪”。 In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals. 特别是,排序后的集合(或排序后的映射)将违反集合(或映射)的一般契约,后者是用等值定义的。

我只想告诉您,对象中应具有特定的属性或属性,用于比较两个相同类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM