HashMap groups keys by hash and distinguishes collisions using equality. Poor distribution increases lookup work and memory behavior matters under load.
Before you start
You should know Java classes, methods, collections and exceptions. Trace object identity separately from the contents of an object. For concurrent examples, state which thread owns or shares the data; a single successful execution is not proof that every interleaving is safe.
The practical goal is to reason through this situation: Keys with identical hash codes can still coexist if equals distinguishes them. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Understand bucket selection
Hashing narrows candidates, while equality distinguishes keys.
Step 2: Check key stability
Changing hash-relevant fields after insertion invalidates lookup assumptions.
Step 3: Measure actual distribution
Investigate collisions, load and access patterns before changing structures.
Worked scenario
Keys with identical hash codes can still coexist if equals distinguishes them.
Two keys returning the same hash can store separate values when equals distinguishes them. Retrieval must examine the relevant candidates rather than treating the hash as a unique identifier. A deliberately constant hash illustrates degraded distribution without violating equality by itself.
Common mistake
A unique hash code is not required, and collisions do not mean keys are equal.
Verify the behavior
Test distinct colliding keys and equal keys; profile representative access rather than one lookup.
Interview exercise
Investigate slow lookups.
Answer and reasoning
Check key mutation, distribution, load and actual access patterns before changing collections or inventing a new hash function.
Continue learning
Compare the scenario with the Java interview questions and test your understanding with the Java MCQs. For terminology and implementation details, consult the reference material.