简体   繁体   English

Comparator和Comparable的compare和compareTo方法的协定

[英]Contracts of the compare and compareTo method from Comparator and Comparable

When implementing the Comparator and Comparable interfaces, is it required to ensure that: 在实现ComparatorComparable接口时,是否需要确保:

If 如果

  • x.compare(Object obj1, Object obj2) == x.compare(Object obj3, Object obj2)

or 要么

  • obj1.compareTo(obj2) == obj3.compareTo(obj2) , obj1.compareTo(obj2) == obj3.compareTo(obj2)

then obj1.equals(obj3) must be true ? 那么obj1.equals(obj3)必须为true吗?

Not necessarily. 不必要。

What you're describing is the transitive relation between three given objects, and as far as both of the interfaces are concerned, the relationship while using them must be transitive between them. 您要描述的是三个给定对象之间的传递关系 两个接口而言,使用它们时的关系必须在它们之间传递。

This is to say, given three classes A , B , and C that are comparable amongst each other, if A.compareTo(B) == 0 && B.compareTo(C) == 0 , then A.compareTo(C) == 0 . 也就是说,给定三个彼此可比较的类ABC ,如果A.compareTo(B) == 0 && B.compareTo(C) == 0 ,则A.compareTo(C) == 0 The same principle applies for Comparator . 相同的原理适用于Comparator

What you're now conflating is the difference between compareTo and equals . 您现在要整理的是compareToequals之间的区别。 While these two are closely related, there isn't anything to enforce the very strong recommendation provided in the documentation: 虽然这两个是密切相关的,但没有任何内容可以强制执行文档中提供的强烈建议

It is strongly recommended, but not strictly required that ( x.compareTo(y)==0) == (x.equals(y) ). 强烈建议(但不严格要求( x.compareTo(y)==0) == (x.equals(y) )。 Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. 一般而言,任何实现Comparable接口且违反此条件的类都应明确指出这一事实。 The recommended language is "Note: this class has a natural ordering that is inconsistent with equals." 推荐的语言是“注意:此类的自然排序与等式不一致”。

It is certainly possible and valid to have an equals definition inconsistent with compareTo or even compare , but depending on the nature of the program, it can lead to obscure bugs. 具有与compareTo或甚至compare不一致的equals定义当然是可能且有效的,但是根据程序的性质,它可能导致模糊的错误。 If you find yourself in a situation in which you need to rely on both equals and compareTo (or compare ), document it and be consistent with its usage. 如果您发现自己需要同时依赖equalscompareTo (或compare ),请对其进行记录并与用法保持一致。

No, because the comparing of two objects only establishes that one is greater than, equal to, or less than the other. 否,因为两个对象的比较仅确定一个对象大于,等于或小于另一个对象。 Normally the result returned from a comparator is one of (-1,0,1). 通常,从比较器返回的结果是(-1,0,1)之一。 For example: 例如:

comparator.compare(1,5) == -1
comparator.compare(1,3) == -1
comparator.compare(3,1) == 1
comparator.compare(5,3) == 1
comparator.compare(3,3) == 0

This makes it much easier for developers to compare between objects. 这使开发人员可以更轻松地在对象之间进行比较。 If the contract required that the scale of the difference be returned, it would be very difficult to implement such a method for, say, strings. 如果合同要求退还差额,那么很难对字符串执行这种方法。 In order to obtain an exact value difference, the entire contents of both strings would have to be read, which could be very bad for performance, especially if each of these strings we very large. 为了获得精确的值差,必须读取两个字符串的全部内容,这对于性能可能非常不利,尤其是如果这些字符串中的每个字符串都很大时,尤其如此。 Because a determination only has to be made about which is larger than the other, normally only small portions of the strings need be compared to determine which is "larger" than the other. 因为仅需要确定哪个大于另一个,通常只需要比较字符串的一小部分即可确定哪个比另一个大。 The only time the entire string would have to be processed is if they were in fact equal. 唯一必须处理整个字符串的时间是它们实际上是否相等。 This performance issue is also the reason why hashes are preferred, when possible 此性能问题也是尽可能使用哈希的原因

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

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