What is a map, and what rule makes it different from a plain list of pairs?
A map stores key-value entries and is searched by key; each key appears at most once, so a key identifies exactly one entry.
A map (also called an associative array or dictionary in many languages) is a collection of entries (k, v). You do not ask it for "the third element"; you ask it for "the value stored under key k". Its main operations are therefore insert, search and delete, all addressed by key.
The defining rule is one entry per key. Storing a second value under an existing key does not add a second entry; it replaces the old value. That is what lets a map answer get(k) with a single value instead of a list.
Typical uses are small databases where one field identifies a record:
- a staff directory keyed by staff number,
- a directory of places keyed by postal code,
- a symbol table in a compiler, keyed by identifier name.
Values, unlike keys, may repeat: two employees can share a salary, two postal codes can share a town name.
Compared with a priority queue: a priority queue only ever hands out its minimum and may hold the same key several times. A map is built for searching any key, and its keys are unique like IDs. Searching a heap for an arbitrary key would take $O(n)$, because heap order only runs from each leaf up to the root and says nothing about left versus right.
Go deeper:
Associative array โ the abstract data type behind maps and dictionaries, and the usual ways to implement it.