org.apache.commons.collections
Class MultiHashMap
HashMap
org.apache.commons.collections.MultiHashMap
- Map, MultiMap
public class MultiHashMap
extends HashMap
MultiHashMap
is the default implementation of the
MultiMap
interface.
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 uses an
ArrayList
as the collection.
The internal storage list is made available without cloning via the
get(Object)
and
entrySet()
methods.
The implementation returns
null
when there are no values mapped to a key.
For example:
MultiMap mhm = new MultiHashMap();
mhm.put(key, "A");
mhm.put(key, "B");
mhm.put(key, "C");
List list = (List) mhm.get(key);
list
will be a list containing "A", "B", "C".
$Revision: 1.20 $ $Date: 2004/06/09 22:11:54 $- Christopher Berry
- James Strachan
- Steve Downey
- Stephen Colebourne
- Julien Buret
- Serhiy Yevtushenko
void | clear() - Clear the map.
|
Object | clone() - Clones the map creating an independent copy.
|
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(Collection coll) - Creates a new instance of the map value Collection container.
|
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.
|
boolean | putAll(Object key, Collection values) - Adds a collection of values to the collection associated with the specified key.
|
Object | remove(Object key, Object item) - 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.
|
MultiHashMap
public MultiHashMap()
Constructor.
MultiHashMap
public MultiHashMap(Map mapToCopy)
Constructor that copies the input map creating an independent copy.
This method performs different behaviour depending on whether the map
specified is a MultiMap or not. If a MultiMap is specified, each internal
collection is also cloned. If the specified map only implements Map, then
the values are not cloned.
NOTE: From Commons Collections 3.1 this method correctly copies a MultiMap
to form a truly independent new map.
mapToCopy
- a Map to copy
MultiHashMap
public MultiHashMap(int initialCapacity)
Constructor.
initialCapacity
- the initial map capacity
MultiHashMap
public MultiHashMap(int initialCapacity,
float loadFactor)
Constructor.
initialCapacity
- the initial map capacityloadFactor
- the amount 0.0-1.0 at which to resize the map
clear
public void clear()
Clear the map.
This clears each collection in the map, and so may be slow.
clone
public Object clone()
Clones the map creating an independent copy.
The clone will shallow clone the collections as well as the map.
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
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(Collection coll)
Creates a new instance of the map value Collection container.
This method can be overridden to use your own collection type.
coll
- the collection to copy, may be null
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
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 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 item)
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 fromitem
- 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
- a collection view of the values contained in this map
Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.