How does HashMap work internally in Java 8+?
Short answer
HashMap is an array of buckets: hash the key into an index, chain same-index entries into a list, turn long lists into a tree. get/put are O(1) on average.
3 points to say:
- Index =
hash & (n - 1), n is a power of 2. - A bucket of 8+ nodes becomes a tree.
- Past 0.75 × capacity the table doubles.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.