Quick revision
The short answer
No. new HashMap<>(1000) asks for a capacity of 1000, which HashMap rounds up to a table of 1024 buckets, and at the default load factor of 0.75 that table only holds 768 entries before it resizes, so the 769th put still doubles the table to 2048. To hold n entries without resizing, the capacity must be at least n / 0.75, here (int) Math.ceil(1000 / 0.75) = 1334, and since Java 19 we should simply write HashMap.newHashMap(1000), which does that calculation for us.
What to remember
In this answer, we saw that new HashMap<>(1000) treats 1000 as a capacity, rounds it to 1024 buckets with a threshold of 768, and therefore still resizes to 2048 on the 769th put. To hold n entries without resizing, the capacity must be at least n / 0.75, which is (int) Math.ceil(1000 / 0.75) = 1334 here, and since Java 19 HashMap.newHashMap(1000) computes this for us, with matching factories on LinkedHashMap, WeakHashMap, HashSet and LinkedHashSet. The key point is that the constructor argument is a capacity, not an expected size.
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.
No. new HashMap<>(1000) asks for a capacity of 1000, which HashMap rounds up to a table of 1024 buckets, and at the default load factor of 0.75 that table only holds 768 entries before it resizes, so the 769th put still doubles the table to 2048. To hold n entries without resizing, the capacity must be at least n / 0.75, here (int) Math.ceil(1000 / 0.75) = 1334, and since Java 19 we should simply write HashMap.newHashMap(1000), which does that calculation for us.
Why this question is asked
Pre-sizing a HashMap is a small optimisation that people get wrong surprisingly often, because the constructor's parameter looks like an expected size but is really a capacity. For 1,000 entries the cost of one extra resize is tiny, but the same mistake in a loop that builds millions of maps, or in a map of tens of millions of entries, shows up in profiles and in memory spikes. Interviewers ask it because it tests whether we understand the relationship between capacity, load factor and threshold. In this answer, we will recap those three numbers, trace exactly what new HashMap<>(1000) does, calculate the right capacity, and then use the Java 19 factory methods that do it for us.
1A Quick Recap: Capacity Is Not Size
A HashMap stores entries in an internal array, the table, and the capacity is the length of that array. Size is the number of entries we have stored. The load factor, 0.75 by default, says how full the table may get before the map grows. The threshold, capacity times load factor, is the size at which the next put triggers a resize, which allocates a table twice as long and moves every entry into it.
The constructor HashMap(int initialCapacity) takes a capacity. It does not mean the map can hold that many entries. With a load factor of 0.75, a table of capacity c holds at most 0.75 x c entries without resizing.
2Tracing new HashMap<>(1000)
Let us follow the numbers. The capacity is always rounded up to a power of two, because HashMap picks a bucket with a bit mask that needs one. The next power of two at or above 1000 is 1024.
| How the map is created | Initial table | Threshold | Resizes for 1,000 puts |
|---|---|---|---|
new HashMap<>() | 16 | 12 | 7 (to 32, 64, ... 2048) |
new HashMap<>(1000) | 1024 | 768 | 1 (at put 769) |
new HashMap<>(1334) | 2048 | 1536 | 0 |
HashMap.newHashMap(1000) | 2048 | 1536 | 0 |
So the colleague's code avoids the several resizes a default map would go through (16, 32, 64 and so on up to 2048), but one resize still happens, when the 769th entry arrives. The map ends with 2048 buckets either way; the extra work is moving 768 entries once.
3Calculating the Right Capacity
To hold n entries without a resize, we need capacity x 0.75 >= n, so capacity >= n / 0.75. For 1000 that is 1333.33, which we round up to 1334, and HashMap then rounds 1334 up to the power of two 2048.
Notice the 0.75 is a double literal, so the division is done in floating point rather than in int arithmetic, and Math.ceil makes the rounding up explicit. A capacity even one short of n / 0.75 could, at the wrong size, leave the threshold just below n and bring back the very resize we wanted to avoid.
4Since Java 19: HashMap.newHashMap
Because so many people got this wrong, Java 19 added static factory methods that take the number of mappings we expect, rather than a capacity, and do the calculation internally.
This is what I would write. It states the intent, expected number of entries, directly in the name, and it uses the default load factor correctly. On a codebase still on Java 17, the Math.ceil formula above, or a small helper method wrapping it, is the equivalent.
5Why the Constructor Is So Often Misread
The parameter name initialCapacity is accurate, but the very similar ArrayList(int initialCapacity) behaves the way people expect: an ArrayList of capacity 1000 holds 1000 elements before it grows, because it has no load factor. Developers carry that intuition over to HashMap, where it is off by a quarter. The JDK documentation of HashMap now points readers to newHashMap for exactly this reason. In code review, any new HashMap<>(n) where n is an expected size is worth a comment.
6Follow-up questions
Does Guava's Maps.newHashMapWithExpectedSize do the same thing?
Yes, in effect. For expected sizes of 3 or more it computes roughly expectedSize / 0.75 + 1 and passes that as the capacity, so for 1000 it asks for 1334 and gets the same 2048-bucket table, with no resize for 1000 entries. It predates Java 19 and was the usual way to do this correctly. On Java 19 or later, HashMap.newHashMap is the standard choice and removes the dependency for this purpose.
Is over-sizing harmful?
Only in memory. Each bucket is a reference, 4 bytes with compressed references, so an unused table of 2048 slots costs about 8 KB. That is negligible for one map but adds up if millions of small maps are each created with a large capacity. Size for what you expect, not for a worst case you never reach.
Does pre-sizing matter for 1,000 entries at all?
Rarely on its own. One resize of 768 entries takes microseconds. It matters when the map is built in a hot loop, when it is very large, or when latency spikes from a single slow put are a problem. The habit is still worth having, because the correct code is just as short.
7Conclusion
In this answer, we saw that new HashMap<>(1000) treats 1000 as a capacity, rounds it to 1024 buckets with a threshold of 768, and therefore still resizes to 2048 on the 769th put. To hold n entries without resizing, the capacity must be at least n / 0.75, which is (int) Math.ceil(1000 / 0.75) = 1334 here, and since Java 19 HashMap.newHashMap(1000) computes this for us, with matching factories on LinkedHashMap, WeakHashMap, HashSet and LinkedHashSet. The key point is that the constructor argument is a capacity, not an expected size.
Open with
No, it does not quite avoid resizing. The constructor argument is a capacity, not an expected number of entries. HashMap rounds 1000 up to the next power of two, 1024, and with the default load factor of 0.75 the threshold is 768.
Then, if they want more
So the first 768 puts fit, but the 769th makes the size exceed the threshold and the table doubles to 2048, moving all 768 entries once. It is better than a default map, which would resize seven times on the way to 2048, but it is not what the colleague intended.
To hold n entries with no resize, the capacity has to be at least n divided by the load factor, so I would compute (int) Math.ceil(1000 / 0.75), which is 1334, and HashMap rounds that to 2048 with a threshold of 1536.
On Java 19 or later I would not do the arithmetic at all; I would write HashMap.newHashMap(1000), which takes the expected number of mappings and sizes the table correctly. There are matching methods, LinkedHashMap.newLinkedHashMap, WeakHashMap.newWeakHashMap, and HashSet.newHashSet.
The common misunderstanding comes from ArrayList, where the initial capacity really is the number of elements it can hold before growing, because ArrayList has no load factor. For a thousand entries the extra resize hardly matters, but in a hot path or a very large map it does, and the correct code is just as short.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 19, Java 17
- 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 "You need to load exactly 1,000 entries into a HashMap. A colleague writes new...": https://scrivio-interview-library.netlify.app/java/hashmap-internals/5b2ef07bb2/