HashMap Resize and Load Factor โ Java Interview Guide
Reviewed by Gurusankar M. ยท Updated Aug 24, 2026
โก Short Answer
When size exceeds capacity ร load factor (default 0.75), the table doubles and every entry rehashes โ an O(n) spike. For known sizes, pre-size with initialCapacity = expected / 0.75 to avoid repeated resizes.
โCoffee Chat Question
Concept Made Simple
โHow do load factor and resizing affect HashMap performance, and how do you tune it?โ
๐ง Mind Map Answer
Remember It Faster
Load factor is the fraction of capacity that can fill up before HashMap grows โ default 0.75. The threshold is capacity ร loadFactor; once size exceeds it, HashMap doubles capacity and rehashes every existing entry into the new, larger table.
| Capacity | Threshold (ร 0.75) | Resizes when size exceeds |
|---|---|---|
| 16 (default) | 12 | 12 entries |
| 32 | 24 | 24 entries |
| 64 | 48 | 48 entries |
| 128 | 96 | 96 entries |
Resizing isn't a cheap copy โ every entry's bucket index depends on hash(key) & (capacity - 1), so a bigger capacity means most entries land in a different bucket. HashMap has to walk every existing entry and re-insert it into the new table, which is why a resize is O(n) even though put() is normally amortized O(1).
A resize can also change the order entries come out in during iteration. HashMap never guaranteed iteration order, but because bucket index depends on capacity, the exact same entries can walk out in a different order before and after a resize. Code that happens to "work" because of an incidental order will break the moment the map crosses a resize threshold โ if order matters, use LinkedHashMap (insertion order) or a TreeMap (sorted), don't rely on HashMap's.
Load factor interacts with a second Java 8+ mechanism: treeification. If a single bucket collects 8 or more colliding entries and the table capacity is at least 64, that bucket's linked list converts to a red-black tree, turning worst-case lookup in that bucket from O(n) to O(log n). A resize can also untreeify a bucket back to a list once it drops to 6 entries after keys redistribute. This only kicks in under pathological hashing (poor hashCode() or an adversarial key set) โ with a well-distributed hash, buckets rarely get anywhere near 8 entries, so most HashMaps never treeify at all.
ConcurrentHashMap resizes differently. A plain HashMap resize is a single-threaded stop-the-world rehash of every entry. ConcurrentHashMap instead marks the table as transferring and lets any thread that calls put()/get() during the resize help migrate a chunk of buckets (transferIndex, ForwardingNode) โ readers never block, and multiple writer threads share the rehash cost instead of one thread paying for all of it. The trade-off is more bookkeeping per resize, which is why ConcurrentHashMap benefits from pre-sizing even more than HashMap does in write-heavy concurrent code.
Capacity is always a power of two, even if you don't ask for one. new HashMap<>(1000) doesn't create a table of exactly 1000 buckets โ the constructor's tableSizeFor() rounds any requested capacity up to the next power of two (1000 โ 1024), because the fast bucket-index calculation hash(key) & (capacity - 1) only behaves correctly (as a substitute for the slower % modulo) when capacity - 1 is all 1-bits, which is only true for powers of two. Pre-sizing math should account for this: new HashMap<>(1366) for ~1000 entries at 0.75 load factor actually allocates a 2048-bucket table, not a 1366-bucket one โ still correct, just worth knowing so a "precisely sized" map isn't a surprise in a memory profiler.
HashSet is a HashMap under the hood โ every HashSet field is backed by an internal HashMap<E, Object>, with each added element stored as a key mapped to a single shared dummy value (PRESENT). Every rule covered here โ load factor, threshold, resize cost, treeification, iteration-order instability โ applies identically to HashSet, because add(), contains(), and size() are just thin wrappers around the underlying map's put(), containsKey(), and size(). A new HashSet<>(expected * 4 / 3 + 1) pre-sizing call is solving the exact same resize problem as pre-sizing a HashMap.
โจ๏ธHands-on Keyboard
Learn by Doing
// Avoid 4 resizes when loading ~1000 entries
Map<String,Order> m = new HashMap<>(1366); // 1000 / 0.75 โ 1334 โ next pow2
records.forEach(r -> m.put(r.id(), r));๐ฅWhat If?
Think Beyond the Expected
A batch job loading 10M rows into a HashMap shows periodic latency spikes โ why?
Each time the map crosses the resize threshold it doubles capacity and rehashes all existing entries (O(n)). For 10M entries that's ~23 doublings, each more expensive. Pre-sizing the map eliminates the repeated rehash spikes.
๐Real World
Pre-sizing maps/lists before bulk loads is a standard performance fix in ETL and caching code โ it turns a series of O(n) rehash pauses into a single allocation.
๐ฏInterviewer's Expectation
Keywords they're listening for:
โ ๏ธCommon Mistakes
- โNot pre-sizing maps before known bulk loads
- โSetting initialCapacity = expected (forgetting the 0.75 factor)
- โRaising load factor to 1.0 and increasing collisions
โ Best Practices
- โPre-size: initialCapacity โ expected / 0.75
- โKeep the default 0.75 unless profiling says otherwise
- โUse the same pattern for ArrayList/StringBuilder
๐Follow-up Questions
- 1What is the default load factor for a Java HashMap?
- 2When exactly does a Java HashMap resize?
- 3What is the relationship between capacity, size, and load factor?
- 4Why does resizing require rehashing and bucket redistribution?
- 5Why is the load factor 0.75 a space/time compromise?
- 6Does a higher load factor save memory? At what cost?
- 7How does this differ for ConcurrentHashMap?
๐งฉRelated Technologies
๐References
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly โ AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Performance (Java Collections) Interview question: "How do load factor and resizing affect HashMap performance, and how do you tune it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
โญ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission โ at no extra cost to you. It helps keep Full Stack Interview Guru free.