K
- the type of the keys in this mapV
- the type of the values in this mappublic abstract class AbstractMultiValuedMap<K,V> extends Object implements MultiValuedMap<K,V>
MultiValuedMap
interface to simplify
the creation of subclass implementations.
Subclasses specify a Map implementation to use as the internal storage.
Modifier | Constructor and Description |
---|---|
protected |
AbstractMultiValuedMap()
Constructor needed for subclass serialisation.
|
protected |
AbstractMultiValuedMap(Map<K,? extends Collection<V>> map)
Constructor that wraps (not copies).
|
Modifier and Type | Method and Description |
---|---|
Map<K,Collection<V>> |
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 key)
Returns
true if this map contains a mapping for the specified
key. |
boolean |
containsMapping(Object key,
Object value)
Checks whether the map contains a mapping for the specified key and value.
|
boolean |
containsValue(Object value)
Checks whether the map contains at least one mapping for the specified value.
|
protected abstract Collection<V> |
createCollection() |
protected void |
doReadObject(ObjectInputStream in)
Read the map in using a custom routine.
|
protected void |
doWriteObject(ObjectOutputStream out)
Write the map out using a custom routine.
|
Collection<Map.Entry<K,V>> |
entries()
Returns a
Collection view of the mappings contained in this multi-valued map. |
boolean |
equals(Object obj) |
Collection<V> |
get(K key)
Gets the collection of values associated with the specified key.
|
protected Map<K,? extends Collection<V>> |
getMap()
Gets the map being wrapped.
|
int |
hashCode() |
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
MultiSet<K> |
keys()
Returns a
MultiSet view of the key mapping contained in this map. |
Set<K> |
keySet()
Returns a
Set view of the keys contained in this multi-valued map. |
MapIterator<K,V> |
mapIterator()
Obtains a
MapIterator over this multi-valued map. |
boolean |
put(K key,
V value)
Adds the value to the collection associated with the specified key.
|
boolean |
putAll(K key,
Iterable<? extends V> values)
Adds Iterable values to the collection associated with the specified key.
|
boolean |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map.
|
boolean |
putAll(MultiValuedMap<? extends K,? extends V> map)
Copies all of the mappings from the specified MultiValuedMap to this map.
|
Collection<V> |
remove(Object key)
Removes all values associated with the specified key.
|
boolean |
removeMapping(Object key,
Object value)
Removes a specific key/value mapping from the multi-valued map.
|
protected void |
setMap(Map<K,? extends Collection<V>> map)
Sets the map being wrapped.
|
int |
size()
Gets the total size of the map.
|
String |
toString() |
Collection<V> |
values()
Gets a collection containing all the values in the map.
|
protected AbstractMultiValuedMap()
protected AbstractMultiValuedMap(Map<K,? extends Collection<V>> map)
map
- the map to wrap, must not be nullNullPointerException
- if the map is nullprotected Map<K,? extends Collection<V>> getMap()
protected void setMap(Map<K,? extends Collection<V>> map)
NOTE: this method should only be used during deserialization
map
- the map to wrapprotected abstract Collection<V> createCollection()
public boolean containsKey(Object key)
MultiValuedMap
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.)containsKey
in interface MultiValuedMap<K,V>
key
- key whose presence in this map is to be testedpublic boolean containsValue(Object value)
MultiValuedMap
containsValue
in interface MultiValuedMap<K,V>
value
- the value to search forpublic boolean containsMapping(Object key, Object value)
MultiValuedMap
containsMapping
in interface MultiValuedMap<K,V>
key
- the key to search forvalue
- the value to search forpublic Collection<Map.Entry<K,V>> entries()
MultiValuedMap
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.
entries
in interface MultiValuedMap<K,V>
public Collection<V> get(K key)
get
in interface MultiValuedMap<K,V>
key
- the key to retrieveCollection
of values, will return an empty Collection
for no mappingpublic Collection<V> remove(Object key)
A subsequent get(Object)
would return an empty collection.
remove
in interface MultiValuedMap<K,V>
key
- the key to remove values fromCollection
of values removed, will return an
empty, unmodifiable collection for no mapping foundpublic boolean removeMapping(Object key, Object value)
The value 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, an empty collection would be
returned from a subsequent get(Object)
.
removeMapping
in interface MultiValuedMap<K,V>
key
- the key to remove fromvalue
- the value to removepublic boolean isEmpty()
MultiValuedMap
true
if this map contains no key-value mappings.isEmpty
in interface MultiValuedMap<K,V>
true
if this map contains no key-value mappingspublic Set<K> keySet()
MultiValuedMap
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.
keySet
in interface MultiValuedMap<K,V>
public int size()
Implementations would return the total size of the map which is the count of the values from all keys.
This implementation does not cache the total size of the multi-valued map, but rather calculates it by iterating over the entries of the underlying map.
size
in interface MultiValuedMap<K,V>
public Collection<V> values()
Returns a collection containing all the values from all keys.
values
in interface MultiValuedMap<K,V>
public void clear()
MultiValuedMap
The map will be empty after this call returns.
clear
in interface MultiValuedMap<K,V>
public 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.
put
in interface MultiValuedMap<K,V>
key
- the key to store againstvalue
- the value to add to the collection at the keypublic boolean putAll(Map<? extends K,? extends V> map)
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.putAll
in interface MultiValuedMap<K,V>
map
- mappings to be stored in this map, may not be nullNullPointerException
- if map is nullpublic boolean putAll(MultiValuedMap<? extends K,? extends V> map)
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.putAll
in interface MultiValuedMap<K,V>
map
- mappings to be stored in this map, may not be nullNullPointerException
- if map is nullpublic MultiSet<K> keys()
MultiSet
view of the key mapping contained in this map.
Returns a MultiSet of keys with its values count as the count of the MultiSet.
This multiset is backed by the map, so any changes in the map is reflected here.
Any method which modifies this multiset like add
, remove
,
Iterator.remove()
etc throws UnsupportedOperationException
.
keys
in interface MultiValuedMap<K,V>
public Map<K,Collection<V>> asMap()
MultiValuedMap
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
.
asMap
in interface MultiValuedMap<K,V>
public boolean putAll(K key, Iterable<? extends V> values)
putAll
in interface MultiValuedMap<K,V>
key
- the key to store againstvalues
- the values to add to the collection at the key, may not be nullNullPointerException
- if values is nullpublic MapIterator<K,V> mapIterator()
MultiValuedMap
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.
mapIterator
in interface MultiValuedMap<K,V>
protected void doWriteObject(ObjectOutputStream out) throws IOException
out
- the output streamIOException
- any of the usual I/O related exceptionsprotected void doReadObject(ObjectInputStream in) throws IOException, ClassNotFoundException
in
- the input streamIOException
- any of the usual I/O related exceptionsClassNotFoundException
- if the stream contains an object which class can not be loadedClassCastException
- if the stream does not contain the correct objectsCopyright © 2001–2019 The Apache Software Foundation. All rights reserved.