If two objects are equal according to `equals()`, they must have the same `hashCode()`. Hash-based collections (HashMap/HashSet) rely on it; breaking the contract leads to “missing” entries and hard-to-debug behavior.
final class User {
private final String email;
User(String email) {
this.email = email;
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
return email.equals(((User) o).email);
}
@Override public int hashCode() {
return email.hashCode();
}
}