Say I have class A
, which override the default equals
method and implement Comparable
interface (Comparable
interface is said to be the natural ordering). To conform to good practice, the result returned by equals
and Comparable's compareTo
method should be consistent.
On some occasions, I want to compare instance of class A
differently, so I implement Comparator
interface (Comparator
interface is said to be the unnatural ordering). So using Comparable's compareTo
method and Comparator's compare
method, the result would not be consistent.
If equals
method and Comparable's compareTo
interface is consistent, Comparable's comparaTo
method and Comparator's compare
method is not consistent, then equals
method and Comparator's compare
method would not be consistent as well.
So what are the consequences of equals
method not consistent with Comparator's compare
method, if there is any?