A class that overrides only one of equals and hashCode is likely to violate the contract of the hashCode method. The contract requires that hashCode gives the same integer result for any two equal objects. Not enforcing this property may cause unexpected results when storing and retrieving objects of such a class in a hashing data structure.

Usually, both methods should be overridden to ensure that they are consistent.

In the following example, the class InconsistentEqualsHashCode overrides hashCode but not equals.

In the following example, the class InconsistentEqualsHashCodeFix overrides both hashCode and equals.

  • J. Bloch, Effective Java (second edition), Item 9. Addison-Wesley, 2008.
  • Java API Documentation: Object.equals, Object.hashCode.
  • IBM developerWorks: Java theory and practice: Hashing it out.
  • Help - Eclipse Platform: Java Compiler Errors/Warnings Preferences.