Collisions, load factor and resizing
Two different keys can land in the same bucket, and every hash map has a plan for it. Here is how chaining and probing handle the clash, and why the table grows long before it is full.
01 Two keys, one bucket
A map with 8 buckets squeezes every hash into a number from 0 to 7. There are endless possible keys and only 8 buckets, so some keys must share. Two keys in one bucket is a collision.
With the toy hash from the previous tutorial (add up the character codes), "ana" hashes to 304 and "eve" to 320. Different hashes, but 304 % 8 and 320 % 8 are both 0.
Even a perfect hash cannot avoid this. If keys land in random buckets, collisions show up much sooner than you might guess:
| Keys | Chance of a collision |
|---|---|
| 2 | 12.5% |
| 3 | 34.4% |
| 4 | 59.0% |
| 5 | 79.5% |
| 6 | 92.3% |
Four keys in eight buckets already collide more often than not. This is the birthday problem, and it is why a map must handle collisions as normal work, not as a rare error.
02 Chaining: each bucket holds a list
The first fix: let each bucket hold a small linked list, its chain. A put hashes to the bucket, walks the chain looking for the key, and adds a new entry if it is not there. A get walks the same chain and compares each entry.
- comparing now
- checked, not it
- just stored
ages["eve"] = 22 is called. Four keys are already stored, one per bucket, and bucket 0 holds "ana". Nothing is read yet.
Java’s HashMap uses chaining. Since Java 8, a chain that grows past 8 entries is turned into a balanced tree (once the table has at least 64 buckets), so even a badly crowded bucket costs a few compares, not hundreds.
03 Open addressing: try the next slot
The second fix keeps one entry per slot and no lists at all. If a key’s home slot is taken, the map probes other slots in a fixed order until it finds a free one. The simplest order is the next slot, then the next: linear probing.
- probing now
- taken, move on
- stored here
Same four keys, but now every slot holds at most one entry. ages["eve"] = 22 needs a home. Nothing is probed yet.
A lookup follows the same path and stops at the first empty slot, because the key would have been stored there. Looking up "ivy", which also hashes to slot 0 and was never stored, now checks slots 0, 1, 2 and 3 before slot 4 proves it is absent. Taken slots bunch together into clusters, and clusters make misses slow.
CPython’s dict uses open addressing, but not the next slot: it jumps around in an order driven by the hash’s higher bits, which breaks up clusters.
04 Load factor: how full is too full
The load factor is entries divided by buckets. With chaining, it is the average chain length. With open addressing, it is the share of slots taken, and it can never pass 1.
For linear probing with a good hash, Knuth’s classic estimates give the expected number of slots a lookup checks. The cost of a miss explodes as the table nears full, because a miss has to walk to the end of a cluster:
| Load factor | Lookup that finds the key | Lookup for a missing key |
|---|---|---|
| 0.25 | 1.2 | 1.4 |
| 0.50 | 1.5 | 2.5 |
| 0.75 | 2.5 | 8.5 |
| 0.90 | 5.5 | 50.5 |
| 0.99 | 50.5 | 5,000.5 |
Up to about three-quarters full, both kinds of lookup take a handful of probes. Past that, misses get expensive fast. So maps pick a limit and grow the table before reaching it.
05 Resizing: a bigger table, every key moved
Growing a hash table is like growing an array: allocate a bigger one, usually twice the size, and move everything. But a key cannot be copied to the same position, because its bucket depends on the table size. Each key’s bucket is worked out again from its hash.
- just placed
- moving now
- moved out
A map of integer keys, where the hash of a number is the number itself (as for Java’s Integer and small Python ints). 3 entries in 4 buckets: load 0.75. The limit is 0.75.
With power-of-two sizes, doubling means each key either keeps its bucket number i or moves to i + old size, depending on one more bit of its hash. Java’s HashMap uses this to split each chain in two without recomputing the hashes. As with arrays, doubling keeps the average cost of a put constant: the big moves are rare enough to share out.
06 Watch a Python dict resize
Print the size of a dict each time it changes as you add keys.
This is CPython 3.14 on a 64-bit machine. The first key allocates 8 slots, which may hold 5 entries (two-thirds of 8, rounded down). The 6th key grows the table to 16 slots, which hold 10; the 11th grows it to 32, which hold 21; the 22nd to 64. The jumps land right at about two-thirds full.
07 When it bites
Constant-time lookups depend on keys spreading out. If many keys share one bucket, every lookup in it is a linear search, and a map of a million entries can be as slow as a list.
int hashCode() { return 1; }A hash that gives everyone the same bucketIt is legal, since equal objects still get equal hashes, but every key collides. Use the language’s helpers, such as Objects.hash(a, b) in Java, that mix every field.HashMap.newHashMap(1_000_000)Paying for resizes you could skipEach resize moves every entry. If you know the size, ask for room up front (Java 19 added HashMap.newHashMap(n), which allows for the load factor).HashMap can iterate in a different order after more puts. CPython’s dict keeps insertion order because its entries live in a separate array from its slots.