HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, a multimap (sometimes also multihash, multidict or multidictionary) is a generalization of a map or associative array abstract data type in which more than one value may be associated with and returned for a given key. Both map and multimap are particular cases of
container A container is any receptacle or enclosure for holding a product used in storage, packaging, and transportation, including shipping. Things kept inside of a container are protected on several sides by being inside of its structure. The term ...
s (for example, see C++ Standard Template Library containers). Often the multimap is implemented as a map with
list A list is a Set (mathematics), set of discrete items of information collected and set forth in some format for utility, entertainment, or other purposes. A list may be memorialized in any number of ways, including existing only in the mind of t ...
s or
set Set, The Set, SET or SETS may refer to: Science, technology, and mathematics Mathematics *Set (mathematics), a collection of elements *Category of sets, the category whose objects and morphisms are sets and total functions, respectively Electro ...
s as the map values.


Examples

* In a student enrollment system, where students may be enrolled in multiple classes simultaneously, there might be an association for each enrollment of a student in a course, where the key is the student ID and the value is the course ID. If a student is enrolled in three courses, there will be three associations containing the same key. * The index of a book may report any number of references for a given index term, and thus may be coded as a multimap from index terms to any number of reference locations or pages. * Querystrings may have multiple values associated with a single field. This is commonly generated when a web form allows multiple check boxes or selections to be chosen in response to a single form element.


Language support


C++

C++'s Standard Template Library provides the multimap
container A container is any receptacle or enclosure for holding a product used in storage, packaging, and transportation, including shipping. Things kept inside of a container are protected on several sides by being inside of its structure. The term ...
for the sorted multimap using a self-balancing binary search tree, and SGI's STL extension provides the hash_multimap container, which implements a multimap using a
hash table In computer science, a hash table is a data structure that implements an associative array, also called a dictionary or simply map; an associative array is an abstract data type that maps Unique key, keys to Value (computer science), values. ...
. As of C++11, the Standard Template Library provides the unordered_multimap for the unordered multimap.


Dart

Quiver provides a Multimap for Dart.


Java

Apache Commons Collections provides a MultiMap interface for
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
. It also provides a MultiValueMap implementing class that makes a MultiMap out of a Map object and a type of Collection. Google Guava provides a Multimap interface and implementations of it.


Kotlin

Kotlin does not have explicit support for multimaps, but can implement them using Maps with containers for the value type. E.g. a Map> can associate each User with a list of Books.


Python

Python provides a collections.defaultdict class that can be used to create a multimap. The user can instantiate the class as collections.defaultdict(list).


OCaml

OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
's standard library module Hashtbl implements a hash table where it's possible to store multiple values for a key.


Scala

The Scala programming language's API also provides Multimap and implementations.


See also

* Multiset for the case where same item can appear several times


References

{{Data structures Associative arrays Abstract data types