Loading published curriculum…

04 · Java language in depth / LESSON 13 OF 102

equals, hashCode, and collection identity

Hash collections need stable agreement about which objects count as equal.

Before this lesson: Generics, bounds, and wildcards

Understand the topic

If a.equals(b), both must have the same hashCode. Unequal objects may share a hash; the collection then checks equality. Overriding only equals can leave equal objects in different buckets and make lookups fail.

Do not mutate fields used for hashing after insertion into a HashSet or HashMap. Separate entity identity from mutable descriptions. Records generate value-based equals and hashCode for their components, which is useful for immutable keys.

EXAMPLE · JAVA

Main.java

import java.util.HashSet;
public class Main {
  record TaskKey(long ownerId, long taskId) {}
  public static void main(String[] args) {
    var keys = new HashSet<TaskKey>();
    keys.add(new TaskKey(7, 12));
    keys.add(new TaskKey(7, 12));
    System.out.println(keys.size());
  }
}

What to expect

Prints 1 because the two record values are equal.

YOUR TURN

Put it into practice

  1. 01Model a composite key for an owner and task.
  2. 02Compare equal, different-owner, and different-task instances in a HashSet.
  3. 03Verify: Equal keys deduplicate and changing a task display title cannot change key identity.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Hash collections need stable agreement about which objects count as equal.

  1. 01If a.equals(b), both must have the same hashCode. Unequal objects may share a hash; the collection then checks equality. Overriding only equals can leave equal objects in different buckets and make lookups fail.
  2. 02Do not mutate fields used for hashing after insertion into a HashSet or HashMap. Separate entity identity from mutable descriptions. Records generate value-based equals and hashCode for their components, which is useful for immutable keys.
  3. 03Trace the example: Prints 1 because the two record values are equal.

A mistake worth avoiding

contains returns false after editing an object already in a HashSet.

DEBUGGING ROOM

Something broke. Where do you look?

contains returns false after editing an object already in a HashSet.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Must unequal objects have different hash codes?

Choose one answer

BUILD IT YOURSELF

Model a composite key for an owner and task.

  1. 1.Model a composite key for an owner and task.
  2. 2.Compare equal, different-owner, and different-task instances in a HashSet.
  3. 3.Reproduce this failure, then repair it: contains returns false after editing an object already in a HashSet.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Must unequal objects have different hash codes?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Equal keys deduplicate and changing a task display title cannot change key identity.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references