ivangeorgiev.dev

Introduction to data structures: Map

Learn about the map data structure in an intuitive way

November 19th, 2024 3 minute read

The map data structure is also know as "dictionary" and "associative array".
It's a really useful data structure that uses keys to store and retrieve values (i.e. it maps keys to values). Another way to look at it is that it associates keys with values (hence the name "associative array").
Let's learn what the map data structure is and how it works.

Let's give a few examples of using maps in our daily lifes:

The map data structure usually provides the following operations:

A map's key and value can be of any data type (e.g. number, string, object).
If you remember, an array provides access to elements using indices (i.e. integers starting from zero). The indices are basically the array's keys and we can use them to get the corresponding value.
In contrast, the map data structure allows us to use any data type as a key. Of course, this comes with a price. Arrays are faster than maps in terms of data access.

The map data structure is useful when there is a large dataset that needs to be accessed efficiently. Let's give a few examples:

How do maps work internally?
The map data structure usually uses an array internally. When a key is provided to put/get/remove a value, the map converts the key to an array index. It then uses this index to access the internal array. The operation of converting the key to an array index is called hashing and these types of maps are called hash maps.
There are other types of maps as well and maybe we will cover some of them in future articles.

If you enjoy my articles, please consider supporting me.