Class 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:
Serializable, Map<K,Object>, Get<K, Object>, IterableGet<K, Object>, IterableMap<K, Object>, MultiMap<K, V>, Put<K, Object>
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:
- 3.2
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classMultiValueMap.ReflectionFactory<T extends Collection<?>>Deprecated.Inner class that provides a simple reflection factory.private classDeprecated.Inner class that provides the values view.private classDeprecated.Inner class that provides the values iterator. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Factory<? extends Collection<V>> Deprecated.The factory for creating value collections.private static final longDeprecated.Serialization versionprivate Collection<V> Deprecated.The cached values.Fields inherited from class AbstractMapDecorator
map -
Constructor Summary
ConstructorsModifierConstructorDescriptionDeprecated.Creates a MultiValueMap based on aHashMapand storing the multiple values in anArrayList.protected <C extends Collection<V>>MultiValueMap(Map<K, ? super C> map, Factory<C> collectionFactory) Deprecated.Creates a MultiValueMap which decorates the givenmapand creates the value collections using the suppliedcollectionFactory. -
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.protected Collection<V> createCollection(int size) Deprecated.Creates a new instance of the map value Collection container using the factory.entrySet()Deprecated.getCollection(Object key) Deprecated.Gets the collection mapped to the specified key.iterator()Deprecated.Gets an iterator for all mappings stored in thisMultiValueMap.Deprecated.Gets an iterator for the collection mapped to the specified key.static <K, V, C extends Collection<V>>
MultiValueMap<K, V> multiValueMap(Map<K, ? super C> map, Class<C> collectionClass) Deprecated.Creates a map which decorates the givenmapand maps keys to collections of typecollectionClass.static <K, V, C extends Collection<V>>
MultiValueMap<K, V> multiValueMap(Map<K, ? super C> map, Factory<C> collectionFactory) Deprecated.Creates a map which decorates the givenmapand creates the value collections using the suppliedcollectionFactory.static <K,V> MultiValueMap <K, V> multiValueMap(Map<K, ? super Collection<V>> map) Deprecated.Creates a map which wraps the given map and maps keys to ArrayLists.Deprecated.Adds the value to the collection associated with the specified key.voidDeprecated.Override superclass to ensure that MultiMap instances are correctly handled.booleanputAll(K key, Collection<V> values) Deprecated.Adds a collection of values to the collection associated with the specified key.private voidDeprecated.Read the map in using a custom routine.booleanremoveMapping(Object key, Object value) 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.private voidDeprecated.Write the map out using a custom routine.Methods inherited from class AbstractMapDecorator
containsKey, decorated, equals, get, hashCode, isEmpty, keySet, remove, size, toStringMethods inherited from class AbstractIterableMap
mapIteratorMethods inherited from interface Get
containsKey, isEmpty, keySetMethods inherited from interface IterableGet
mapIteratorMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDDeprecated.Serialization version- See Also:
-
collectionFactory
Deprecated.The factory for creating value collections. -
valuesView
-
-
Constructor Details
-
MultiValueMap
public MultiValueMap()Deprecated.Creates a MultiValueMap based on aHashMapand storing the multiple values in anArrayList. -
MultiValueMap
protected <C extends Collection<V>> MultiValueMap(Map<K, ? super C> map, Factory<C> collectionFactory) Deprecated.Creates a MultiValueMap which decorates the givenmapand creates the value collections using the suppliedcollectionFactory.- Type Parameters:
C- the collection class type- Parameters:
map- the map to decoratecollectionFactory- the collection factory which must return a Collection instance
-
-
Method Details
-
multiValueMap
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 Collection<V>> MultiValueMap<K,V> multiValueMap(Map<K, ? super C> map, Class<C> collectionClass) Deprecated.Creates a map which decorates the givenmapand 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 Collection<V>> MultiValueMap<K,V> multiValueMap(Map<K, ? super C> map, Factory<C> collectionFactory) Deprecated.Creates a map which decorates the givenmapand 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
-
writeObject
Deprecated.Write the map out using a custom routine.- Parameters:
out- the output stream- Throws:
IOException- if an error occurs while writing to the stream- Since:
- 4.0
-
readObject
Deprecated.Read the map in using a custom routine.- Parameters:
in- the input stream- Throws:
IOException- if an error occurs while reading from the streamClassNotFoundException- if an object read from the stream can not be loaded- Since:
- 4.0
-
clear
-
removeMapping
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 subsequentget(key).- Specified by:
removeMappingin interfaceMultiMap<K,V> - Parameters:
key- the key to remove fromvalue- the value to remove- Returns:
trueif the mapping was removed,falseotherwise
-
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 interfaceGet<K,V> - Specified by:
containsValuein interfaceMap<K,V> - Specified by:
containsValuein interfaceMultiMap<K,V> - Overrides:
containsValuein classAbstractMapDecorator<K,Object> - Parameters:
value- the value to search for- Returns:
- true if the map contains the value
- See Also:
-
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.- Specified by:
putin interfaceMap<K,V> - Specified by:
putin interfaceMultiMap<K,V> - Specified by:
putin interfacePut<K,V> - Overrides:
putin classAbstractMapDecorator<K,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:
-
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). -
entrySet
Deprecated.NOTE: the returned Entry objects will contain as value a
Collectionof all values that are mapped to the given key. To get a "flattened" version of all mappings contained in this map, useiterator(). -
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
-
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
-
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
-
createCollection
Deprecated.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
-
MultiValuedMapinstead