Class MultiValueMap
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 a Class
to instantiate may be specified,
or a factory that returns a Collection
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:
- Commons Collections 3.2
- Version:
- $Revision: 1713175 $ $Date: 2015-11-07 21:47:04 +0100 (Sat, 07 Nov 2015) $
- Author:
- James Carman, Christopher Berry, James Strachan, Steve Downey, Stephen Colebourne, Julien Buret, Serhiy Yevtushenko
-
Nested Class Summary
-
Field Summary
Fields inherited from class org.apache.commons.collections.map.AbstractMapDecorator
map
-
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a MultiValueMap based on aHashMap
and storing the multiple values in anArrayList
.protected
MultiValueMap
(Map map, Factory collectionFactory) Creates a MultiValueMap which decorates the givenmap
and creates the value collections using the suppliedcollectionFactory
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the map.boolean
containsValue
(Object value) Checks whether the map contains the value specified.boolean
containsValue
(Object key, Object value) Checks whether the collection at the specified key contains the value.protected Collection
createCollection
(int size) Creates a new instance of the map value Collection container using the factory.static MultiValueMap
Creates a map which wraps the given map and maps keys to ArrayLists.static MultiValueMap
Creates a map which decorates the givenmap
and maps keys to collections of typecollectionClass
.static MultiValueMap
Creates a map which decorates the givenmap
and creates the value collections using the suppliedcollectionFactory
.getCollection
(Object key) Gets the collection mapped to the specified key.Gets an iterator for the collection mapped to the specified key.Adds the value to the collection associated with the specified key.boolean
putAll
(Object key, Collection values) Adds a collection of values to the collection associated with the specified key.void
Override superclass to ensure that MultiMap instances are correctly handled.removeMapping
(Object key, Object value) Removes a specific value from map.int
Gets the size of the collection mapped to the specified key.int
Gets the total size of the map by counting all the values.values()
Gets a collection containing all the values in the map.Methods inherited from class org.apache.commons.collections.map.AbstractMapDecorator
containsKey, entrySet, equals, get, getMap, hashCode, isEmpty, keySet, remove, size, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
MultiValueMap
public MultiValueMap()Creates a MultiValueMap based on aHashMap
and storing the multiple values in anArrayList
. -
MultiValueMap
Creates a MultiValueMap which decorates the givenmap
and creates the value collections using the suppliedcollectionFactory
.- Parameters:
map
- the map to decoratecollectionFactory
- the collection factory which must return a Collection instance
-
-
Method Details
-
decorate
Creates a map which wraps the given map and maps keys to ArrayLists.- Parameters:
map
- the map to wrap
-
decorate
Creates a map which decorates the givenmap
and maps keys to collections of typecollectionClass
.- Parameters:
map
- the map to wrapcollectionClass
- the type of the collection class
-
decorate
Creates a map which decorates the givenmap
and creates the value collections using the suppliedcollectionFactory
.- Parameters:
map
- the map to decoratecollectionFactory
- the collection factory (must return a Collection object).
-
clear
public void clear()Clear the map.- Specified by:
clear
in interfaceMap
- Overrides:
clear
in classAbstractMapDecorator
-
removeMapping
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 subsequantget(key)
.- Specified by:
removeMapping
in interfaceMultiMap
- Parameters:
key
- the key to remove fromvalue
- the value to remove- Returns:
- the value removed (which was passed in), null if nothing removed
-
containsValue
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 interfaceMap
- Specified by:
containsValue
in interfaceMultiMap
- Overrides:
containsValue
in classAbstractMapDecorator
- Parameters:
value
- the value to search for- Returns:
- true if the map contains the value
-
put
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. -
putAll
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)
.- Specified by:
putAll
in interfaceMap
- Overrides:
putAll
in classAbstractMapDecorator
- Parameters:
map
- the map to copy (either a normal or multi map)
-
values
Gets a collection containing all the values in the map.This returns a collection containing the combination of values from all keys.
-
containsValue
Checks whether the collection at the specified key contains the value.- Parameters:
value
- the value to search for- Returns:
- true if the map contains the value
-
getCollection
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
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
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
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
-
totalSize
public int totalSize()Gets the total size of the map by counting all the values.- Returns:
- the total size of the map counting all values
-
createCollection
Creates a new instance of the map value Collection container using the factory.This method can be overridden to perform your own processing instead of using the factory.
- Parameters:
size
- the collection size that is about to be added- Returns:
- the new collection
-