Interface MultiValuedMap<K,V> 
- Type Parameters:
- K- the type of the keys in this map
- V- the type of the values in this map
- All Known Subinterfaces:
- ListValuedMap<K,,- V> - SetValuedMap<K,- V> 
- All Known Implementing Classes:
- AbstractListValuedMap,- AbstractMultiValuedMap,- AbstractMultiValuedMapDecorator,- AbstractSetValuedMap,- ArrayListValuedHashMap,- ArrayListValuedLinkedHashMap,- HashSetValuedHashMap,- LinkedHashSetValuedLinkedHashMap,- TransformedMultiValuedMap,- UnmodifiableMultiValuedMap
 A MultiValuedMap is a Map with slightly different semantics:
 
- Putting a value into the map will add the value to a Collectionat that key.
- Getting a value will return a Collection, holding all the values put to that key.
For example:
 MultiValuedMap<Integer, String> map = new ArrayListValuedHashMap<>();
 map.put(1, "A");
 map.put(1, "B");
 map.put(1, "C");
 Collection<String> coll = map.get(1);
 
 coll will be a collection containing "A", "B", "C".
 
- Since:
- 4.1
- 
Method SummaryModifier and TypeMethodDescriptionMap<K, Collection<V>> asMap()Returns a view of this multivalued map as aMapfrom each distinct key to the non-empty collection of that key's associated values.voidclear()Removes all of the mappings from this map (optional operation).booleancontainsKey(Object key) Returnstrueif this map contains a mapping for the specified key.booleancontainsMapping(Object key, Object value) Checks whether the map contains a mapping for the specified key and value.booleancontainsValue(Object value) Checks whether the map contains at least one mapping for the specified value.entries()Returns aCollectionview of the mappings contained in this multivalued map.Gets a view collection of the values associated with the specified key.booleanisEmpty()Returnstrueif this map contains no key-value mappings.keys()Returns aMultiSetview of the keys contained in this multivalued map.keySet()Returns aSetview of the keys contained in this multivalued map.Obtains aMapIteratorover this multivalued map.booleanAdds a key-value mapping to this multivalued map.booleanCopies all mappings from the specified map to this multivalued map (optional operation).booleanAdds a mapping to the specified key for all values contained in the given Iterable.booleanputAll(MultiValuedMap<? extends K, ? extends V> map) Copies all mappings from the specified map to this multivalued map (optional operation).Removes all values associated with the specified key.booleanremoveMapping(Object key, Object item) Removes a key-value mapping from the map.intsize()Gets the total size of the map.values()Gets aCollectionview of all values contained in this multivalued map.
- 
Method Details- 
asMapMap<K,Collection<V>> asMap()Returns a view of this multivalued map as aMapfrom each distinct key to the non-empty collection of that key's associated values.Note that this.asMap().get(k)is equivalent tothis.get(k)only whenkis a key contained in the multivalued map; otherwise it returnsnullas opposed to an empty collection.Changes to the returned map or the collections that serve as its values will update the underlying multivalued map, and vice versa. The map does not support putorputAll, nor do its entries supportsetValue.- Returns:
- a map view of the mappings in this multivalued map
 
- 
clearvoid clear()Removes all of the mappings from this map (optional operation).The map will be empty after this call returns. - Throws:
- UnsupportedOperationException- if the map is unmodifiable
 
- 
containsKeyReturnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains a mapping for a keyksuch that(key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)- Parameters:
- key- key whose presence in this map is to be tested
- Returns:
- true if this map contains a mapping for the specified key
- Throws:
- NullPointerException- if the specified key is null and this map does not permit null keys (optional)
 
- 
containsMappingChecks whether the map contains a mapping for the specified key and value.- Parameters:
- key- the key to search for
- value- the value to search for
- Returns:
- true if the map contains the value
 
- 
containsValueChecks whether the map contains at least one mapping for the specified value.- Parameters:
- value- the value to search for
- Returns:
- true if the map contains the value
- Throws:
- NullPointerException- if the value is null and null values are not supported by the used collection types (optional)
 
- 
entriesCollection<Map.Entry<K,V>> entries()Returns aCollectionview of the mappings contained in this multivalued map.The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. - Returns:
- a set view of the mappings contained in this map
 
- 
getGets a view collection of the values associated with the specified key.This method will return an empty collection if containsKey(Object)returnsfalse. Changes to the returned collection will update the underlyingMultiValuedMapand vice-versa.- Parameters:
- key- the key to retrieve
- Returns:
- the Collectionof values, implementations should return an empty collection for no mapping
- Throws:
- NullPointerException- if the key is null and null keys are invalid (optional)
 
- 
isEmptyboolean isEmpty()Returnstrueif this map contains no key-value mappings.- Returns:
- trueif this map contains no key-value mappings
 
- 
keysReturns aMultiSetview of the keys contained in this multivalued map.The MultiSet.getCount(Object)method of the returned multiset will give the same result a callingget(Object).size()for the same key.This multiset is backed by the map, so any changes in the map are reflected in the multiset. - Returns:
- a multiset view of the keys contained in this map
 
- 
keySetReturns aSetview of the keys contained in this multivalued map.The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own removeoperation), the result of the iteration is undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations.- Returns:
- a set view of the keys contained in this map
 
- 
mapIteratorMapIterator<K,V> mapIterator()Obtains aMapIteratorover this multivalued map.A map iterator is an efficient way of iterating over maps. There is no need to access the entries collection or use Map.Entryobjects.- Returns:
- a map iterator
 
- 
putAdds a key-value mapping to this multivalued map.Unlike a normal Mapthe previous value is not replaced. Instead, the new value is added to the collection stored against the key. Depending on the collection type used, duplicate key-value mappings may be allowed.The method will return trueif the size of the multivalued map has been increased because of this operation.- Parameters:
- key- the key to store against
- value- the value to add to the collection at the key
- Returns:
- true if the map changed as a result of this put operation, or false if the map already contained the key-value mapping and the collection type does not allow duplicate values, for example when using a Set
- Throws:
- UnsupportedOperationException- if the put operation is not supported by this multivalued map, for example if it is unmodifiable
- NullPointerException- if the key or value is null and null is invalid (optional)
- IllegalArgumentException- if some aspect of the specified key or value prevents it from being stored in this multivalued map
 
- 
putAllAdds a mapping to the specified key for all values contained in the given Iterable.- Parameters:
- key- the key to store against
- values- the values to add to the collection at the key, may not be null
- Returns:
- true if the map changed as a result of this operation
- Throws:
- NullPointerException- if the specified iterable is null, or if this map does not permit null keys or values, and the specified key or values contain null (optional)
 
- 
putAllCopies all mappings from the specified map to this multivalued map (optional operation).The effect of this call is equivalent to that of calling put(k, v)on this map once for each mapping from keykto valuevin the specified map.The behavior of this operation is undefined if the specified map is modified while the operation is in progress. - Parameters:
- map- mappings to be stored in this map, may not be null
- Returns:
- true if the map changed as a result of this operation
- Throws:
- UnsupportedOperationException- if the- putAlloperation is not supported by this map
- NullPointerException- if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values (optional)
- IllegalArgumentException- if some property of a key or value in the specified map prevents it from being stored in this map
 
- 
putAllCopies all mappings from the specified map to this multivalued map (optional operation).The effect of this call is equivalent to that of calling put(k, v)on this map once for each mapping from keykto valuevin the specified map.The behavior of this operation is undefined if the specified map is modified while the operation is in progress. - Parameters:
- map- mappings to be stored in this map, may not be null
- Returns:
- true if the map changed as a result of this operation
- Throws:
- UnsupportedOperationException- if the- putAlloperation is not supported by this map
- NullPointerException- if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values (optional)
- IllegalArgumentException- if some property of a key or value in the specified map prevents it from being stored in this map
 
- 
removeRemoves all values associated with the specified key.The returned collection may be modifiable, but updates will not be propagated to this multivalued map. In case no mapping was stored for the specified key, an empty, unmodifiable collection will be returned. - Parameters:
- key- the key to remove values from
- Returns:
- the values that were removed
- Throws:
- UnsupportedOperationException- if the map is unmodifiable
- NullPointerException- if the key is null and null keys are invalid (optional)
 
- 
removeMappingRemoves a key-value mapping from the map.The item is removed from the collection mapped to the specified key. Other values attached to that key are unaffected. If the last value for a key is removed, implementations typically return an empty collection from a subsequent get(Object).- Parameters:
- key- the key to remove from
- item- the item to remove
- Returns:
- true if the mapping was removed, false otherwise
- Throws:
- UnsupportedOperationException- if the map is unmodifiable
- NullPointerException- if the key or value is null and null is invalid (optional)
 
- 
sizeint size()Gets the total size of the map.Implementations would return the total size of the map which is the count of the values from all keys. - Returns:
- the total size of the map
 
- 
valuesCollection<V> values()Gets aCollectionview of all values contained in this multivalued map.Implementations typically return a collection containing the combination of values from all keys. - Returns:
- a collection view of the values contained in this multivalued map
 
 
-