equals and hashCode
equals decides whether two objects mean the same thing. hashCode maps an object to an int for hash tables. If you override one, you override both, and they must agree.
Detailed Explanation
Object.equals is identity: this == other. That is wrong for a value type. Two Money objects with the same currency and amount should be equal, and they should work as HashMap keys. The contract is written in the Object javadoc and is not optional.
equals must be reflexive, symmetric, transitive, consistent, and false for null. Use instanceof (or a getClass check if you refuse subclasses) and compare the fields that define meaning. Records generate this for you. For arrays, equals is still identity; use Arrays.equals or Arrays.deepEquals.
hashCode must return the same int for two objects that are equal, every time you ask during one run. Unequal objects may share a hash (a collision). A hash that uses only some of the equality fields, or that changes when a field used in equals changes, breaks maps: you put the object in, mutate it, and get cannot find it.
HashMap and HashSet place an object in a bucket from hashCode, then use equals to find it. If the pair disagrees, the entry is lost. If hashCode is a constant, every entry lands in one bucket and operations become O(n). If you forget to override hashCode, the identity hash is used and two equal values are two keys.
You reach for a value-based pair on records, on types you put in sets or as map keys, and on types you compare in tests. You keep identity equals on entities with a lifecycle (a User row that is still the same user after a name change) and compare by id, or you do not put mutable entities in a HashSet at all.
The cost of a bad pair is a Heisenbug: tests pass because they use == or a list, and production maps misbehave. Lombok and records reduce boilerplate. Objects.hash and Objects.equals handle null fields. Do not include expensive or mutable derived fields. Once an object is in a hash collection, do not change fields that participate in equality.
Code Examples
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public final class Money {
private final String currency;
private final long minor;
public Money(String currency, long minor) {
this.currency = currency;
this.minor = minor;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Money m)) {
return false;
}
return minor == m.minor && Objects.equals(currency, m.currency);
}
@Override
public int hashCode() {
return Objects.hash(currency, minor);
}
public static void main(String[] args) {
Set<Money> set = new HashSet<>();
set.add(new Money("INR", 100));
System.out.println(set.contains(new Money("INR", 100))); // prints: true
}
}public record Point(int x, int y) {
public static void main(String[] args) {
System.out.println(new Point(1, 2).equals(new Point(1, 2))); // prints: true
System.out.println(new Point(1, 2).hashCode() == new Point(1, 2).hashCode());
// prints: true
}
}Common Gotchas
Overriding `equals` without `hashCode` breaks `HashMap`
equal keys land in different buckets and lookups fail.
Mutating a key after insert loses it
the bucket is chosen from the old hash. Use immutable keys.
`equals(MyType)` is an overload, not an override
`HashMap` still calls `equals(Object)`. Use `@Override`.
`getClass() != o.getClass()` refuses subclasses
`instanceof` allows them and can break symmetry if the subclass adds a field.