Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
Chaturmind
← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
HomeLearnJavaJava Core FundamentalsExceptions & Best Practices
✓ FreeIntermediate· 10 min read

equals() and hashCode()

The contract between equals and hashCode — the most commonly broken Java rule.

Published January 20, 2025


equals() and hashCode()

This is one of the most commonly broken rules in Java, and one of the most commonly asked interview questions.

The Contract

Java's specification requires:

  1. If a.equals(b) is true, then a.hashCode() == b.hashCode() must be true
  2. The reverse is NOT required — two objects can have the same hashCode and not be equal (hash collision)

Why does it matter?

HashMap and HashSet rely on this contract:

  1. First compare hashCode() — O(1) bucket lookup
  2. If hashes match, compare with equals() for actual equality

If you override equals() but not hashCode(), your objects break in HashMap:

public class User {
    String email;

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof User u)) return false;
        return Objects.equals(email, u.email);
    }
    // ❌ No hashCode override!
}

Set<User> users = new HashSet<>();
users.add(new User("alice@example.com"));
users.contains(new User("alice@example.com")); // ❌ returns false!

The correct implementation

public class User {
    String email;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User u)) return false;
        return Objects.equals(email, u.email);
    }

    @Override
    public int hashCode() {
        return Objects.hash(email); // use the same fields as equals
    }
}

Records handle this automatically

Java 16+ Records generate correct equals() and hashCode() based on all record components:

record User(String email, String name) {} // equals/hashCode generated correctly

Interview Tip

"You must always override hashCode() when you override equals(). The rule is: if two objects are equal, they must produce the same hash code. Use Objects.hash() with the same fields you use in equals()."

Previous

Exception Handling

Next

String Manipulation

AI Tutor

Lesson: equals() and hashCode()

Quick actions

AI responses can be inaccurate. Verify critical information.