If I implement a custom comparator is it considered good practice to overide equals besides compare?
Additionally is there a defined contract for a Comparator?
If I implement a custom comparator is it considered good practice to overide equals
Share
The contract of Comparator is defined in its javadoc. In particular:
Typically, if 2 objects are equal from an
equalsperspective but not from acompareToperspective, you can store both objects as keys in a TreeMap. This can lead to un-intuitive behaviour. It can also be done on purpose in specific situations.For example, this answer shows an example where it is desirable that equals and compareTo be inconsistent.