Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How hash maps work: hash functions, buckets, collisions, chaining vs open addressing, load factor and resizing, and when O(1) quietly becomes O(n).
Quick answer: A hash map stores key-value pairs in an array. To find where a key belongs, it runs the key through a hash function, which turns it into a number, and uses that number to compute an array index. Because computing a hash and jumping to an array position take the same time no matter how many items are stored, lookups, inserts and deletes take constant time on average: O(1). When two keys land on the same index (a collision), the map resolves it by chaining or probing, and it grows the array before collisions become common.
Hash maps go by many names: dict in Python, Map or plain objects in JavaScript, HashMap in Java and Rust, map in Go, unordered_map in C++.
Arrays are fast because the computer can jump straight to position i. But arrays are indexed by small integers, and we want to look things up by names, IDs or anything else.
A hash map bridges that gap:
index = hash(key) % number_of_buckets
Looking up "alice" in a map of ten items or ten million involves the same three steps.
A hash function for a hash map should be:
These are not cryptographic hashes. Functions such as SHA-256 are designed to resist attackers and are far slower. Hash maps use lightweight functions such as wyhash, xxHash or SipHash.
Two rules follow for the keys themselves:
There are far more possible keys than buckets, so some keys must share. Thanks to the birthday paradox, collisions appear much sooner than intuition suggests: among just 23 people, two probably share a birthday out of 365 possible days.
There are two main ways to handle collisions.
Each bucket holds a small list of entries. To look up a key, hash to the bucket, then walk the list comparing keys.
HashMap uses chaining and converts long chains into balanced trees to limit worst-case cost.All entries live directly in the array. If a slot is taken, the map probes for another:
Lookups follow the same probe sequence until they find the key or an empty slot.
dict, Rust's HashMap and Go's map use variations of open addressing. Google's Swiss Tables design, which several of these are based on, stores a byte of each hash in a compact metadata array so it can check many slots at once.| Separate chaining | Open addressing | |
|---|---|---|
| Where entries live | In lists hanging off buckets | In the array itself |
| Cache behaviour | Poorer | Better |
| Tolerates a full table | Yes, with slower lookups | No, must stay partly empty |
| Deletion | Simple | Needs tombstones or shifting |
The load factor is the number of entries divided by the number of buckets. As it rises, collisions multiply and operations slow down.
So when the load factor passes a threshold (0.75 in Java, about two-thirds in CPython, around 0.875 in Swiss Tables), the map resizes:
A resize costs O(n), but it happens so rarely that the average cost per insert stays constant. This is called amortised O(1). Doubling means that across n inserts, the total copying work is proportional to n.
If you know roughly how many items you will insert, reserve capacity up front and skip the intermediate resizes.
"Constant time" is an average. The worst case is O(n):
| Need | Best choice |
|---|---|
| Look up by exact key | Hash map |
| Keys in sorted order, or range queries | Balanced tree or B-tree |
| Smallest or largest item quickly | Heap |
| A handful of items | A plain array is often faster |
This is why databases usually use B-trees for indexes: they support range queries such as "between 10 and 20", which a hash cannot. See how database indexes work. A URL shortener is the same lookup in miniature, one code in and one destination out; see how to design a URL shortener. Distributed systems apply the same hashing idea across machines; see consistent hashing.
By design, no. But several languages add it:
Map iterates in insertion order.LinkedHashMap for insertion order and TreeMap for sorted order.Because finding the bucket is arithmetic on the key's hash, which does not depend on how many items are stored. With few collisions, only one or two comparisons follow.
Two different keys that map to the same bucket. The map handles it by chaining or probing.
A set stores only keys; a map stores a value with each key. They work the same way internally.
Only if it has a stable hash and a matching definition of equality. Mutable objects generally make poor keys.
A hash map turns "search for this key" into "compute a position and go there". A good hash function spreads keys out, a collision strategy handles the overlaps, and resizing keeps the table sparse enough to stay fast. It is one of the most useful data structures in programming, and knowing its limits tells you when to reach for a tree instead.