org.apache.commons.collections.map
Class MultiValueMap
- Map, MultiMap
public class MultiValueMap
A 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 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.
$Revision: 348007 $ $Date: 2005-11-21 22:52:57 +0000 (Mon, 21 Nov 2005) $- James Carman
- Christopher Berry
- James Strachan
- Steve Downey
- Stephen Colebourne
- Julien Buret
- Serhiy Yevtushenko
MultiValueMap() - Creates a MultiValueMap based on a
HashMap and
storing the multiple values in an ArrayList .
|
MultiValueMap(Map map, Factory collectionFactory) - Creates a MultiValueMap which decorates the given
map and
creates the value collections using the supplied collectionFactory .
|
void | 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 | decorate(Map map) - Creates a map which wraps the given map and
maps keys to ArrayLists.
|
static MultiValueMap | decorate(Map map, Class collectionClass) - Creates a map which decorates the given
map and
maps keys to collections of type collectionClass .
|
static MultiValueMap | decorate(Map map, Factory collectionFactory) - Creates a map which decorates the given
map and
creates the value collections using the supplied collectionFactory .
|
Collection | getCollection(Object key) - Gets the collection mapped to the specified key.
|
Iterator | iterator(Object key) - Gets an iterator for the collection mapped to the specified key.
|
Object | put(Object key, Object value) - Adds the value to the collection associated with the specified key.
|
void | putAll(Map map) - Override superclass to ensure that MultiMap instances are
correctly handled.
|
boolean | putAll(Object key, Collection values) - Adds a collection of values to the collection associated with
the specified key.
|
Object | remove(Object key, Object value) - Removes a specific value from map.
|
int | size(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.
|
Collection | values() - Gets a collection containing all the values in the map.
|
clear , containsKey , containsValue , entrySet , equals , get , getMap , hashCode , isEmpty , keySet , put , putAll , remove , size , toString , values |
MultiValueMap
public MultiValueMap()
Creates a MultiValueMap based on a HashMap
and
storing the multiple values in an ArrayList
.
MultiValueMap
protected MultiValueMap(Map map,
Factory collectionFactory)
Creates a MultiValueMap which decorates the given map
and
creates the value collections using the supplied collectionFactory
.
map
- the map to decoratecollectionFactory
- the collection factory which must return a Collection instance
containsValue
public boolean containsValue(Object value)
Checks whether the map contains the value specified.
This checks all collections against all keys for the value, and thus could be slow.
- containsValue in interface MultiMap
- containsValue in interface AbstractMapDecorator
value
- the value to search for
- true if the map contains the value
containsValue
public boolean containsValue(Object key,
Object value)
Checks whether the collection at the specified key contains the value.
value
- the value to search for
- true if the map contains the value
createCollection
protected Collection createCollection(int size)
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.
size
- the collection size that is about to be added
decorate
public static MultiValueMap decorate(Map map)
Creates a map which wraps the given map and
maps keys to ArrayLists.
decorate
public static MultiValueMap decorate(Map map,
Class collectionClass)
Creates a map which decorates the given map
and
maps keys to collections of type collectionClass
.
map
- the map to wrapcollectionClass
- the type of the collection class
decorate
public static MultiValueMap decorate(Map map,
Factory collectionFactory)
Creates a map which decorates the given map
and
creates the value collections using the supplied collectionFactory
.
map
- the map to decoratecollectionFactory
- the collection factory (must return a Collection object).
getCollection
public Collection getCollection(Object key)
Gets the collection mapped to the specified key.
This method is a convenience method to typecast the result of get(key)
.
key
- the key to retrieve
- the collection mapped to the key, null if no mapping
iterator
public Iterator iterator(Object key)
Gets an iterator for the collection mapped to the specified key.
key
- the key to get an iterator for
- the iterator of the collection at the key, empty iterator if key not in map
put
public Object put(Object key,
Object value)
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.
- put in interface MultiMap
- put in interface AbstractMapDecorator
key
- the key to store againstvalue
- the value to add to the collection at the key
- the value added if the map changed and null if the map did not change
putAll
public void putAll(Map map)
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 using
putAll(Object,Collection)
.
- putAll in interface AbstractMapDecorator
map
- the map to copy (either a normal or multi map)
putAll
public boolean putAll(Object key,
Collection values)
Adds a collection of values to the collection associated with
the specified key.
key
- the key to store againstvalues
- the values to add to the collection at the key, null ignored
remove
public Object remove(Object key,
Object value)
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 subsequant
get(key)
.
- remove in interface MultiMap
key
- the key to remove fromvalue
- the value to remove
- the value removed (which was passed in), null if nothing removed
size
public int size(Object key)
Gets the size of the collection mapped to the specified key.
key
- the key to get size for
- the size of the collection at the key, zero if key not in map
totalSize
public int totalSize()
Gets the total size of the map by counting all the values.
- the total size of the map counting all values
values
public Collection values()
Gets a collection containing all the values in the map.
This returns a collection containing the combination of values from all keys.
- values in interface MultiMap
- values in interface AbstractMapDecorator
- a collection view of the values contained in this map
Copyright © 2001-2015 Apache Software Foundation. All Rights Reserved.