public interface MultiValuedMap<K,V>
A MultiValuedMap is a Map with slightly different semantics:
Collection at that key.Collection, holding all the values put to that key.For example:
MultiValuedMap<K, String> map = new MultiValuedHashMap<K, String>(); map.put(key, "A"); map.put(key, "B"); map.put(key, "C"); Collection<String> coll = map.get(key);
coll will be a collection containing "A", "B", "C".
| Modifier and Type | Method and Description |
|---|---|
Map |
asMap()
Returns a view of this multi-valued map as a
Map from each distinct key to the non-empty collection of that key's associated values.
|
void |
clear()
Removes all of the mappings from this map (optional operation).
|
boolean |
containsKey(Object
Returns
true if this map contains a mapping for the specified key.
|
boolean |
containsMapping(Object
Checks whether the map contains a mapping for the specified key and value.
|
boolean |
containsValue(Object
Checks whether the map contains at least one mapping for the specified value.
|
Collection |
entries()
Returns a
Collection view of the mappings contained in this multi-valued map.
|
Collection |
get(K key)
Returns a view collection of the values associated with the specified key.
|
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings.
|
MultiSet |
keys()
Returns a
MultiSet view of the keys contained in this multi-valued map.
|
Set |
keySet()
Returns a
Set view of the keys contained in this multi-valued map.
|
MapIterator |
mapIterator()
Obtains a
MapIterator over this multi-valued map.
|
boolean |
put(K key, V value)
Adds a key-value mapping to this multi-valued map.
|
boolean |
putAll(K key, Iterable
Adds a mapping to the specified key for all values contained in the given Iterable.
|
boolean |
putAll(Map
Copies all mappings from the specified map to this multi-valued map (optional operation).
|
boolean |
putAll(MultiValuedMap
Copies all mappings from the specified map to this multi-valued map (optional operation).
|
Collection |
remove(Object
Removes all values associated with the specified key.
|
boolean |
removeMapping(Object
Removes a key-value mapping from the map.
|
int |
size()
Gets the total size of the map.
|
Collection |
values()
Gets a
Collection view of all values contained in this multi-valued map.
|
int size()
Implementations would return the total size of the map which is the count of the values from all keys.
boolean isEmpty()
true if this map contains no key-value mappings.
true if this map contains no key-value mappings
boolean containsKey(Objectkey)
true if this map contains a mapping for the specified key. More formally, returns
true if and only if this map contains a mapping for a key
k such that
(key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
key - key whose presence in this map is to be tested
NullPointerException - if the specified key is null and this map does not permit null keys (optional)
boolean containsValue(Objectvalue)
value - the value to search for
NullPointerException - if the value is null and null values are not supported by the used collection types (optional)
boolean containsMapping(Objectkey, Object value)
key - the key to search for
value - the value to search for
Collection<V> get(K key)
This method will return an empty collection if containsKey(Object) returns false. Changes to the returned collection will update the underlying MultiValuedMap and vice-versa.
key - the key to retrieve
Collection of values, implementations should return an empty collection for no mapping
NullPointerException - if the key is null and null keys are invalid (optional)
boolean put(K key, V value)
Unlike a normal Map the 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 true if the size of the multi-valued map has been increased because of this operation.
key - the key to store against
value - the value to add to the collection at the key
UnsupportedOperationException - if the put operation is not supported by this multi-valued map, e.g. 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 multi-valued map
boolean putAll(K key, Iterable<? extends V> values)
key - the key to store against
values - the values to add to the collection at the key, may not be null
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)
boolean putAll(Map<? extends K ,? extends V> map)
The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map.
The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
map - mappings to be stored in this map, may not be null
UnsupportedOperationException - if the
putAll operation 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
boolean putAll(MultiValuedMap<? extends K ,? extends V> map)
The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map.
The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
map - mappings to be stored in this map, may not be null
UnsupportedOperationException - if the
putAll operation 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
Collection<V> remove(Object key)
The returned collection may be modifiable, but updates will not be propagated to this multi-valued map. In case no mapping was stored for the specified key, an empty, unmodifiable collection will be returned.
key - the key to remove values from
UnsupportedOperationException - if the map is unmodifiable
NullPointerException - if the key is null and null keys are invalid (optional)
boolean removeMapping(Objectkey, Object item)
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).
key - the key to remove from
item - the item to remove
UnsupportedOperationException - if the map is unmodifiable
NullPointerException - if the key or value is null and null is invalid (optional)
void clear()
The map will be empty after this call returns.
UnsupportedOperationException - if the map is unmodifiable
Collection<Map .Entry <K ,V>> entries()
Collection view of the mappings contained in this multi-valued map.
The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
MultiSet<K> keys()
MultiSet view of the keys contained in this multi-valued map.
The MultiSet method of the returned multiset will give the same result a calling get(Object).size() for the same key.
This multiset is backed by the map, so any changes in the map are reflected in the multiset.
Set<K> keySet()
Set view of the keys contained in this multi-valued 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 remove operation), the result of the iteration is undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
Collection<V> values()
Collection view of all values contained in this multi-valued map.
Implementations typically return a collection containing the combination of values from all keys.
Map<K ,Collection <V>> asMap()
Map from each distinct key to the non-empty collection of that key's associated values.
Note that this.asMap().get(k) is equivalent to this.get(k) only when k is a key contained in the multi-valued map; otherwise it returns null as opposed to an empty collection.
Changes to the returned map or the collections that serve as its values will update the underlying multi-valued map, and vice versa. The map does not support put or putAll, nor do its entries support setValue.
MapIterator<K ,V> mapIterator()
MapIterator over this multi-valued map.
A map iterator is an efficient way of iterating over maps. There is no need to access the entries collection or use Map.Entry objects.