Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is the equals and hashCode Contract in Java?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
7 min read

The equals and hashCode contract in Java says that if two objects are equal according to equals, they must return the same hashCode. The reverse is not required: two unequal objects may share a hash code. Hash-based collections such as HashMap and HashSet rely on this rule, so breaking it produces lookups that fail even though the key is present.

Beyond that headline rule, equals itself must follow five properties. This guide lists them, shows how they are broken in practice, and gives a correct implementation.

Table of Contents

  • The rules of the equals and hashCode contract
  • Why the contract matters to HashMap and HashSet
  • A correct implementation
  • Inheritance and the symmetry trap
  • Mutable fields and hash codes
  • Records, arrays and floating point
  • Frequently Asked Questions
    • What happens if I override equals but not hashCode?
    • Can two different objects have the same hashCode?
    • Should hashCode use the same fields as equals?
    • Does a record satisfy the contract automatically?
  • Summary
  • Related Articles

The rules of the equals and hashCode contract

The Object documentation defines these requirements for equals(Object) on non-null references:

PropertyMeaning
Reflexivex.equals(x) is always true.
Symmetricx.equals(y) is true exactly when y.equals(x) is true.
TransitiveIf x.equals(y) and y.equals(z), then x.equals(z).
ConsistentRepeated calls return the same result while the compared fields do not change.
Non-nullx.equals(null) is always false and never throws.

And for hashCode():

  1. Repeated calls in one run return the same value as long as the fields used by equals do not change.
  2. If a.equals(b), then a.hashCode() == b.hashCode().
  3. Unequal objects are allowed to collide, but good hash codes spread them out to keep collections fast.

Why the contract matters to HashMap and HashSet

A HashMap finds a key in two steps: it uses hashCode() to pick a bucket, then equals() to find the matching key inside that bucket. If equal keys give different hash codes, the lookup goes to the wrong bucket and never reaches equals.

java
text

The objects are equal, but their identity-based hash codes differ, so the set looks in another bucket. (The first line is false in practice; with a rare hash collision it could be true, which is exactly why this bug is hard to spot.)

A correct implementation

java

Both methods use exactly the same fields. If equals compares cents and currency, hashCode must be derived from those and no others. Using fewer fields in hashCode is allowed (it only means more collisions); using more fields breaks the contract.

The class is final on purpose, which leads to the next problem.

Inheritance and the symmetry trap

Mixing instanceof with subclasses can break symmetry.

java

Here new Point(1, 2).equals(new ColorPoint(1, 2, "red")) is true, but the reverse is false. That violates symmetry. There is no way to add a value component in a subclass and keep equals correct, so the usual solutions are to make the class final, to compare with getClass() instead of instanceof (which treats subclasses as different), or to use composition instead of inheritance.

Mutable fields and hash codes

If a field used by hashCode changes while the object sits in a HashSet or as a HashMap key, the object is stuck in its old bucket and contains will fail.

java

Prefer immutable classes for map keys: String, the wrapper types, LocalDate and records all behave well.

Records, arrays and floating point

  • A record generates equals and hashCode from all components, following the contract.
  • Arrays use identity equality. Compare content with Arrays.equals and Arrays.hashCode, or Arrays.deepEquals for nested arrays.
  • For double and float fields, use Double.compare(a, b) == 0 and Double.hashCode(a), which is what records and Objects.hash do. Plain == treats NaN as unequal to itself and 0.0 as equal to -0.0.

Frequently Asked Questions

What happens if I override equals but not hashCode?

Equal objects will usually get different hash codes, so HashMap, HashSet and Hashtable fail to find them. Always override both or neither.

Can two different objects have the same hashCode?

Yes. That is a collision and it is allowed. The collection then uses equals to tell the objects apart, but many collisions slow lookups down.

Should hashCode use the same fields as equals?

It must use only fields that equals also uses. Using a subset is legal. Using a field that equals ignores can make equal objects hash differently.

Does a record satisfy the contract automatically?

Yes. Records generate consistent equals and hashCode from their components, which is why they are a good fit for keys and value objects.

Summary

The contract requires equals to be reflexive, symmetric, transitive, consistent and null-safe, and requires equal objects to share a hash code. Override the two methods together, derive the hash from the same fields, avoid mutable key fields, be careful with subclasses, and prefer records or final immutable classes for value objects.

Related Articles

  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Composition vs Inheritance in Java and Which Should You Prefer?
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • What Is Composition vs Inheritance in Java and Which Should You Prefer?
  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • What Is Polymorphism in Java? Compile-Time vs Runtime
  • What Is Inheritance in Java and How Does extends Work?
  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • What Are Access Modifiers in Java: public, private, protected, default?
  • What Is the static Keyword in Java? Variables, Methods, Blocks