Quick revision
The short answer
The constructor argument of HashMap is the initial capacity, the number of buckets, not the number of entries the map should hold. With the default load factor of 0.75, the map resizes as soon as it holds more than three quarters of its capacity, so inserting exactly expectedSize entries crosses that threshold and triggers a resize. HashMap.newHashMap(expectedSize), added in JDK 19, computes the right capacity for us; EnumMap, Map.of and a good hashCode cover the related cases.
What to remember
In this answer, we saw that new HashMap<>(n) sets the initial capacity, not the number of entries, and that with a load factor of 0.75 the map resizes once its size passes three quarters of its capacity, so inserting n entries triggers a resize. HashMap.newHashMap(n) and its siblings from JDK 19, or dividing by the load factor ourselves, size the map for entries instead. For enum keys, EnumMap avoids hashing entirely, Map.of suits small immutable tables, and a poor hashCode can make any map slow, so we check key distribution as well.
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.
The constructor argument of HashMap is the initial capacity, the number of buckets, not the number of entries the map should hold. With the default load factor of 0.75, the map resizes as soon as it holds more than three quarters of its capacity, so inserting exactly expectedSize entries crosses that threshold and triggers a resize. HashMap.newHashMap(expectedSize), added in JDK 19, computes the right capacity for us; EnumMap, Map.of and a good hashCode cover the related cases.
Why this question is asked
Pre-sizing collections is a classic optimisation, and this question shows how easily it goes wrong. The code looks careful, someone clearly thought about sizing, and yet the profile shows the map resizing anyway. Interviewers ask it to check that we know what the HashMap constructor's number actually means and how the load factor works. In this answer, we will recap capacity and load factor, reproduce the resize, fix it in a few ways, and then look at related cases where a different map or a better hashCode is the real answer.
1A Quick Recap of Capacity and Load Factor
A HashMap keeps its entries in an array of buckets. The length of that array is the capacity, and it is always a power of two. The load factor, 0.75 by default, decides how full the map may get before it grows: when the number of entries exceeds capacity times load factor, called the threshold, the map doubles its capacity and moves every entry into the new array. That move is the resize the profile shows.
The reason for the load factor is speed. If the map waited until it was completely full, many buckets would hold several entries and lookups would slow down. Keeping the map at most three quarters full keeps collisions rare.
2Reproducing the Resize
Now the constructor. new HashMap<>(n) treats n as the initial capacity, rounds it up to the next power of two, and computes the threshold from that capacity. It does not mean "room for n entries".
- Constructorcapacity rounded up to a power of two
- Thresholdcapacity x 0.75
- Insert entriesone by one
- Size above threshold?YesResize: double the table, move all entriesNoKeep inserting
- Donetable about twice the size needed
5 steps in order. Play them, or step through them one at a time.
Because the threshold is three quarters of the capacity, n entries always cross it when capacity is close to n.
So every one of these maps does one full resize near the end of the insertion, copying three quarters of a million entries into a new table that is twice as big, and then holds a table much larger than needed. If the batch job builds many such maps, for example one per input file or per customer, those resizes add up to the time the profile shows. For an expectedSize that is not close to a power of two, such as 100,000, the rounded capacity of 131,072 has a threshold of 98,304, so the same thing happens.
3The Fix: Size for Entries, Not Buckets
JDK 19 added a factory that takes the number of entries and computes the capacity for us. It exists for the related classes too.
The arithmetic is simple: to hold n entries without crossing the threshold, the capacity must be at least n divided by 0.75. The factory does exactly this and avoids the rounding mistakes people make by hand. After the change, the profile should show no time in HashMap.resize during insertion.
4When a Different Map Is Better
Sometimes the best fix is not sizing the HashMap correctly but not using one. If the keys are values of an enum, EnumMap stores the values in an array indexed by the enum's ordinal. It never hashes, never resizes and never collides, and it iterates in declaration order.
Map.of and Map.ofEntries create immutable maps sized exactly for their contents, which suits small configuration tables built once. They reject null keys and values and duplicate keys, which is usually a feature.
5Check the Keys' hashCode Too
While we are looking at a slow map, it is worth checking the keys. If many keys produce the same hash code, they all land in the same bucket, and every lookup in that bucket has to compare against many entries. Since Java 8, a crowded bucket turns into a tree when the keys are Comparable, which limits the damage, but a poor hashCode still makes the map much slower than it should be.
A quick way to check key distribution is to count distinct hash codes across a sample of keys. If a million keys produce only a few thousand distinct hash codes, the hashCode is the bottleneck, not the sizing.
6Follow-up questions
Why is the default load factor 0.75?
It is a balance between memory and speed. A lower load factor wastes more empty buckets; a higher one causes more collisions and longer bucket chains. 0.75 keeps the expected chain length short while leaving about a quarter of the table empty.
Does resizing cost more than just time?
Yes. During a resize the old and new tables exist at the same time, so memory briefly peaks, and the large new array may be allocated directly in the old generation or, in G1, as a humongous object. For very large maps that can trigger extra GC work on top of the copying.
Should we always pre-size maps?
Only when we know the size and the map is large or created often. For small maps or unknown sizes, the default is fine, and a wrong guess wastes memory. Pre-sizing is an optimisation for measured hot spots like this batch job.
7Conclusion
In this answer, we saw that new HashMap<>(n) sets the initial capacity, not the number of entries, and that with a load factor of 0.75 the map resizes once its size passes three quarters of its capacity, so inserting n entries triggers a resize. HashMap.newHashMap(n) and its siblings from JDK 19, or dividing by the load factor ourselves, size the map for entries instead. For enum keys, EnumMap avoids hashing entirely, Map.of suits small immutable tables, and a poor hashCode can make any map slow, so we check key distribution as well.
Open with
It resizes because the constructor argument means initial capacity, the number of buckets, not the number of entries. HashMap rounds it up to a power of two and sets the resize threshold at capacity times the load factor, 0.75 by default.
Then, if they want more
So with new HashMap of one million, the capacity is 1,048,576 and the threshold is about 786,000. When we insert the full million entries, we cross the threshold and the map doubles to two million buckets and rehashes everything. If the job builds many such maps, that is the time we see in HashMap.resize.
The fix is to size for entries. Since JDK 19 there is HashMap.newHashMap(expectedSize), which computes a capacity large enough that the entries fit below the threshold, and there are matching factories for HashSet and LinkedHashMap. Before 19, I would divide the expected size by 0.75 and round up, or use Guava's newHashMapWithExpectedSize.
I would also check whether a HashMap is the right structure at all: for enum keys, EnumMap is an array indexed by ordinal with no hashing or resizing, and for small fixed tables Map.of is compact and immutable.
Finally, a slow map can also come from a poor hashCode, so I would check how many distinct hash codes the keys produce.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 19, Java 8
- 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.
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 "A profile of a batch job shows significant time in HashMap.resize. The code creates new...": https://scrivio-interview-library.netlify.app/java/java-performance/9fc9003aae/