The equals and hashCode contract in Java says that if two objects are equal according to equals, they must return the same hashCode. The reverse is not required: two unequal objects may share a hash code. Hash-based collections such as HashMap and HashSet rely on this rule, so breaking it produces lookups that fail even though the key is present.
Beyond that headline rule, equals itself must follow five properties. This guide lists them, shows how they are broken in practice, and gives a correct implementation.
The Object documentation defines these requirements for equals(Object) on non-null references:
| Property | Meaning |
|---|---|
| Reflexive | x.equals(x) is always true. |
| Symmetric | x.equals(y) is true exactly when y.equals(x) is true. |
| Transitive | If x.equals(y) and y.equals(z), then x.equals(z). |
| Consistent | Repeated calls return the same result while the compared fields do not change. |
| Non-null | x.equals(null) is always false and never throws. |
And for hashCode():
equals do not change.a.equals(b), then a.hashCode() == b.hashCode().A HashMap finds a key in two steps: it uses hashCode() to pick a bucket, then equals() to find the matching key inside that bucket. If equal keys give different hash codes, the lookup goes to the wrong bucket and never reaches equals.
The objects are equal, but their identity-based hash codes differ, so the set looks in another bucket. (The first line is false in practice; with a rare hash collision it could be true, which is exactly why this bug is hard to spot.)
Both methods use exactly the same fields. If equals compares cents and currency, hashCode must be derived from those and no others. Using fewer fields in hashCode is allowed (it only means more collisions); using more fields breaks the contract.
The class is final on purpose, which leads to the next problem.
Mixing instanceof with subclasses can break symmetry.
Here new Point(1, 2).equals(new ColorPoint(1, 2, "red")) is true, but the reverse is false. That violates symmetry. There is no way to add a value component in a subclass and keep equals correct, so the usual solutions are to make the class final, to compare with getClass() instead of instanceof (which treats subclasses as different), or to use composition instead of inheritance.
If a field used by hashCode changes while the object sits in a HashSet or as a HashMap key, the object is stuck in its old bucket and contains will fail.
Prefer immutable classes for map keys: String, the wrapper types, LocalDate and records all behave well.
record generates equals and hashCode from all components, following the contract.Arrays.equals and Arrays.hashCode, or Arrays.deepEquals for nested arrays.double and float fields, use Double.compare(a, b) == 0 and Double.hashCode(a), which is what records and Objects.hash do. Plain == treats NaN as unequal to itself and 0.0 as equal to -0.0.Equal objects will usually get different hash codes, so HashMap, HashSet and Hashtable fail to find them. Always override both or neither.
Yes. That is a collision and it is allowed. The collection then uses equals to tell the objects apart, but many collisions slow lookups down.
It must use only fields that equals also uses. Using a subset is legal. Using a field that equals ignores can make equal objects hash differently.
Yes. Records generate consistent equals and hashCode from their components, which is why they are a good fit for keys and value objects.
The contract requires equals to be reflexive, symmetric, transitive, consistent and null-safe, and requires equal objects to share a hash code. Override the two methods together, derive the hash from the same fields, avoid mutable key fields, be careful with subclasses, and prefer records or final immutable classes for value objects.