Lably
Skip to content

Associative array

Data structure holding key/value pairs

  • 10 min read
  • 5 chapters

Related on Lably

Through Computer science, Function (mathematics) and Domain of a function

The dictionary problem is the classic problem of designing efficient data structures that implement associative arrays. The two major solutions to the dictionary problem are hash tables and search trees. It is sometimes also possible to solve the problem using directly addressed arrays, binary search trees, or other more specialized structures.

Many programming languages include associative arrays as primitive data types, while many other languages provide software libraries that support associative arrays. Content-addressable memory is a form of direct hardware-level support for associative arrays.

Associative arrays have many applications including such fundamental programming patterns as memoization and the decorator pattern. The name does not come from the associative property known in mathematics. Rather, it arises from the association of values with keys. It should not be confused with associative processors.

Operations

In an associative array, the association between a key and a value is often known as a "mapping"; the same word may also be used to refer to the process of creating a new association.

The operations that are usually defined for an associative array are:

Insert or put
add a new (key,value) pair to the collection, mapping the key to its new value. Any existing mapping is overwritten. The arguments to this operation are the key and the value.
Remove or delete
remove a (key,value) pair from the collection, unmapping a given key from its value. The argument to this operation is the key.
Lookup, find, or get
find the value (if any) that is bound to a given key. The argument to this operation is the key, and the value is returned from the operation. If no value is found, some lookup functions raise an exception, while others return a default value (such as zero, null, or a specific value passed to the constructor).

Associative arrays may also include other operations such as determining the number of mappings or constructing an iterator to loop over all the mappings. For such operations, the order in which the mappings are returned is usually implementation-defined.

A multimap generalizes an associative array by allowing multiple values to be associated with a single key. A bidirectional map is a related abstract data type in which the mappings operate in both directions: each value must be associated with a unique key, and a second lookup operation takes a value as an argument and looks up the key associated with that value.

Properties

The operations of the associative array should satisfy various properties:

  • lookup(k, insert(j, v, D)) = if k == j then v else lookup(k, D)
  • lookup(k, new()) = fail, where fail is an exception or default value
  • remove(k, insert(j, v, D)) = if k == j then remove(k, D) else insert(j, v, remove(k, D))
  • remove(k, new()) = new()

where k and j are keys, v is a value, D is an associative array, and new() creates a new, empty associative array.

Example

Suppose that the set of loans made by a library is represented in a data structure. Each book in a library may be checked out by one patron at a time. However, a single patron may be able to check out multiple books. Therefore, the information about which books are checked out to which patrons may be represented by an associative array, in which the books are the keys and the patrons are the values. Using notation from Python or JSON, the data structure would be:

<span id="mwgg"></span><span class="p" id="mwgw">{</span> <span class="w" id="mwhA"> </span><span class="s2" id="mwhQ">"Pride and Prejudice"</span><span class="o" id="mwhg">:</span><span class="w" id="mwhw"> </span><span class="s2" id="mwiA">"Alice"</span><span class="p" id="mwiQ">,</span> <span class="w" id="mwig"> </span><span class="s2" id="mwiw">"Wuthering Heights"</span><span class="o" id="mwjA">:</span><span class="w" id="mwjQ"> </span><span class="s2" id="mwjg">"Alice"</span><span class="p" id="mwjw">,</span> <span class="w" id="mwkA"> </span><span class="s2" id="mwkQ">"Great Expectations"</span><span class="o" id="mwkg">:</span><span class="w" id="mwkw"> </span><span class="s2" id="mwlA">"John"</span> <span class="p" id="mwlQ">}</span>

A lookup operation on the key "Great Expectations" would return "John". If John returns his book, that would cause a deletion operation, and if Pat checks out a book, that would cause an insertion operation, leading to a different state:

<span id="mwmQ"></span><span class="p" id="mwmg">{</span> <span class="w" id="mwmw"> </span><span class="s2" id="mwnA">"Pride and Prejudice"</span><span class="o" id="mwnQ">:</span><span class="w" id="mwng"> </span><span class="s2" id="mwnw">"Alice"</span><span class="p" id="mwoA">,</span> <span class="w" id="mwoQ"> </span><span class="s2" id="mwog">"The Brothers Karamazov"</span><span class="o" id="mwow">:</span><span class="w" id="mwpA"> </span><span class="s2" id="mwpQ">"Pat"</span><span class="p" id="mwpg">,</span> <span class="w" id="mwpw"> </span><span class="s2" id="mwqA">"Wuthering Heights"</span><span class="o" id="mwqQ">:</span><span class="w" id="mwqg"> </span><span class="s2" id="mwqw">"Alice"</span> <span class="p" id="mwrA">}</span>

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Theoretical computer science