public class MultiValueMap extends AbstractMapDecorator implements MultiMap
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.
Constructor and Description |
---|
MultiValueMap()
Creates a MultiValueMap based on a
HashMap and
storing the multiple values in an ArrayList . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear the map.
|
boolean |
containsValue(java.lang.Object value)
Checks whether the map contains the value specified.
|
boolean |
containsValue(java.lang.Object key,
java.lang.Object value)
Checks whether the collection at the specified key contains the value.
|
static MultiValueMap |
decorate(java.util.Map map)
Creates a map which wraps the given map and
maps keys to ArrayLists.
|
static MultiValueMap |
decorate(java.util.Map map,
java.lang.Class collectionClass)
Creates a map which decorates the given
map and
maps keys to collections of type collectionClass . |
static MultiValueMap |
decorate(java.util.Map map,
Factory collectionFactory)
Creates a map which decorates the given
map and
creates the value collections using the supplied collectionFactory . |
java.util.Collection |
getCollection(java.lang.Object key)
Gets the collection mapped to the specified key.
|
java.util.Iterator |
iterator(java.lang.Object key)
Gets an iterator for the collection mapped to the specified key.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Adds the value to the collection associated with the specified key.
|
void |
putAll(java.util.Map map)
Override superclass to ensure that MultiMap instances are
correctly handled.
|
boolean |
putAll(java.lang.Object key,
java.util.Collection values)
Adds a collection of values to the collection associated with
the specified key.
|
java.lang.Object |
remove(java.lang.Object key,
java.lang.Object value)
Removes a specific value from map.
|
int |
size(java.lang.Object key)
Gets the size of the collection mapped to the specified key.
|
int |
totalSize()
Gets the total size of the map by counting all the values.
|
java.util.Collection |
values()
Gets a collection containing all the values in the map.
|
public MultiValueMap()
HashMap
and
storing the multiple values in an ArrayList
.public static MultiValueMap decorate(java.util.Map map)
map
- the map to wrappublic static MultiValueMap decorate(java.util.Map map, java.lang.Class collectionClass)
map
and
maps keys to collections of type collectionClass
.map
- the map to wrapcollectionClass
- the type of the collection classpublic static MultiValueMap decorate(java.util.Map map, Factory collectionFactory)
map
and
creates the value collections using the supplied collectionFactory
.map
- the map to decoratecollectionFactory
- the collection factory (must return a Collection object).public void clear()
clear
in interface java.util.Map
clear
in class AbstractMapDecorator
public java.lang.Object remove(java.lang.Object key, java.lang.Object value)
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 subsequant get(key)
.
public boolean containsValue(java.lang.Object value)
This checks all collections against all keys for the value, and thus could be slow.
containsValue
in interface java.util.Map
containsValue
in interface MultiMap
containsValue
in class AbstractMapDecorator
value
- the value to search forpublic java.lang.Object put(java.lang.Object key, java.lang.Object 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 java.util.Map
put
in interface MultiMap
put
in class AbstractMapDecorator
key
- the key to store againstvalue
- the value to add to the collection at the keypublic void putAll(java.util.Map map)
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 using putAll(Object,Collection)
.
putAll
in interface java.util.Map
putAll
in class AbstractMapDecorator
map
- the map to copy (either a normal or multi map)public java.util.Collection values()
This returns a collection containing the combination of values from all keys.
values
in interface java.util.Map
values
in interface MultiMap
values
in class AbstractMapDecorator
public boolean containsValue(java.lang.Object key, java.lang.Object value)
value
- the value to search forpublic java.util.Collection getCollection(java.lang.Object key)
get(key)
.key
- the key to retrievepublic int size(java.lang.Object key)
key
- the key to get size forpublic boolean putAll(java.lang.Object key, java.util.Collection values)
key
- the key to store againstvalues
- the values to add to the collection at the key, null ignoredpublic java.util.Iterator iterator(java.lang.Object key)
key
- the key to get an iterator forpublic int totalSize()
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"