Going deeper
After this: See how open addressing works and what Python and Java really do.
On this page · 5 sections · no checks
This lesson is optional and short. It is the hardest part of the tutorial, and you can skip it. The hash map in this tutorial is small on purpose. Here is what the real ones do differently, and why none of it changes what you learned.
Open addressing
Chaining keeps colliding pairs in a list inside the slot. The other main method keeps every pair in the array itself. On a collision, it tries the next slot, then the next, until it finds a free one. Trying slots like this is called probing, and the whole method is called . The simplest rule is linear probing, which tries the slots in order.
- 10cat → 31—2—3—4—5—6—7—
Insert cat → 3. Slot 0 is free, so it goes there, exactly as with chaining.
- 20cat → 31act → 52—3—4—5—6—7—
Insert act → 5. Slot 0 is taken. Instead of starting a chain, probe the next slot: 1 is free, so act lands there.
- 30cat → 31act → 52empty3—4—5—6—7—
Look up tac (also slot 0). Compare cat (no), probe to 1, compare act (no), probe to 2: empty, so tac is not in the map.
A lookup follows the same probes and stops at the first empty slot. Deleting is the awkward part. If a slot were emptied, a later lookup would stop there too early and miss a pair stored further along. So a deleted slot gets a special marker called a tombstone, which tells lookups to keep going.
Open addressing needs a lower load factor than chaining, because neighbouring full slots form long runs called clusters. But it keeps everything in one array, which uses memory well, and processors read neighbouring memory quickly.
What Python's dict does
Python's dict uses open addressing. Its probing order mixes in more bits of the hash, which breaks clusters up. A dict also keeps its pairs in a separate compact array, in the order they were inserted, and the hash table holds only positions in that array. This is why going through a dict gives insertion order, which Python has guaranteed since version 3.7, and it is why a dict uses memory well. A dict grows when it is about two-thirds full. String hashes change each time a program starts.
What Java's HashMap does
Java's HashMap uses chaining, like this tutorial, with two improvements.
- It mixes the high bits of the hash into the low ones before it takes the remainder. The capacity is always a power of two, so the remainder can be found with a quick operation on the bits, called a bit mask.
- A chain longer than eight pairs, in a table of at least 64 slots, is turned into a balanced tree. This limits the worst-case lookup to O(log n), where the steps grow very slowly, as long as the keys can be compared with each other. The tree turns back into a list when it shrinks to six pairs.
The load factor limit is 0.75 by default, the same as here. LinkedHashMap adds insertion order. TreeMap is a different structure, a red-black tree, which keeps its keys sorted and costs O(log n) for its main operations.
Hash sets
A hash set is a hash map with the values removed. add is an insert without a value, and incontains is a lookup. Python's set also uses open addressing, but with its own code. It has no separate compact array, so it does not keep insertion order. Java's HashSet holds a HashMap inside it and stores a dummy value with each key. The hash sets tutorial comes next.
Everyday and cryptographic hash functions
Everything here uses everyday hash functions, which are fast and spread keys well, and promise nothing more. A cryptographic hash function, such as SHA-256, adds a guarantee: in any practical amount of time, nobody can find two inputs with the same output, or an input that gives a chosen output. That makes it right for checking that a download has not been changed. It is the wrong choice for a hash map, where it would be much slower and give no benefit. Passwords need a third kind, a deliberately slow password hash such as bcrypt or Argon2. All three are called hash functions, but they do different jobs.
- Explain open addressing with linear probing and how it differs from chaining.
- Say what Python's dict and Java's HashMap really do.
- Tell an everyday hash function from a cryptographic one.