public class MultiHashMap extends java.util.HashMap implements 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".
Constructor and Description |
---|
MultiHashMap()
Deprecated.
Constructor.
|
MultiHashMap(int initialCapacity)
Deprecated.
Constructor.
|
MultiHashMap(int initialCapacity,
float loadFactor)
Deprecated.
Constructor.
|
MultiHashMap(java.util.Map mapToCopy)
Deprecated.
Constructor that copies the input map creating an independent copy.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Deprecated.
Clear the map.
|
java.lang.Object |
clone()
Deprecated.
Clones the map creating an independent copy.
|
boolean |
containsValue(java.lang.Object value)
Deprecated.
Checks whether the map contains the value specified.
|
boolean |
containsValue(java.lang.Object key,
java.lang.Object value)
Deprecated.
Checks whether the collection at the specified key contains the value.
|
java.util.Collection |
getCollection(java.lang.Object key)
Deprecated.
Gets the collection mapped to the specified key.
|
java.util.Iterator |
iterator(java.lang.Object key)
Deprecated.
Gets an iterator for the collection mapped to the specified key.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Deprecated.
Adds the value to the collection associated with the specified key.
|
void |
putAll(java.util.Map map)
Deprecated.
Override superclass to ensure that MultiMap instances are
correctly handled.
|
boolean |
putAll(java.lang.Object key,
java.util.Collection values)
Deprecated.
Adds a collection of values to the collection associated with the specified key.
|
java.lang.Object |
remove(java.lang.Object key,
java.lang.Object item)
Deprecated.
Removes a specific value from map.
|
int |
size(java.lang.Object key)
Deprecated.
Gets the size of the collection mapped to the specified key.
|
int |
totalSize()
Deprecated.
Gets the total size of the map by counting all the values.
|
java.util.Collection |
values()
Deprecated.
Gets a collection containing all the values in the map.
|
public MultiHashMap()
public MultiHashMap(int initialCapacity)
initialCapacity
- the initial map capacitypublic MultiHashMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial map capacityloadFactor
- the amount 0.0-1.0 at which to resize the mappublic MultiHashMap(java.util.Map mapToCopy)
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.
mapToCopy
- a Map to copypublic int totalSize()
public 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 java.util.Iterator iterator(java.lang.Object key)
key
- the key to get an iterator 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 java.util.HashMap
key
- the key to store againstvalue
- the value to add to the collection at the keypublic void putAll(java.util.Map map)
NOTE: Prior to version 3.2, putAll(map) did not work properly when passed a MultiMap.
putAll
in interface java.util.Map
putAll
in class java.util.HashMap
map
- the map to copy (either a normal or multi map)public 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 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 java.util.HashMap
value
- the value to search forpublic boolean containsValue(java.lang.Object key, java.lang.Object value)
value
- the value to search forpublic java.lang.Object remove(java.lang.Object key, java.lang.Object item)
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 void clear()
This clears each collection in the map, and so may be slow.
clear
in interface java.util.Map
clear
in class java.util.HashMap
public java.util.Collection values()
This returns a collection containing the combination of values from all keys.
public java.lang.Object clone()
The clone will shallow clone the collections as well as the map.
clone
in class java.util.HashMap
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"