org.apache.commons.collections
Class MultiHashMap
HashMap
|
+--org.apache.commons.collections.MultiHashMap
All Implemented Interfaces:
MultiMap
public class
MultiHashMapextends HashMap
implements
MultiMapMultiHashMap
is the default implementation of the
org.apache.commons.collections.MultiMap 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 always return a Collection, holding all the
values put to that key. This implementation uses an ArrayList as the
collection.
For example:
MultiMap mhm = new MultiHashMap();
mhm.put(key, "A");
mhm.put(key, "B");
mhm.put(key, "C");
Collection coll = mhm.get(key);
coll
will be a list containing "A", "B", "C".
- Commons Collections 2.0
- Christopher Berry
- James Strachan
- Steve Downey
- Stephen Colebourne
- Julien Buret
- Serhiy Yevtushenko
MultiHashMap
public MultiHashMap()
Constructor.
MultiHashMap
public MultiHashMap(int initialCapacity, float loadFactor)
Constructor.
- initialCapacity - the initial map capacity
- loadFactor - the amount 0.0-1.0 at which to resize the map
MultiHashMap
public MultiHashMap(int initialCapacity)
Constructor.
- initialCapacity - the initial map capacity
MultiHashMap
public MultiHashMap(Map mapToCopy)
Constructor.
- mapToCopy - a Map to copy
clear
public void clear()
Clear the map.
This clears each collection in the map, and so may be slow.
clone
public Object clone()
Clone the map.
The clone will shallow clone the collections as well as the map.
- the cloned map
containsValue
public boolean containsValue(Object value)
Does the map contain a specific value.
This searches the collection mapped to each key, and thus could be slow.
- value - the value to search for
- true if the list 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
- the new collection
put
public Object put(Object key, Object value)
Put a key and value into the map.
The value is added to a collection mapped to the key instead of
replacing the previous value.
- key - the key to set
- value - the value to set the key to
- the value added if the add is successful, null otherwise
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.
- key - the key to remove from
- item - the value to remove
- the value removed (which was passed in)
values
public Collection values()
Gets a view over all the values in the map.
The values view includes all the entries in the collections at each map key.
- the collection view of all the values in the map
MultiHashMap
is the default implementation of the org.apache.commons.collections.MultiMap MultiMap interface. AMultiMap
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 always return a Collection, holding all the values put to that key. This implementation uses an ArrayList as the collection. For example:coll
will be a list containing "A", "B", "C".