‘compareTo()’ is incompatible with ‘equals()’
If a class implements Comparable, compareTo() method must return zero if and only if equals() returns true for the same non-null argument (and vice versa). Violating this rule may cause unexpected behavior of the objects.
class Product implements Comparable
...
public int compareTo(Object o) {
if (o == null)
throw new NullPointerException();
if (this.equals(o))
return 0;
...
}
}
See also:
The best bongs money can buy!…
[...]‘compareTo()’ is incompatible with ‘equals()’ « Java AntiPatterns[...]…
The best bongs money can buy!
February 16, 2012 at 2:29 am