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
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Creates a MultiValueMap based on aHashMapand storing the multiple values in anArrayList. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Deprecated.Clear the map.booleancontainsValue(Object value) Deprecated.Checks whether the map contains the value specified.booleancontainsValue(Object key, Object value) Deprecated.Checks whether the collection at the specified key contains the value.static MultiValueMapDeprecated.Creates a map which wraps the given map and maps keys to ArrayLists.static MultiValueMapDeprecated.Creates a map which decorates the givenmapand maps keys to collections of typecollectionClass.static MultiValueMapDeprecated.Creates a map which decorates the givenmapand creates the value collections using the suppliedcollectionFactory.getCollection(Object key) Deprecated.Gets the collection mapped to the specified key.Deprecated.Gets an iterator for the collection mapped to the specified key.Deprecated.Adds the value to the collection associated with the specified key.booleanputAll(Object key, Collection values) Deprecated.Adds a collection of values to the collection associated with the specified key.voidDeprecated.Override superclass to ensure that MultiMap instances are correctly handled.Deprecated.Removes a specific value from map.intDeprecated.Gets the size of the collection mapped to the specified key.intDeprecated.Gets the total size of the map by counting all the values.values()Deprecated.Gets a collection containing all the values in the map.Methods inherited from class org.apache.commons.collections.map.AbstractMapDecorator
containsKey, entrySet, equals, get, hashCode, isEmpty, keySet, remove, size, toStringMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, putIfAbsent, replace, replace, replaceAll
-
Constructor Details
-
MultiValueMap
public MultiValueMap()Deprecated.Creates a MultiValueMap based on aHashMapand storing the multiple values in anArrayList.
-
-
Method Details
-
decorate
Deprecated.Creates a map which wraps the given map and maps keys to ArrayLists.- Parameters:
map- the map to wrap
-
decorate
Deprecated.Creates a map which decorates the givenmapand maps keys to collections of typecollectionClass.- Parameters:
map- the map to wrapcollectionClass- the type of the collection class
-
decorate
Deprecated.Creates a map which decorates the givenmapand 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()Deprecated.Clear the map.- Specified by:
clearin interfaceMap- Overrides:
clearin classAbstractMapDecorator
-
remove
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,
nullwill be returned from a subsequantget(key). -
containsValue
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:
containsValuein interfaceMap- Specified by:
containsValuein interfaceMultiMap- Overrides:
containsValuein classAbstractMapDecorator- Parameters:
value- the value to search for- Returns:
- true if the map contains the value
-
put
Deprecated.Adds the value to the collection associated with the specified key.Unlike a normal
Mapthe previous value is not replaced. Instead the new value is added to the collection stored against the key. -
putAll
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).- Specified by:
putAllin interfaceMap- Overrides:
putAllin classAbstractMapDecorator- Parameters:
map- the map to copy (either a normal or multi map)
-
values
Deprecated.Gets a collection containing all the values in the map.This returns a collection containing the combination of values from all keys.
-
containsValue
Deprecated.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
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
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
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
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
-
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
-