Hash Maps

Jaskomal
2 min readAug 2, 2021

Hash maps are a data structure that have a key value pair. With hash maps we don’t really care bout the exact sequence of the data. We only care that a given input, when fed into the map, gives the accurate output. Imagine we want our computer to remember that our good friend John is a Leo. We take his name and we turn that name into a number. Let’s say that the number we correspond with the name John is 47. We find the 47th index of the array we’re using to store our map and save the value (Leo) there. How did we get 47, though? We use a special function that turns data like the string “John” into a number. This function is called a hashing function, or a hash function. Hashing functions are useful in many domains, but for our data structure the most important aspect is that a hashing function returns an array index as output.

A hash function takes a string or some data as input and returns an array index as output. In order for it to return an array index, our hash map implementation needs to know the size of our array. If the array we are saving values into only has 4 slots, our hash map’s hashing method should not return an index bigger than that.

It is actually a defining feature of all hash functions that they greatly reduce any possible inputs (any string you can imagine) into a much smaller range of potential outputs (an integer smaller than the size of our array). For this reason, hash functions are also known as compression functions.

--

--