Class MultiHashMap
- All Implemented Interfaces:
Serializable
,Cloneable
,Map
,MultiMap
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".
- Since:
- Commons Collections 2.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Christopher Berry, James Strachan, Steve Downey, Stephen Colebourne, Julien Buret, Serhiy Yevtushenko, Robert Ribnitz
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Constructor.MultiHashMap
(int initialCapacity) Deprecated.Constructor.MultiHashMap
(int initialCapacity, float loadFactor) Deprecated.Constructor.MultiHashMap
(Map mapToCopy) Deprecated.Constructor that copies the input map creating an independent copy. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Deprecated.Clear the map.clone()
Deprecated.Clones the map creating an independent copy.boolean
containsValue
(Object value) Deprecated.Checks whether the map contains the value specified.boolean
containsValue
(Object key, Object value) Deprecated.Checks whether the collection at the specified key contains the value.protected Collection
createCollection
(Collection coll) Deprecated.Creates a new instance of the map value Collection container.getCollection
(Object key) Deprecated.Gets the collection mapped to the specified key.Deprecated.Gets an iterator for the collection mapped to the specified key.Deprecated.Adds the value to the collection associated with the specified key.boolean
putAll
(Object key, Collection values) Deprecated.Adds a collection of values to the collection associated with the specified key.void
Deprecated.Override superclass to ensure that MultiMap instances are correctly handled.removeMapping
(Object key, Object item) Deprecated.Removes a specific value from map.int
Deprecated.Gets the size of the collection mapped to the specified key.int
Deprecated.Gets the total size of the map by counting all the values.values()
Deprecated.Gets a collection containing all the values in the map.Methods inherited from class java.util.HashMap
compute, computeIfAbsent, computeIfPresent, containsKey, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, putIfAbsent, remove, remove, replace, replace, replaceAll, size
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
MultiHashMap
public MultiHashMap()Deprecated.Constructor. -
MultiHashMap
public MultiHashMap(int initialCapacity) Deprecated.Constructor.- Parameters:
initialCapacity
- the initial map capacity
-
MultiHashMap
public MultiHashMap(int initialCapacity, float loadFactor) Deprecated.Constructor.- Parameters:
initialCapacity
- the initial map capacityloadFactor
- the amount 0.0-1.0 at which to resize the map
-
MultiHashMap
Deprecated.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. NOTE: From Commons Collections 3.2 this method delegates to the newly added putAll(Map) override method.
- Parameters:
mapToCopy
- a Map to copy
-
-
Method Details
-
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
- Since:
- Commons Collections 3.1
-
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
- Since:
- Commons Collections 3.1
-
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
- Since:
- Commons Collections 3.1
-
iterator
Deprecated.Gets an iterator for the collection mapped to the specified key.- Parameters:
key
- the key to get an iterator for- Returns:
- the iterator of the collection at the key, empty iterator if key not in map
- Since:
- Commons Collections 3.1
-
put
Deprecated.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. -
putAll
Deprecated.Override superclass to ensure that MultiMap instances are correctly handled.NOTE: Prior to version 3.2, putAll(map) did not work properly when passed a MultiMap.
-
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
- Since:
- Commons Collections 3.1
-
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:
containsValue
in interfaceMap
- Specified by:
containsValue
in interfaceMultiMap
- Overrides:
containsValue
in classHashMap
- Parameters:
value
- the value to search for- Returns:
- true if the map contains the value
-
containsValue
Deprecated.Checks whether the collection at the specified key contains the value.- Parameters:
value
- the value to search for- Returns:
- true if the map contains the value
- Since:
- Commons Collections 3.1
-
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,
null
will be returned from a subsequantget(key)
.- Specified by:
removeMapping
in interfaceMultiMap
- Parameters:
key
- the key to remove fromitem
- the value to remove- Returns:
- the value removed (which was passed in), null if nothing removed
-
clear
public void clear()Deprecated.Clear the map.This clears each collection in the map, and so may be slow.
-
values
Deprecated.Gets a collection containing all the values in the map.This returns a collection containing the combination of values from all keys.
-
clone
Deprecated.Clones the map creating an independent copy.The clone will shallow clone the collections as well as the map.
-
createCollection
Deprecated.Creates a new instance of the map value Collection container.This method can be overridden to use your own collection type.
- Parameters:
coll
- the collection to copy, may be null- Returns:
- the new collection
-