What issues should be considered when overriding equals and hashCode in Java?
The theory (for the language lawyers and the mathematically inclined):
equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, andtransitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore, o.equals(null) must always return false.
hashCode() (javadoc) must also be consistent (if the object is not modified in terms of equals(), it must keep returning the same value).
The relation between the two methods is:
Whenever a.equals(b), then a.hashCode() _must be same as b.hashCode()_.
In practice:
If you override one, then you should override the other.
Use the same set of fields that you use to compute equals() to compute hashCode().
Use the excellent helper classes EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library.