
The Contract Between equals() and hashCode()
In Java, the equals() and hashCode() methods, both defined in the Object class, are fundamental for determining object equality. When you create custom classes, you often need to override equals() to define logical equality (based on object state) rather than reference equality (based on memory address).
However, thereโs a critical rule you must follow: If you override equals(), you MUST override hashCode().
This is because of the contract between them, which is essential for the correct functioning of hash-based collections like HashMap, HashSet, and Hashtable.
The contract states:
- If two objects are equal according to the
equals(Object)method, then calling thehashCode()method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal according to the
equals(Object)method, then calling thehashCode()method on each of the two objects must produce distinct integer results. However, producing distinct results for unequal objects may improve the performance of hash tables.
Why is this Contract Important?
Hash-based collections use the hashCode() method to determine where to store an object in memory (which โbucketโ to put it in). When you try to retrieve an object (e.g., with map.get(key) or set.contains(object)), the collection does the following:
- It calculates the hash code of the object youโre looking for.
- It uses this hash code to quickly find the bucket where the object should be.
- It then iterates through the (usually small) number of objects in that bucket, using the
equals()method to find the exact match.
What Happens if You Break the Contract?
Letโs say you have a User class and you override equals() but not hashCode().
class User {
private int id;
private String email;
// Constructor, getters...
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id && email.equals(user.email);
}
// Missing hashCode() override!
}
Now, letโs use this class in a HashSet:
User user1 = new User(1, "test@example.com");
User user2 = new User(1, "test@example.com");
System.out.println("user1.equals(user2): " + user1.equals(user2)); // true
Set<User> userSet = new HashSet<>();
userSet.add(user1);
System.out.println("userSet.contains(user2): " + userSet.contains(user2)); // false!
Why does contains() return false?
- When
userSet.add(user1)is called,HashSetcalculatesuser1.hashCode()(using the default implementation fromObject, which is based on memory address) and storesuser1in a bucket corresponding to that hash code. - When
userSet.contains(user2)is called,HashSetcalculatesuser2.hashCode(). Sinceuser1anduser2are different objects in memory, their default hash codes are different. HashSetlooks in the bucket foruser2โs hash code, which is a different bucket from whereuser1is stored. It doesnโt find anything, so it immediately returnsfalsewithout ever callingequals().
The collection fails to find an object that is logically equal because the broken contract led it to look in the wrong place.
How to Correctly Override hashCode()
To fix this, you must implement hashCode() so that it produces the same hash for objects that are considered equal. A good hashCode() implementation should use the same fields that are used in the equals() method.
Correct Implementation:
import java.util.Objects;
class User {
private int id;
private String email;
// Constructor, getters...
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id && Objects.equals(email, user.email);
}
@Override
public int hashCode() {
// Use Objects.hash() to easily generate a hash code from the fields.
return Objects.hash(id, email);
}
}
With this corrected hashCode(), user1.hashCode() and user2.hashCode() will be the same. The HashSet will now correctly find the bucket and then use equals() to confirm the match, returning true for contains().
Key Takeaway
Always override hashCode() when you override equals(). The easiest and safest way to do this is by using the java.util.Objects.hash() utility method, passing it the same fields you used in your equals() implementation.
Professional Depth Check
For Understanding equals() and hashCode() in Java, the practical standard is not whether the reader can repeat one instruction once. Treat the topic as a reproducible debugging procedure: verify JDK version, build tool configuration, classpath or module path, and runtime stack trace before drawing a conclusion. The result should be written as a small decision record, because future readers need to know which fact was observed, which assumption was used, and which condition would change the answer.
Evidence That Makes the Guidance Reliable
Use objective evidence before changing a workflow. Good evidence includes java -version, javac -version, Maven or Gradle output, and the smallest failing class. If two pieces of evidence conflict, keep the conflict visible instead of smoothing it over. For example, a successful quick fix is still weak evidence if the same input, account, dependency, or device state has not been tested again. A durable article should help the reader distinguish a confirmed fix from a plausible fix.
Review Table
| Review Item | What To Confirm | Why It Matters |
|---|---|---|
| Scope | The exact case covered by this article | Prevents over-applying the advice |
| Baseline | The state before any change | Makes rollback and comparison possible |
| Change | The smallest action taken | Reduces hidden side effects |
| Result | The observed output after the change | Separates evidence from expectation |
| Recheck | When to revisit the conclusion | Keeps the post accurate over time |
Edge Cases and Failure Modes
The main risks are fixing the symptom while leaving the root cause, and mixing unrelated changes into the same test. When the situation involves production data, personal information, money, health, legal rights, or security recovery, the conservative path is to stop and collect evidence before applying a broad fix. The same title can describe very different cases, so the reader should compare their environment with the assumptions in the post before copying commands or decisions.
Related Reading
Continue with these related posts from the same topic area.
Leave a comment