Associative array
In computer science, an associative array is a way of storing information as pairs of a key and a value. It can also be called a key-value store, a map, a symbol table, or a dictionary. Each possible key appears at most once in the collection, so every key points to just one value. In mathematical terms, an associative array is a function with a finite domain. People can look up values, remove them, or insert new ones.
The dictionary problem is a classic challenge in designing efficient data structures that can work as associative arrays. The two main solutions are hash tables and search trees. Sometimes the problem can also be solved 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 them. Content-addressable memory is a form of direct hardware-level support for associative arrays.
Associative arrays have many uses, including important programming patterns such as memoization and the decorator pattern. The name does not come from the associative property known in mathematics. Instead, it comes from the way values are linked with keys. It should not be confused with associative processors.
Operations
In an associative array, the link between a key and a value is often called a "mapping." The same word can also mean the act of creating a new link. Three common operations are insert (or put), which adds a new key and value pair and replaces any older value for that key, remove (or delete), which takes a key and its value out, and lookup (or find or get), which finds the value linked to a key. If no value is found, some systems cause an exception, while others give back a default value, such as zero, null, or a value chosen in advance.
Other operations can count the mappings or use an iterator to go through all of them, and the order of the results is usually decided by how the system is built. A multimap lets one key have several values, while a bidirectional map lets you look up a key from a value as well as a value from a key. In a library example, books can be the keys and patrons the values, showing which person has checked out which book. The notation used in Python or JSON can show this kind of data.
Implementation
For dictionaries with very few mappings, an association list, which is a linked list of mappings, can be used. It is easy to build, and its operations take longer as the number of mappings grows. When keys fall within a narrow range, direct addressing stores each value in an array cell for its key. This is simple and fast, but it needs space for the whole range of keys, so it is only practical when that range is small. The two major approaches are a hash table or a search tree.
A hash table is the most common general-purpose choice. It uses a hash function to sort each key into a "bucket" in an array, and it usually runs quickly. When two keys land in the same bucket, called a collision, the table handles it with separate chaining or open addressing. Search trees, such as an AVL tree or a red–black tree, have a better worst case and keep their items in order, which helps with finding all values between two bounds. Hash tables are usually faster on average, though.
Main article: Hash table
Main article: Search tree
| Underlying data structure | Lookup or Removal | Insertion | Ordered | ||
|---|---|---|---|---|---|
| average | worst case | average | worst case | ||
| Hash table | O(1) | O(n) | O(1) | O(n) | No |
| Self-balancing binary search tree | O(log n) | O(log n) | O(log n) | O(log n) | Yes |
| unbalanced binary search tree | O(log n) | O(n) | O(log n) | O(n) | Yes |
| Sequential container of key–value pairs (e.g. association list) | O(n) | O(n) | O(1) | O(1) | No |
Ordered dictionary
The basic idea of a dictionary does not require any order. To keep items in a fixed order, programmers often use ordered versions of the associative array. There are two kinds. In one kind, the order comes from sorting the keys, as in tree-based versions like the tree map container in C++. In the other kind, the order depends on the order in which items were added, as in the "ordered dictionary" in .NET Framework, and the LinkedHashMap in Java and Python.
The second kind is more common. Ordered dictionaries can be built using an association list, by placing a doubly linked list on top of a normal dictionary, or by moving the data into a dense array that keeps items in the order they were added.
Language support
Main article: Comparison of programming languages (associative array)
Associative arrays can be added to any programming language, and many language systems include them in their standard tools. Some languages have special syntax for them, often written like array subscripts. Built-in support first appeared in 1969 in SNOBOL4, where they were called "table". TMG offered tables with string keys and integer values, MUMPS used multi-dimensional associative arrays as its main data structure, and SETL supported them as one way to build sets and maps. Most modern scripting languages, starting with AWK and including Rexx, Perl, PHP, Tcl, JavaScript, Maple, Python, Ruby, Wolfram Language, Go, and Lua, support associative arrays as a main container type. Many other languages offer them as library functions without special syntax.
Languages use different names for them. In Smalltalk, Objective-C, .NET, Python, REALbasic, Swift, VBA and Delphi they are called dictionaries. In Perl and Ruby they are called hashes. In C++, C#, Java, Go, Clojure, Scala, OCaml, and Haskell they are called maps (see map (C++), unordered_map (C++), and Map). In Common Lisp and Windows PowerShell they are called hash tables, and in Maple and Lua they are called tables. In PHP and R, all arrays can be associative, but keys are limited to integers and strings. In JavaScript (see also JSON), all objects act as associative arrays with string keys, while the Map and WeakMap types accept any objects as keys. In Lua, they are the basic building block for all data structures. In Visual FoxPro, they are called Collections. The D language also supports associative arrays.
Permanent storage
Main article: Key–value store
Many programs that use associative arrays need to save their data in a more permanent form, such as a computer file. A common solution is called archiving or serialization. It turns the objects into a text or binary form that can be written directly to a file. This is usually built into the underlying object model, such as .Net or Cocoa, which includes standard functions that convert the data into text.
For programs that use very large amounts of data, saving individual files is not suitable, so a database management system (DB) is needed. Some DB systems store associative arrays by saving the data along with its key, so each array can be loaded or saved using that key. These key–value stores have been used for many years, almost as long as the more common relational database (RDBs). However, a lack of standardization limited them to certain special uses. RDBs were used for most of these jobs, although saving objects to an RDB can be complicated, a problem known as object-relational impedance mismatch. After about 2010, the need for fast databases suited to cloud computing led to a revival of key–value stores, which can store and retrieve associative arrays directly and greatly improve speed in common web tasks.

Quiz time!
5 questions about Associative array
What is an associative array?
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on Associative array, available under CC BY-SA 4.0.
Images from Wikimedia Commons. Tap any image to view credits and license.