← Back to Questions
Java

Why should hashCode() and equals() be overridden together?

Learn Why should hashCode() and equals() be overridden together? with simple explanations, real-time examples, interview tips and practical use cases.

hashCode() and equals() should be overridden together because Java collections like HashMap, HashSet, Hashtable, and ConcurrentHashMap internally depend on both methods to correctly store, search, and compare objects.

In simple words:

If equals() says two objects are equal, their hashCode() values must also be equal.


Why This Rule Exists?

Java hash-based collections first use hashCode() to find bucket location and then use equals() to verify exact object match.


Core Internal Flow


Object Inserted into HashMap

      |
      v

hashCode() Called

      |
      v

Bucket Selected

      |
      v

equals() Checks Exact Match


What Happens if Only equals() is Overridden?

Collections may store duplicate logically equal objects.


Example Without Proper hashCode()

class Employee {

    int id;

    Employee(int id) {

        this.id = id;

    }

    @Override
    public boolean equals(
        Object obj
    ) {

        Employee e =
            (Employee)obj;

        return this.id == e.id;

    }

}

Test Example

Employee e1 =
    new Employee(101);

Employee e2 =
    new Employee(101);

HashSet set =
    new HashSet<>();

set.add(e1);

set.add(e2);

System.out.println(
    set.size()
);

Expected Output

1

Actual Output

2

Why Does This Happen?

Because:

  • equals() says objects are equal
  • But default hashCode() generates different values
  • Objects go into different buckets

Problem Flow Diagram


e1.hashCode() ---> Bucket 1

e2.hashCode() ---> Bucket 5

Different Buckets

      |
      v

equals() Never Called

      |
      v

Duplicate Objects Stored


Correct Solution

Override both equals() and hashCode().


Correct equals() and hashCode()

class Employee {

    int id;

    Employee(int id) {

        this.id = id;

    }

    @Override
    public boolean equals(
        Object obj
    ) {

        Employee e =
            (Employee)obj;

        return this.id == e.id;

    }

    @Override
    public int hashCode() {

        return id;

    }

}

Now What Happens?

  • Both objects generate same hashCode
  • Both go into same bucket
  • equals() checks logical equality
  • Duplicate prevented

Correct Flow Diagram


e1.hashCode() ---> Bucket 3

e2.hashCode() ---> Bucket 3

Same Bucket

      |
      v

equals() Called

      |
      v

Objects Found Equal

      |
      v

Duplicate Prevented


Java Contract Between equals() and hashCode()

Java defines strict rules:


Rule 1

If two objects are equal using equals(), their hashCode() values MUST be same.


Rule 2

If two objects have same hashCode(), they may or may not be equal.


Rule 3

If equals() is overridden, hashCode() should also be overridden.


Contract Diagram


equals() = true

      |
      v

hashCode() MUST be same


Why Hash-Based Collections Need hashCode()?

Searching every object one-by-one would be slow.


Hashing Improves Performance


hashCode()

      |
      v

Direct Bucket Access

      |
      v

Fast Lookup


HashMap Internal Architecture


HashMap

      |
      +-------> Bucket 1

      |
      +-------> Bucket 2

      |
      +-------> Bucket 3


How HashMap Uses Both Methods?

Method Purpose
hashCode() Find Bucket
equals() Find Exact Match

Real String Example

String s1 = "Java";

String s2 = "Java";

Results

s1.equals(s2)
-> true

s1.hashCode()
==
s2.hashCode()

Why?

String class correctly overrides both methods.


String Processing Flow


String Content Compared

      |
      v

equals() Returns true

      |
      v

Same hashCode() Returned


What Happens if hashCode() is Same but equals() is False?

This situation is allowed.


This is Called

Hash Collision

Collision Example


Different Objects

      |
      v

Same hashCode()

      |
      v

equals() Returns false


Why Java Allows Collisions?

Because generating unique hashCode for every object is impossible.


equals() and hashCode() in Banking Systems

Banking applications use these methods for:

  • Transaction deduplication
  • Account comparison
  • Distributed caching
  • Fraud detection
  • Session management

Banking Example

class Account {

    Long accountId;

}

Why Important?

Multiple objects representing same account should behave as equal.


Banking Flow


Transaction Loaded

      |
      v

hashCode() Finds Bucket

      |
      v

equals() Verifies Same Account

      |
      v

Duplicate Transaction Prevented


equals() and hashCode() in E-Commerce Systems

E-commerce platforms use them for:

  • Shopping cart comparison
  • Order deduplication
  • Inventory caching
  • User session tracking

equals() and hashCode() in Spring Boot

Spring Boot applications heavily use these methods in:

  • JPA entities
  • Hibernate caching
  • DTO comparisons
  • Security contexts
  • Distributed cache systems

Spring JPA Example

@Entity
class User {

    Long id;

}

Why Critical?

Hibernate collections and cache systems depend on proper equality logic.


Hibernate Flow


Entity Loaded

      |
      v

hashCode() Finds Cache Bucket

      |
      v

equals() Verifies Entity Equality


equals() and hashCode() in Microservices

Microservices architectures use these methods for:

  • Distributed caching
  • Kafka event deduplication
  • DTO validation
  • API response comparison
  • Session synchronization

Microservice Flow


Event Received

      |
      v

hashCode() Finds Bucket

      |
      v

equals() Checks Duplicate Event

      |
      v

Duplicate Event Ignored


Difference Between equals(), hashCode(), and ==

Feature equals() hashCode() ==
Purpose Logical Equality Hash Generation Reference Comparison
Return Type boolean int boolean
Used In Object Matching Hash Collections Reference Checking

Advantages of Overriding Both Methods Properly

  • Correct HashMap behavior
  • Correct HashSet behavior
  • Fast searching
  • Duplicate prevention
  • Reliable caching
  • Framework compatibility

Problems if Not Overridden Properly

  • Duplicate entries
  • Cache inconsistencies
  • Collection bugs
  • Incorrect comparisons
  • Performance issues

Common Interview Mistake

Many developers think equals() alone is enough.

Actually:

  • Hash-based collections require both methods.

Another Common Mistake

Many developers think same hashCode means objects are equal.

Actually:

  • Same hashCode does not guarantee equality.

Best Practices

  • Always override equals() and hashCode() together
  • Use immutable fields for hashCode()
  • Prefer Objects.hash()
  • Keep equality logic consistent
  • Avoid mutable fields in hashCode calculation

Realtime Enterprise Example

Distributed Payment Processing System


Payment Event Received

      |
      v

hashCode() Locates Cache Bucket

      |
      v

equals() Detects Duplicate Transaction

      |
      v

Duplicate Payment Prevented


Related Learning Topics


Professional Interview Answer

hashCode() and equals() should be overridden together because Java hash-based collections such as HashMap, HashSet, Hashtable, and ConcurrentHashMap internally rely on both methods for correct object storage, searching, caching, and duplicate prevention. The hashCode() method is used to locate the correct bucket in hash-based collections, while equals() is used to verify logical equality between objects within the same bucket. Java defines an important contract stating that if two objects are equal according to equals(), they must return the same hashCode(). If equals() is overridden without overriding hashCode(), logically equal objects may generate different hash values and get stored in different buckets, causing duplicate entries, lookup failures, cache inconsistencies, and collection-related bugs. Enterprise applications, Spring Boot systems, banking platforms, Hibernate ORM frameworks, distributed caching systems, Kafka event processors, and cloud-native microservices heavily depend on properly implemented equals() and hashCode() methods for high-performance lookups, entity comparison, transaction deduplication, distributed synchronization, and reliable caching behavior.


Frequently Asked Questions

Why should equals() and hashCode() be overridden together?

Because hash-based collections depend on both methods for correct behavior.

What happens if only equals() is overridden?

Duplicate objects may be stored in HashMap or HashSet.

Can two unequal objects have same hashCode?

Yes, this is called hash collision.

Which collections use hashCode() heavily?

HashMap, HashSet, Hashtable, and ConcurrentHashMap.

What is the Java contract for equals() and hashCode()?

If equals() returns true, both objects must have same hashCode().

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.