A collection that maps keys to values, similar to Map
, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values:
- a → 1, 2
- b → 3
... or as a single "flattened" collection of key-value pairs:
- a → 1
- a → 2
- b → 3
Important: although the first interpretation resembles how most multimaps are implemented, the design of the Multimap
API is based on the second form. So, using the multimap shown above as an example, the size
is 3
, not 2
, and the values
collection is [1, 2, 3]
, not [[1, 2], [3]]
. For those times when the first style is more useful, use the multimap's asMap
view (or create a Map<K, Collection<V>>
in the first place).
Example
The following code:
ListMultimap<String, String> multimap = ArrayListMultimap.create();
for (President pres : US_PRESIDENTS_IN_ORDER) {
multimap.put(pres.firstName(), pres.lastName());
}
for (String firstName : multimap.keySet()) {
List lastNames = multimap.get(firstName);
out.println(firstName + ": " + lastNames);
}}
... produces output such as:
Zachary: [Taylor]
John: [Adams, Adams, Tyler, Kennedy] // Remember, Quincy!
George: [Washington, Bush, Bush]
Grover: [Cleveland, Cleveland] // Two, non-consecutive terms, rep'ing NJ!
...
Views
Much of the power of the multimap API comes from the view collections it provides. These always reflect the latest state of the multimap itself. When they support modification, the changes are write-through (they automatically update the backing multimap). These view collections are:
asMap
, mentioned abovekeys
,keySet
,values
,entries
, which are similar to the corresponding view collections ofMap
- and, notably, even the collection returned by
get(key)
is an active view of the values corresponding tokey
The collections returned by the replaceValues
and removeAll
methods, which contain values that have just been removed from the multimap, are naturally not views.
Subinterfaces
Instead of using the Multimap
interface directly, prefer the subinterfaces ListMultimap
and SetMultimap
. These take their names from the fact that the collections they return from get
behave like (and, of course, implement) List
and Set
, respectively.
For example, the "presidents" code snippet above used a ListMultimap
; if it had used a SetMultimap
instead, two presidents would have vanished, and last names might or might not appear in chronological order.
Warning: instances of type Multimap
may not implement Object.equals
in the way you expect (multimaps containing the same key-value pairs, even in the same order, may or may not be equal). The recommended subinterfaces provide a much stronger guarantee.
Comparison to a map of collections
Multimaps are commonly used in places where a Map<K, Collection<V>>
would otherwise have appeared. The differences include:
- There is no need to populate an empty collection before adding an entry with
put
. get
never returnsnull
, only an empty collection.- A key is contained in the multimap if and only if it maps to at least one value. Any operation that causes a key to have zero associated values has the effect of removing that key from the multimap.
- The total entry count is available as
size
. - Many complex operations become easier; for example,
Collections.min(multimap.values())
finds the smallest value across all keys.
Implementations
As always, prefer the immutable implementations, ImmutableListMultimap
and ImmutableSetMultimap
. General-purpose mutable implementations are listed above under "All Known Implementing Classes". You can also create a custom multimap, backed by any Map
and Collection
types, using the Multimaps.newMultimap
family of methods. Finally, another popular way to obtain a multimap is using Multimaps.index
. See the Multimaps
class for these and other static utilities related to multimaps.
Other Notes
As with Map
, the behavior of a Multimap
is not specified if key objects already present in the multimap change in a manner that affects equals
comparisons. Use caution if mutable objects are used as keys in a Multimap
.
All methods that modify the multimap are optional. The view collections returned by the multimap may or may not be modifiable. Any modification method that is not supported will throw UnsupportedOperationException
.
See the Guava User Guide article on Multimap
.
- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Jared Levy