java 不好的做法 - 类定义了 compareTo(...) 并使用 Object.equals()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16701047/
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
Bad practice - Class defines compareTo(...) and uses Object.equals()
提问by Maulzey
Wondering what needs to be done for listed method
想知道列出的方法需要做什么
public final int compareTo(final FieldDTO o) {
return o.available.compareTo(this.available);
its throwing exception on line 2 stating
Bad practice - Class defines compareTo(...) and uses Object.equals() 16 days
field defines compareTo(FieldDTO) and uses Object.equals()
它在第 2 行抛出异常,说明错误做法 - 类定义了 compareTo(...) 并使用了 Object.equals() 16 天
字段定义了 compareTo(FieldDTO) 并使用了 Object.equals()
Not sure how should i handle this. Thanks in advance.
不知道我应该如何处理这个。提前致谢。
回答by OldCurmudgeon
If you define compareTo
you should at least define equals
如果你定义compareTo
你至少应该定义equals
boolean equals(it) {
return compareTo(it) == 0;
}
otherwise you will get strange problems when you put your object in Map
s and Set
s. It is generally good practice to also define hashCode
.
否则当你把你的对象放在Map
s 和Set
s时你会遇到奇怪的问题。通常还定义hashCode
.
回答by zw324
This is the documentation from FindBugs:
这是FindBugs的文档:
Eq: Class defines compareTo(...) and uses Object.equals() (EQ_COMPARETO_USE_OBJECT_EQUALS)
This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object. Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue. In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.
From the JavaDoc for the compareTo method in the Comparable interface:
It is strongly recommended, but not strictly required that (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. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
Eq:类定义 compareTo(...) 并使用 Object.equals() (EQ_COMPARETO_USE_OBJECT_EQUALS)
该类定义了一个 compareTo(...) 方法,但从 java.lang.Object 继承了它的 equals() 方法。通常,当且仅当等于返回真时,compareTo 的值才应返回零。如果违反了这一点,则在 PriorityQueue 等类中将发生奇怪且不可预测的故障。在 Java 5 中 PriorityQueue.remove 方法使用 compareTo 方法,而在 Java 6 中它使用 equals 方法。
来自 Comparable 接口中 compareTo 方法的 JavaDoc:
强烈推荐,但不严格要求 (x.compareTo(y)==0) == (x.equals(y))。一般而言,任何实现 Comparable 接口并违反此条件的类都应清楚表明这一事实。推荐的语言是“注意:这个类有一个与equals不一致的自然顺序。”
So it seems you need to implement the equals
method thus overriding the default implementation from Object
.
因此,您似乎需要实现该equals
方法,从而覆盖Object
.
回答by Raj Pandiri
you need to override Object class equals() and hashCode() methods. Use IDE generated code for these, it will pull all the Object attributes and creates method for you.
您需要覆盖 Object 类的 equals() 和 hashCode() 方法。使用 IDE 为这些生成的代码,它将拉取所有对象属性并为您创建方法。
On eclipse IDE:
在 Eclipse IDE 上:
- Right click on the class
- Select Source
- Generate hashCode() and equals()...
- 右键单击类
- 选择来源
- 生成 hashCode() 和 equals()...
回答by aran
Your class, as it seems, implements the Comparable
interface.
看起来,您的类实现了该Comparable
接口。
JavaDocs are strict here: if you are using your own compareTo()
method, it must return a 0 only if the equals()
method returns true.
JavaDocs 在这里很严格:如果您使用自己的compareTo()
方法,则仅当该equals()
方法返回true时才必须返回 0 。
So:
所以:
(x.compareTo==0) == (x.equals).