K
- the type of keys usedV
- the type of mapped valuespublic interface Multimap<K,V>
Map
that can associate multiple values for keys.
Unlike Map
however, this interface is read-only so the results of access methods such as get(Object)
return a view onto the values associated with that key. The MutableMultimap
subinterface
provides methods to mutate the collection.
The advantages to using this container over a Map<K, Collection<V>>
is that all of the handling of the
value collection can be done automatically. It also allows implementations to further specialize in how duplicate
values will be handled. Value collections with list semantics would allow duplicate values for a key, while those
implementing set semantics would not.
Internal iteration methods for keys and values (singly - forEachKey(Procedure)
, forEachValue(Procedure)
, and together - forEachKeyValue(Procedure2)
) are provided to allow flexible
browsing of the collection's contents. Similarly, views also are provided for keys (keysView()
), values
(valuesView()
) and the combination thereof (keyValuePairsView()
, keyMultiValuePairsView()
).
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(Object key)
Returns
true if any values are mapped to the specified key. |
boolean |
containsKeyAndValue(Object key,
Object value)
Returns
true if the specified key-value pair is mapped. |
boolean |
containsValue(Object value)
Returns
true if any key is mapped to the specified value. |
boolean |
equals(Object obj)
Compares the specified object with this Multimap for equality.
|
void |
forEachKey(Procedure<? super K> procedure)
Calls the
procedure with each key. |
void |
forEachKeyValue(Procedure2<K,V> procedure)
Calls the
procedure with each key-value pair. |
void |
forEachValue(Procedure<? super V> procedure)
Calls the procedure with each value.
|
RichIterable<V> |
get(K key)
Returns a view of all values associated with the given key.
|
int |
hashCode()
Returns the hash code for this Multimap.
|
boolean |
isEmpty()
Returns
true if there are no entries. |
Bag<K> |
keyBag()
Returns a
Bag of keys with the count corresponding to the number of mapped values. |
RichIterable<Pair<K,RichIterable<V>>> |
keyMultiValuePairsView()
Returns a lazy view of the pair of a key and and a lazy view of the values mapped to that key.
|
RichIterable<K> |
keysView()
Returns a lazy view of the unique keys.
|
RichIterable<Pair<K,V>> |
keyValuePairsView()
Returns a lazy view of all of the key/value pairs.
|
RichIterable<RichIterable<V>> |
multiValuesView()
Returns an unmodifiable view of all of the values mapped to each key.
|
Multimap<K,V> |
newEmpty()
Creates a new instance of the same implementation type, using the default capacity and growth parameters.
|
boolean |
notEmpty()
Returns
true if there is at least one entry. |
int |
size()
Returns the number of key-value entry pairs.
|
int |
sizeDistinct()
Returns the number of distinct keys.
|
ImmutableMultimap<K,V> |
toImmutable()
Returns an immutable copy of this Multimap if it is not already immutable.
|
MutableMap<K,RichIterable<V>> |
toMap()
Returns a new
MutableMap of keys from this Multimap to the mapped values as a RichIterable . |
<R extends Collection<V>> |
toMap(Function0<R> collectionFactory)
Returns a new
MutableMap of keys from this Multimap to the mapped values as a RichIterable . |
MutableMultimap<K,V> |
toMutable()
Returns a mutable copy of this Multimap.
|
RichIterable<V> |
valuesView()
Returns a lazy flattened view of all the values.
|
Multimap<K,V> newEmpty()
boolean isEmpty()
true
if there are no entries.boolean notEmpty()
true
if there is at least one entry.void forEachValue(Procedure<? super V> procedure)
{ "key1" : ["val1", "val2", "val2"], "key2" : ["val3"] }
The given procedure would be invoked with the parameters:
[ "val1", "val2", "val2", "val3" ]
void forEachKey(Procedure<? super K> procedure)
procedure
with each key.
Given a Multimap with the contents:
{ "key1" : ["val1", "val2", "val2"], "key2" : ["val3"] }
The given procedure would be invoked with the parameters:
[ "key1", "key2" ]
void forEachKeyValue(Procedure2<K,V> procedure)
procedure
with each key-value pair.
Given a Multimap with the contents:
{ "key1" : ["val1", "val2", "val2"], "key2" : ["val3"] }
The given procedure would be invoked with the parameters:
[ ["key1", "val1"], ["key1", "val2"], ["key1", "val2"], ["key2", "val3"] ]
int size()
int sizeDistinct()
boolean containsKey(Object key)
true
if any values are mapped to the specified key.key
- the key to search forboolean containsValue(Object value)
true
if any key is mapped to the specified value.value
- the value to search forboolean containsKeyAndValue(Object key, Object value)
true
if the specified key-value pair is mapped.key
- the key to search forvalue
- the value to search forRichIterable<V> get(K key)
RichIterable
is returned.key
- the key to search forRichIterable<K> keysView()
Bag<K> keyBag()
Bag
of keys with the count corresponding to the number of mapped values.RichIterable<RichIterable<V>> multiValuesView()
RichIterable<V> valuesView()
RichIterable<Pair<K,RichIterable<V>>> keyMultiValuePairsView()
RichIterable<Pair<K,V>> keyValuePairsView()
MutableMap<K,RichIterable<V>> toMap()
MutableMap
of keys from this Multimap to the mapped values as a RichIterable
.<R extends Collection<V>> MutableMap<K,R> toMap(Function0<R> collectionFactory)
MutableMap
of keys from this Multimap to the mapped values as a RichIterable
.collectionFactory
- used to create the collections that hold the values and affects the return typeboolean equals(Object obj)
toMap()
) are also equal.
In general, two Multimaps with identical key-value mappings may or may not be equal, depending on the type of the collections holding the values. If the backing collections are Sets, then two instances with the same key-value mappings are equal, but if the backing collections are Lists, equality depends on the ordering of the values for each key.
Any two empty Multimaps are equal, because they both have emptytoMap()
views.int hashCode()
The hash code of a Multimap is defined as the hash code of the map view, as returned by toMap()
.
MutableMultimap<K,V> toMutable()
ImmutableMultimap<K,V> toImmutable()
Serializable
if this Multimap is Serializable
.Copyright © 2004–2019. All rights reserved.