Medium👤 3-5 years👤 8-15 years 1 min read

Walk me through what happens inside HashMap.put() in Java 8.

Asked inAmazonMicrosoftDeloitte
#hashmap#internals#treeify#hashing#buckets
Report issue

⚡ Short Answer

put() hashes the key (with a spread to mix high bits), finds the bucket via (n-1)&hash, then appends/updates in that bucket. Buckets are linked lists that convert to red-black trees once they exceed 8 entries (and the table ≥ 64), giving O(log n) worst case instead of O(n).

Coffee Chat Question

Concept Made Simple

Walk me through what happens inside HashMap.put() in Java 8.

🧠Mind Map Answer

Remember It Faster

Java 8 HashMap = array of buckets. Each bucket is a linked list that treeifies to a balanced tree under heavy collision, so a hot bucket degrades to O(log n), not O(n).

hash(key)h ^ (h >>> 16) — spreads high bits
bucket(n - 1) & hash
Treeifybucket > 8 entries & table ≥ 64
Untreeifyshrinks back below 6

🔥What If?

Think Beyond the Expected

Why does HashMap mix the high bits of the hash before indexing?

The bucket index is (n-1) & hash, which only uses the low bits. Many hashCodes differ mainly in high bits, so without spreading (h ^ (h>>>16)) they'd collide in the same bucket. Mixing reduces collisions cheaply.

😂Real World

Understanding treeify explains why a HashMap keyed by objects with a poor hashCode (all colliding) is 'slow' — pre-Java 8 it was O(n) per bucket; Java 8 caps it at O(log n) via trees.

🎯Interviewer's Expectation

Keywords they're listening for:

spread function(n-1)&hash indexingtreeify threshold 8 / table 64O(log n) worst caseneeds equals + hashCode

⚠️Common Mistakes

  • Thinking collisions are always O(n) in Java 8
  • Using mutable objects as keys
  • Ignoring the equals/hashCode contract

Best Practices

  • Use immutable keys with well-distributed hashCode
  • Pre-size the map when the count is known
  • Prefer records/value objects as keys

🔁Follow-up Questions

  • 1What triggers a resize and how does it rehash?
  • 2Why must keys be effectively immutable?
  • 3Why is the default capacity 16 and load factor 0.75?

🧩Related Technologies

red-black treeObjects.hashrecords

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: HashMap Internals (Java Collections)
Interview question: "Walk me through what happens inside HashMap.put() in Java 8."

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions