SteelReed

Search

Search posts, questions…

Collections & DSAQuestion 10 / 25

Middle

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.

hash & (n - 1)≥ 8 nodes, table ≥ 64size 0.75 × capacityput('cat', 1)bucket 5list: cat → dogred-black treedouble the table

3 points to say:

  1. Index = hash & (n - 1), n is a power of 2.
  2. A bucket of 8+ nodes becomes a tree.
  3. 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.