HashMap Capacity and Load Factor
Fast recall
Quick revision
Core answer
HashMap allocates its buckets; a default-constructed map allocates lazily, with a default target capacity of 16. Load factor controls the resize threshold; the default is 0.75. If you know the expected mappings, size the map to avoid repeated growth, but remember capacities are normalized internally and exact allocation is an implementation detail.Whether you understand the resize trigger and how to pre-size to avoid rehashing
Setting initial capacity to exactly n (forgetting the 0.75 factor still triggers a resize)
Explain capacity is a power of two in the current implementation and the 0.75 default balances space vs collision rate
Interview-ready guide
Interview answer
HashMap capacity is the size of its allocated table, while the load factor helps determine the resize threshold. The default constructor does not immediately allocate a 16-element table; allocation is lazy, and the normal first table targets capacity 16 with load factor 0.75.
When I know the expected number of mappings, I pre-size the map to reduce growth and redistribution. On recent Java I can use HashMap.newHashMap(expectedMappings). On older Java I allow enough capacity for the expected count at the default load factor. I do not change the load factor without measurement, and I describe power-of-two capacity and exact growth rules as current implementation details rather than part of the Map contract.
HashMap Capacity and Load Factor
Direct Answer
Initial capacity is the table size used when HashMap allocates its buckets; a default-constructed map allocates lazily, with a default target capacity of 16. Load factor controls the resize threshold; the default is 0.75. If you know the expected mappings, size the map to avoid repeated growth, but remember capacities are normalized internally and exact allocation is an implementation detail.
Code Example
When you know how many entries are coming, pre-sizing skips the repeated resize-and-redistribution storms a default map would suffer.
import java.util.*;
public class CapacityDemo {
public static void main(String[] args) {
// Default: capacity 16, load factor 0.75 -> resizes at 12 entries
Map<String, Integer> def = new HashMap<>();
// Expecting 1000 entries? Pre-size to avoid repeated resizing:
int expected = 1000;
int cap = (int) (expected / 0.75f) + 1; // ~1334
Map<String, Integer> sized = new HashMap<>(cap);
for (int i = 0; i < expected; i++) sized.put("k" + i, i);
// No resize storms during the loop.
// Custom load factor (rarely needed): more memory, fewer collisions
Map<String, Integer> tight = new HashMap<>(64, 0.5f);
}
}
Note n / 0.75 + 1, not n — capacity equal to n would still trip the 0.75 threshold and resize.
Common Mistakes & Pitfalls
- Setting capacity to exactly
n. The 0.75 factor still triggers a resize at0.75 x n; estimate room fornmappings at the chosen load factor. - Forgetting power-of-two rounding. Allocated table capacity is normalized to a power of two by the current implementation.
- Tuning the load factor blindly. 0.75 is a good default; change it only with a measured reason.
- Ignoring resize cost. Each resize is O(n) and redistributes existing entries — wasteful in tight loops.
Check your understanding
Answer these without looking back at the article.
- What happens during HashMap resize?
- How do you avoid HashMap resizing?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.