The big idea: turn the key into a position
After this: Work out a key's slot by hand and say what makes a hash function good.
On this page · 5 sections · 3 checks
The last lesson ended with a question: how do we turn a key into a position? This lesson answers it.
Picture the coat room at a theatre. You hand over your coat. The attendant hangs it on hook 17 and gives you a ticket with 17 on it. When you come back, nobody searches the whole rail. The ticket says hook 17, so that is where your coat is.
A hash map works in a similar way. It keeps an array of places to store pairs. Each place is called a , like a hook on the rail, and the slots are numbered from 0. The whole array of slots is called the . The key tells us which slot to use, and this takes two moves.
- If the key is not a number, turn it into a number.
- Turn that number into a slot number.
We start with the second move, because a key that is already a number does not need the first.
Keys that are numbers
Suppose the table has eight slots, numbered 0 to 7, and the keys are student ids. Student 1042 needs a slot, but there is no slot 1042. We need a way to turn any number into a number from 0 to 7.
The remainder does this. A remainder is what is left over after a division, and Python and Java both write it as %.
- 10 % 8 is 2, because 8 goes into 10 once and leaves 2.
- 16 % 8 is 0, because 8 goes into 16 twice and leaves nothing.
- 3 % 8 is 3, because 8 does not go into 3, so all of the 3 is left over.
Now take student 1042.
- The biggest multiple of 8 that fits is 8 × 130 = 1040.
- That leaves 1042 − 1040 = 2, so 1042 % 8 = 2.
- Student 1042 goes in slot 2.
Whatever the id is, dividing by 8 leaves a remainder from 0 to 7, so every id gets a slot. The same id also gets the same slot every time. Try it with two ids. The second one asks you to predict the slot first.
- Hash 1042The table has eight slots. Where does student 1042 go? Press the button and read the slot.
- Hash 2031
- 0—
- 1—
- 2—
- 3—
- 4—
- 5—
- 6—
- 7—
Start with the first task above, or try any key.
- count
- 0
- load
- 0.00
- compares
- 0
Keys that are words
Keys are usually words, and you cannot divide a word by 8. So we need the first move: turn the word into a number.
A computer stores every character as a code number. For example, c is 99, a is 97 and t is 116. To turn a word into a number, we can add up the codes of its characters. Here is the word cat.
- Find the codes: 99, 97 and 116.
- Add them: 99 + 97 + 116 = 312.
- Take the remainder: 312 % 8 = 0, because 8 × 39 is exactly 312.
So cat lands in slot 0.
- Hash catWatch the three stages: find the codes, add them, take the remainder.
- Hash dog
- 0—
- 1—
- 2—
- 3—
- 4—
- 5—
- 6—
- 7—
Start with the first task above, or try any key.
- count
- 0
- load
- 0.00
- compares
- 0
The name for this
A function that turns a key into a number is called a . The number it gives is the key's , so the hash of cat is 312. To hash a key means to run it through the hash function. The hash map is named after this step.
Three things make a hash function good.
- The same key always gives the same result. The word for this is deterministic. If
catgave slot 0 today and slot 5 tomorrow, you could never find it again. - It is fast. It runs every time you store a pair or find one.
- It spreads the keys out. If most keys landed in the same slot, you would be back to searching through them one by one.
Here is the whole function in code.
def index_for(key, capacity): total = 0 for ch in key: total += ord(ch) # the character's code: 'c' is 99 return total % capacity # 312 % 8 is 0
print(index_for("cat", 8))0
static int indexFor(String key, int capacity) { int total = 0; for (char ch : key.toCharArray()) { total += ch; // a char is its code: 'c' is 99 } return total % capacity; // 312 % 8 is 0}
System.out.println(indexFor("cat", 8));0
The loop adds up the code of each character. The last line of the function takes the remainder. capacity is the word for the number of slots in the table, so for eight slots it is 8. For cat, the total is 312 and the function returns 0.
Check yourself
Which slot does fox land in? (f = 102, o = 111, x = 120)
Which slot does emu land in? (e = 101, m = 109, u = 117)
Which quality must a hash function have for the map to give correct answers at all?
Connect
Python and Java each have a hash function built in. It gives a much bigger, better mixed number, and the remainder idea is the same.
print(hash("cat") % 8) # Python's own hash: a big number, same ideaSystem.out.println(Math.floorMod("cat".hashCode(), 8)); // Java's own hash: a big number, same idea- Work out a slot from a number: take the remainder by the number of slots.
- Work out a slot from a word by hand: add up the character codes, then take the remainder.
- Name the three things that make a hash function good, and say which one is needed for correct answers.