Class defines compareTo(...) and uses Object.equals()同事没见过这个问题,不了解如何修改,于是在中午回基地吃饭的路上一起讨论这个问题。
This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object.Comparable的官方文档
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."
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |