What are the initial capacity and load factor of a HashMap, and what happens when it resizes?

A HashMap starts with 16 buckets by default and a load factor of 0.75. When the number of entries exceeds capacity × load factor (12 for the default), the map doubles its bucket array and redistributes the entries. Resizing is expensive, so setting a suitable initial capacity helps when you know roughly how many entries you will store.

Key Points: • Default capacity is 16 and the default load factor is 0.75, which is a good balance between memory use and collisions. • The capacity is always a power of two. new HashMap<>(10) actually uses 16 buckets. • Threshold = capacity × load factor. When size exceeds it, the table doubles (16 → 32 → 64 …). • Since Java 8, each entry either stays in the same bucket index or moves to index + oldCapacity, so resizing doesn't recompute every hash. • The bucket array is created lazily, on the first put(). • Java 19 added HashMap.newHashMap(n), which calculates the right capacity for n entries for you.

Code Example:

import java.util.HashMap;
import java.util.Map;

public class HashMapCapacity {

    static long fill(Map<Integer, Integer> map, int n) {
        long start = System.nanoTime();
        for (int i = 0; i < n; i++) map.put(i, i);
        return (System.nanoTime() - start) / 1_000_000;
    }

    public static void main(String[] args) {
        int n = 1_000_000;
        int threshold = (int) (16 * 0.75f);
        System.out.println("Default threshold: " + threshold + " entries before the first resize");

        // Without a size hint: about 17 resizes on the way to 1 million entries
        Map<Integer, Integer> grow = new HashMap<>();
        // With a size hint: no resizing at all (Java 19+)
        Map<Integer, Integer> sized = HashMap.newHashMap(n);

        fill(grow, n);
        fill(sized, n);
        System.out.println("Both maps hold " + grow.size() + " and " + sized.size() + " entries");

        // Before Java 19, the usual formula:
        int capacity = (int) Math.ceil(n / 0.75);
        System.out.println("Initial capacity for " + n + " entries: " + capacity);
    }
}

Output:

Default threshold: 12 entries before the first resize Both maps hold 1000000 and 1000000 entries Initial capacity for 1000000 entries: 1333334

Good to Know: Lowering the load factor (for example 0.5) means fewer collisions but more memory. Raising it above 0.75 saves memory but makes lookups slower. In practice you almost never change it; setting the initial capacity is the useful optimisation.

Interview Tip: A concise interview answer is:

"A HashMap has a default capacity of 16 buckets and a load factor of 0.75, so it resizes after 12 entries. Resizing doubles the bucket array and redistributes the entries, which is costly, so when I know the expected size I create the map with enough capacity, or use HashMap.newHashMap(n) in Java 19 and later."

Previous question: Can a HashMap have a null key? How is it stored?

Next question: What is the difference between HashMap, Hashtable and ConcurrentHashMap?

More Java Collections interview questions

All Java Collections questions