Quick revision
The short answer
A new HashMap has no table at all until the first put, which creates one of 16 buckets; with the default load factor of 0.75 the threshold is 12, so the 13th entry triggers the first resize. Resizing doubles the table and moves every entry, which makes that one put O(n), but because the size doubles, each entry's new index is either its old index i or i + oldCap, decided by the single bit hash & oldCap, so nothing is rehashed. Java 8 splits every bucket into a lo list and a hi list that keep their relative order, and splits tree bins the same way, turning small halves back into lists.
What to remember
In this answer, we saw that a HashMap starts with no table, creates 16 buckets on the first put, and resizes when the size exceeds the threshold, capacity times load factor, so the 13th entry triggers the first resize with the defaults. Each resize doubles the table and moves every entry, an O(n) step that averages out to O(1) per put. Because the table doubles, each entry stays at index i or moves to i + oldCap depending on hash & oldCap, with no rehashing, and Java 8 splits each bin into order-preserving lo and hi lists, splitting tree bins too and turning halves of 6 or fewer nodes back into lists.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A new HashMap has no table at all until the first put, which creates one of 16 buckets; with the default load factor of 0.75 the threshold is 12, so the 13th entry triggers the first resize. Resizing doubles the table and moves every entry, which makes that one put O(n), but because the size doubles, each entry's new index is either its old index i or i + oldCap, decided by the single bit hash & oldCap, so nothing is rehashed. Java 8 splits every bucket into a lo list and a hi list that keep their relative order, and splits tree bins the same way, turning small halves back into lists.
Why this question is asked
Resizing is the hidden cost of a HashMap. Most put() calls are a few nanoseconds, but now and then one put copies the whole table, and in a large map that one call can take milliseconds and briefly double the memory the table uses. Interviewers ask about it because it explains latency spikes, why pre-sizing matters and how Java 8 made the operation cheaper and safer. In this answer, we will recap capacity, load factor and threshold, watch a map reach its first resize, follow how entries move to the new table, see how bins are split, and compare it with what Java 7 did.
1A Quick Recap of Capacity, Load Factor and Threshold
The capacity of a HashMap is the length of its internal array, the table. Each slot is a bucket. The load factor is how full the map may get, as a ratio of entries to buckets, before it grows; the default is 0.75. Multiplying them gives the threshold, the number of entries the map can hold before it resizes: 16 x 0.75 = 12 for a default map.
Capacity is always a power of two, because the bucket index is computed as (n - 1) & hash, a bit mask that only works as a remainder when n is a power of two. If we ask for a capacity of 100, HashMap rounds it up to 128.
2Watching the First Resize
We cannot see the table without reflection, but we can reason about it step by step. The constructor stores the settings and allocates nothing. The first put() notices that the table is null and calls resize(), which creates the table of 16 buckets and sets the threshold to 12.
Notice that the check is ++size > threshold, made after the new entry is added. That is why the 13th entry, not the 12th, triggers the first resize. Creating the table lazily means that maps which are created but never used, which is common in objects with optional data, cost only a small object and no array.
3How Entries Move to the New Table
resize() allocates a new array of twice the length and then visits every old bucket. The work is proportional to the number of entries, so the put that triggers it costs O(n). Spread over all the puts that filled the map, it averages out to O(1) per put, which is called amortised constant time.
The clever part is how each entry finds its new bucket. The index is (n - 1) & hash. When n doubles from 16 to 32, the mask grows from 1111 to 11111, one extra bit. So the new index has the same low four bits as before, and only the fifth bit, worth 16, can differ. That bit is exactly hash & oldCap.
- Take the next node in bucket inodes in their current order
- hash & oldCap == 0?one bit decidesYesAppend to the lo listNoAppend to the hi list
- Place lo list at new index i
- Place hi list at new index i + oldCap
- Bucket was a tree?
TreeNode.splitYesSplit it; halves of 6 or fewer become listsNoDone
5 steps in order. Play them, or step through them one at a time.
So resize() never calls hashCode() again and never recomputes a full index. It tests one bit of the hash stored in each node. HashMap stores the spread hash in every node precisely so that this step is cheap.
4Splitting Bins Into lo and hi Lists
Java 8 walks each bucket once and builds two lists as it goes: the lo list for nodes whose bit is 0 and the hi list for nodes whose bit is 1. Nodes are appended at the tail of each list, so their relative order is preserved. At the end, the lo list goes into new bucket i and the hi list into new bucket i + oldCap.
If the bucket was a red-black tree, a tree bin, the same split happens, through TreeNode.split. Each half is then checked: if it has UNTREEIFY_THRESHOLD, 6, or fewer nodes, it is turned back into a plain linked list, because a tree is not worth its memory for so few nodes; otherwise it is rebuilt as a tree in its new bucket.
| Node hash | hash & 16 | Old index | New index |
|---|---|---|---|
| 5 | 0 | 5 | 5 (lo) |
| 21 | 16 | 5 | 21 (hi) |
| 37 | 0 | 5 | 5 (lo) |
| 53 | 16 | 5 | 21 (hi) |
5What This Means in Practice
Because of the doubling, a map that grows to a million entries one put at a time resizes 17 times on the way (16, 32, and so on to 2^21), and the last few resizes each move hundreds of thousands of entries. During each resize the old and new tables are both alive, so the table memory briefly triples relative to the old one. If we know the size in advance, HashMap.newHashMap(expected) in Java 19 and later creates a table large enough that no resize happens at all.
6Follow-up questions
What did Java 7 do differently during transfer?
Java 7's transfer() recomputed each entry's index with indexFor(hash, newCapacity) and inserted it at the head of its new bucket. Head insertion reversed the order of every chain on each resize, and when two threads resized the same map at once, the reversal could link two nodes to each other, creating a cycle that made get() loop forever. It could also rehash keys when its optional alternative string hashing was enabled. Java 8's lo and hi split with tail insertion keeps order and avoids that particular cycle, though HashMap is still not thread-safe.
Does a HashMap ever shrink?
No. Removing entries never makes the table smaller. A map that once held a million entries keeps its large table until it is garbage collected. If a long-lived map shrinks a lot, copying it into a new HashMap is the only way to release the table memory.
Why test hash & oldCap rather than recompute the index?
Because it gives the same answer for less work. The new index can only differ from the old one in the bit worth oldCap, so testing that bit is enough, and it avoids touching the key at all, which matters when hashCode() is expensive.
7Conclusion
In this answer, we saw that a HashMap starts with no table, creates 16 buckets on the first put, and resizes when the size exceeds the threshold, capacity times load factor, so the 13th entry triggers the first resize with the defaults. Each resize doubles the table and moves every entry, an O(n) step that averages out to O(1) per put. Because the table doubles, each entry stays at index i or moves to i + oldCap depending on hash & oldCap, with no rehashing, and Java 8 splits each bin into order-preserving lo and hi lists, splitting tree bins too and turning halves of 6 or fewer nodes back into lists.
Open with
A HashMap is created without a table; the first put calls resize, which allocates 16 buckets and sets the threshold to capacity times load factor, 16 times 0.75, which is 12.
Then, if they want more
After each insertion it checks whether size is greater than the threshold, so the 13th entry triggers the first resize. Resizing allocates a table twice as long and moves every entry, so that one put is O(n), though it is amortised O(1) across all puts. The nice part is how entries move.
Because the capacity is a power of two and doubles, the new mask has exactly one more bit, so each entry either stays at its index i or moves to i plus the old capacity, and the deciding bit is hash AND oldCap. The node already stores its hash, so there is no call to hashCode and no full rehash.
In Java 8 the code walks each bucket once and builds a lo list and a hi list, appending at the tail, so relative order is preserved, then drops them into i and i plus oldCap. Tree bins are split the same way, and any half with 6 or fewer nodes is turned back into a plain list.
Java 7 instead recomputed the index and inserted at the head, which reversed chains and, with concurrent resizes, could create cycles. If I know the size up front, I use HashMap.newHashMap so no resize happens.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 7, Java 19
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.HashMap, API documentation, The Collections Framework, overview
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how a Java 8+ HashMap resizes: the defaults, the trigger, and how entries move...": https://scrivio-interview-library.netlify.app/java/hashmap-internals/7c39631f43/