A hashmap stores pairs of keys and values. A key identifies an entry, and its value is the information stored there. For example, "apple" → 3 can record that the word apple appears three times in a document: apple is the key, and 3 is its value.
A map is the set of operations on keys and values. A hash table is one way to implement those operations; other implementations use trees, which keep the keys sorted. This lesson builds the hash-table implementation.
How does hashing work?
So how does a hash map find a key without searching every stored entry? It uses a hash function that computes an integer from the key, and then reduces that integer to one of the buckets of an internal array:
Let’s use a small table. It has buckets, numbered 0 through 7, and an integer key is its own hash, so key goes into bucket k % 8. Key 12 goes to bucket 4, and key 7 to bucket 7. The stored value plays no part in choosing the bucket.