How does HashMap work internally in Java?
Reported in Just Eat Takeaway.com European engineering loops. Java-specific collections question on buckets, hashing, and resize behavior.
Interview scenario
Often asked in Just Eat Takeaway.com loops at European offices (London, Berlin, Amsterdam, Paris, Stockholm, Dublin, and remote EU). Prepare a clear spoken answer plus key trade-offs.
Model answer
Try answering aloud first
Cover trade-offs, structure, and a concrete example before revealing the baseline response.
How to frame this at Just Eat Takeaway.com: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.
HashMap stores key-value entries in an array of buckets. Index = (n - 1) & hash(key) after spreading high bits to reduce collisions. Collisions chain in linked lists; JDK 8+ converts long chains to balanced trees (≥8 nodes) for O(log n) worst case.
Default load factor 0.75 triggers resize (double capacity) when size exceeds threshold. Resize rehashes all entries—expensive; size buckets appropriately upfront.
equals and hashCode contract: equal keys must share hash codes. Mutable keys are dangerous—changing hash breaks lookup.
Not thread-safe—use ConcurrentHashMap for concurrency (segmented/CAS locks). Contrast with HashTable (legacy synchronized) and LinkedHashMap (insertion/access order).
Discussion
Comments (0)
Share how this question came up in your loop, or add tips for others preparing.
Log in to comment on this question.