Class MultiValueMap<K,V>
- java.lang.Object
-
- org.apache.commons.collections4.map.AbstractIterableMap<K,V>
-
- org.apache.commons.collections4.map.AbstractMapDecorator<K,java.lang.Object>
-
- org.apache.commons.collections4.map.MultiValueMap<K,V>
-
- Type Parameters:
K
- the type of the keys in this mapV
- the type of the values in this map
- All Implemented Interfaces:
java.io.Serializable
,java.util.Map<K,java.lang.Object>
,Get<K,java.lang.Object>
,IterableGet<K,java.lang.Object>
,IterableMap<K,java.lang.Object>
,MultiMap<K,V>
,Put<K,java.lang.Object>
@Deprecated public class MultiValueMap<K,V> extends AbstractMapDecorator<K,java.lang.Object> implements MultiMap<K,V>, java.io.Serializable
Deprecated.since 4.1, useMultiValuedMap
insteadA MultiValueMap decorates another map, allowing it to have more than one value for a key.A
MultiMap
is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.This implementation is a decorator, allowing any Map implementation to be used as the base.
In addition, this implementation allows the type of collection used for the values to be controlled. By default, an
ArrayList
is used, however aClass
to instantiate may be specified, or a factory that returns aCollection
instance.Note that MultiValueMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. This class may throw exceptions when accessed by concurrent threads without synchronization.
- Since:
- 3.2
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MultiValueMap()
Deprecated.Creates a MultiValueMap based on aHashMap
and storing the multiple values in anArrayList
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clear()
Deprecated.Clear the map.boolean
containsValue(java.lang.Object value)
Deprecated.Checks whether the map contains the value specified.boolean
containsValue(java.lang.Object key, java.lang.Object value)
Deprecated.Checks whether the collection at the specified key contains the value.java.util.Set<java.util.Map.Entry<K,java.lang.Object>>
entrySet()
Deprecated.java.util.Collection<V>
getCollection(java.lang.Object key)
Deprecated.Gets the collection mapped to the specified key.java.util.Iterator<java.util.Map.Entry<K,V>>
iterator()
Deprecated.Gets an iterator for all mappings stored in thisMultiValueMap
.java.util.Iterator<V>
iterator(java.lang.Object key)
Deprecated.Gets an iterator for the collection mapped to the specified key.static <K,V,C extends java.util.Collection<V>>
MultiValueMap<K,V>multiValueMap(java.util.Map<K,? super C> map, java.lang.Class<C> collectionClass)
Deprecated.Creates a map which decorates the givenmap
and maps keys to collections of typecollectionClass
.static <K,V,C extends java.util.Collection<V>>
MultiValueMap<K,V>multiValueMap(java.util.Map<K,? super C> map, Factory<C> collectionFactory)
Deprecated.Creates a map which decorates the givenmap
and creates the value collections using the suppliedcollectionFactory
.static <K,V>
MultiValueMap<K,V>multiValueMap(java.util.Map<K,? super java.util.Collection<V>> map)
Deprecated.Creates a map which wraps the given map and maps keys to ArrayLists.java.lang.Object
put(K key, java.lang.Object value)
Deprecated.Adds the value to the collection associated with the specified key.void
putAll(java.util.Map<? extends K,?> map)
Deprecated.Override superclass to ensure that MultiMap instances are correctly handled.boolean
putAll(K key, java.util.Collection<V> values)
Deprecated.Adds a collection of values to the collection associated with the specified key.boolean
removeMapping(java.lang.Object key, java.lang.Object value)
Deprecated.Removes a specific value from map.int
size(java.lang.Object key)
Deprecated.Gets the size of the collection mapped to the specified key.int
totalSize()
Deprecated.Gets the total size of the map by counting all the values.java.util.Collection<java.lang.Object>
values()
Deprecated.Gets a collection containing all the values in the map.-
Methods inherited from class org.apache.commons.collections4.map.AbstractMapDecorator
containsKey, equals, get, hashCode, isEmpty, keySet, remove, size, toString
-
Methods inherited from class org.apache.commons.collections4.map.AbstractIterableMap
mapIterator
-
Methods inherited from interface org.apache.commons.collections4.Get
containsKey, isEmpty, keySet
-
Methods inherited from interface org.apache.commons.collections4.IterableGet
mapIterator
-
-
-
-
Method Detail
-
multiValueMap
public static <K,V> MultiValueMap<K,V> multiValueMap(java.util.Map<K,? super java.util.Collection<V>> map)
Deprecated.Creates a map which wraps the given map and maps keys to ArrayLists.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
map
- the map to wrap- Returns:
- a new multi-value map
- Since:
- 4.0
-
multiValueMap
public static <K,V,C extends java.util.Collection<V>> MultiValueMap<K,V> multiValueMap(java.util.Map<K,? super C> map, java.lang.Class<C> collectionClass)
Deprecated.Creates a map which decorates the givenmap
and maps keys to collections of typecollectionClass
.- Type Parameters:
K
- the key typeV
- the value typeC
- the collection class type- Parameters:
map
- the map to wrapcollectionClass
- the type of the collection class- Returns:
- a new multi-value map
- Since:
- 4.0
-
multiValueMap
public static <K,V,C extends java.util.Collection<V>> MultiValueMap<K,V> multiValueMap(java.util.Map<K,? super C> map, Factory<C> collectionFactory)
Deprecated.Creates a map which decorates the givenmap
and creates the value collections using the suppliedcollectionFactory
.- Type Parameters:
K
- the key typeV
- the value typeC
- the collection class type- Parameters:
map
- the map to decoratecollectionFactory
- the collection factory (must return a Collection object).- Returns:
- a new multi-value map
- Since:
- 4.0
-
clear
public void clear()
Deprecated.Clear the map.
-
removeMapping
public boolean removeMapping(java.lang.Object key, java.lang.Object value)
Deprecated.Removes a specific value from 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,
null
will be returned from a subsequentget(key)
.- Specified by:
removeMapping
in interfaceMultiMap<K,V>
- Parameters:
key
- the key to remove fromvalue
- the value to remove- Returns:
true
if the mapping was removed,false
otherwise
-
containsValue
public boolean containsValue(java.lang.Object value)
Deprecated.Checks whether the map contains the value specified.This checks all collections against all keys for the value, and thus could be slow.
- Specified by:
containsValue
in interfaceGet<K,V>
- Specified by:
containsValue
in interfacejava.util.Map<K,V>
- Specified by:
containsValue
in interfaceMultiMap<K,V>
- Overrides:
containsValue
in classAbstractMapDecorator<K,java.lang.Object>
- Parameters:
value
- the value to search for- Returns:
- true if the map contains the value
- See Also:
Map.containsValue(Object)
-
put
public java.lang.Object put(K key, java.lang.Object value)
Deprecated.Adds the value to the collection associated with the specified key.Unlike a normal
Map
the previous value is not replaced. Instead the new value is added to the collection stored against the key.- Specified by:
put
in interfacejava.util.Map<K,V>
- Specified by:
put
in interfaceMultiMap<K,V>
- Specified by:
put
in interfacePut<K,V>
- Overrides:
put
in classAbstractMapDecorator<K,java.lang.Object>
- Parameters:
key
- the key to store againstvalue
- the value to add to the collection at the key- Returns:
- the value added if the map changed and null if the map did not change
- See Also:
Map.put(Object, Object)
-
putAll
public void putAll(java.util.Map<? extends K,?> map)
Deprecated.Override superclass to ensure that MultiMap instances are correctly handled.If you call this method with a normal map, each entry is added using
put(Object,Object)
. If you call this method with a multi map, each entry is added usingputAll(Object,Collection)
.
-
entrySet
public java.util.Set<java.util.Map.Entry<K,java.lang.Object>> entrySet()
Deprecated.NOTE: the returned Entry objects will contain as value a
Collection
of all values that are mapped to the given key. To get a "flattened" version of all mappings contained in this map, useiterator()
.- Specified by:
entrySet
in interfaceGet<K,V>
- Specified by:
entrySet
in interfacejava.util.Map<K,V>
- Overrides:
entrySet
in classAbstractMapDecorator<K,java.lang.Object>
- Returns:
- a set view of the mappings contained in this map
- See Also:
iterator()
-
values
public java.util.Collection<java.lang.Object> values()
Deprecated.Gets a collection containing all the values in the map.This returns a collection containing the combination of values from all keys.
- Specified by:
values
in interfaceGet<K,V>
- Specified by:
values
in interfacejava.util.Map<K,V>
- Specified by:
values
in interfaceMultiMap<K,V>
- Overrides:
values
in classAbstractMapDecorator<K,java.lang.Object>
- Returns:
- a collection view of the values contained in this map
- See Also:
Map.values()
-
containsValue
public boolean containsValue(java.lang.Object key, java.lang.Object value)
Deprecated.Checks whether the collection at the specified key contains the value.- Parameters:
key
- the key to search forvalue
- the value to search for- Returns:
- true if the map contains the value
-
getCollection
public java.util.Collection<V> getCollection(java.lang.Object key)
Deprecated.Gets the collection mapped to the specified key. This method is a convenience method to typecast the result ofget(key)
.- Parameters:
key
- the key to retrieve- Returns:
- the collection mapped to the key, null if no mapping
-
size
public int size(java.lang.Object key)
Deprecated.Gets the size of the collection mapped to the specified key.- Parameters:
key
- the key to get size for- Returns:
- the size of the collection at the key, zero if key not in map
-
putAll
public boolean putAll(K key, java.util.Collection<V> values)
Deprecated.Adds a collection of values to the collection associated with the specified key.- Parameters:
key
- the key to store againstvalues
- the values to add to the collection at the key, null ignored- Returns:
- true if this map changed
-
iterator
public java.util.Iterator<V> iterator(java.lang.Object key)
Deprecated.Gets an iterator for the collection mapped to the specified key.- Parameters:
key
- the key to get an iterator for- Returns:
- the iterator of the collection at the key, empty iterator if key not in map
-
iterator
public java.util.Iterator<java.util.Map.Entry<K,V>> iterator()
Deprecated.Gets an iterator for all mappings stored in thisMultiValueMap
.The iterator will return multiple Entry objects with the same key if there are multiple values mapped to this key.
NOTE: calling
Map.Entry.setValue(Object)
on any of the returned elements will result in aUnsupportedOperationException
.- Returns:
- the iterator of all mappings in this map
- Since:
- 4.0
-
totalSize
public int totalSize()
Deprecated.Gets the total size of the map by counting all the values.- Returns:
- the total size of the map counting all values
-
-