java.lang.Comparable 和 equals

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6970879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:05:30  来源:igfitidea点击:

java.lang.Comparable and equals

javaequalscomparable

提问by aps

If I implement java.lang.Comparablefor a class, do I still have to override the equals()method? Or will the Comparablework for equalsas well?

如果我java.lang.Comparable为一个类实现,我是否仍然需要覆盖该equals()方法?或者也可以Comparable工作equals

If the answer is no, then what if some discrepancy arises? Let's say the way I term two objects as equal within the equals()method is different from the way I term two objects of the same class as equal within the compareTo()of the Comparable.

如果答案是否定的,那么如果出现一些差异怎么办?假设我在equals()方法中将两个对象称为相等的方式与我compareTo()Comparable.

Moreover, if I implement Comparable, do I also have to override equals()?

此外,如果我实现了Comparable,我还必须覆盖equals()吗?

回答by dlev

While it is recommended (and pretty sensible) that having a.compareTo(b) == 0imply that a.equals(b)(and visa versa), it is notrequired. Comparableis intended to be used when performing an ordering on a series of objects, whereas equals()just tests for straight equality.

虽然建议(并且非常明智)a.compareTo(b) == 0暗示a.equals(b)(反之亦然),但这不是必需的。Comparable旨在在对一系列对象执行排序时使用,而equals()只是测试直接相等性。

This linkhas some good information on implementing compareToproperly.

这个链接有一些关于compareTo正确实施的好信息。

回答by Andrey Adamovich

From Javadoc of java.lang.Comparable:

来自java.lang.Comparable 的Javadoc :

It is strongly recommended (though not required) that natural orderings be consistent with equals.

强烈建议(虽然不是必需的)自然顺序与 equals 一致。

回答by gnomed

While it is recommended, it is not required that .equals()and .compareTo()have the same behaviour.

尽管建议,它不要求.equals().compareTo()具有相同的行为。

Just look at the BigDecimal API: http://download.oracle.com/javase/1,5.0/docs/api/java/math/BigDecimal.html#equals(java.lang.Object)

看看 BigDecimal API:http: //download.oracle.com/javase/1,5.0/docs/api/java/math/BigDecimal.html#equals(java.lang.Object)

回答by SharkAlley

Let's say the way I term two objects as equal within the equals() method is different from the way I term two objects of the same class as equal within the toCompare() of the Comparable?

假设我在 equals() 方法中将两个对象定义为相等的方式与我在 Comparable 的 toCompare() 中将同一类的两个对象定义为相等的方式不同?

If you do this, and you put those objects into a sorted set, the set will misbehave. From the docs on SortedSet:

如果您这样做,并将这些对象放入已排序的集合中,则该集合将行为不端。从 SortedSet 上的文档

Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface.

请注意,如果排序集要正确实现 Set 接口,则排序集维护的排序(无论是否提供显式比较器)必须与 equals 一致。

For example, a TreeSetmay (erroneously) contain two objects where

例如,一个TreeSet可能(错误地)包含两个对象,其中

a.compareTo(b) != 0

even though

虽然

a.equals(b) == true