|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.collections15.multimap.MultiHashMap<K,V>
public class MultiHashMap<K,V>
MultiHashMap
is the default implementation of the
MultiMap
interface.
MultiMap
is like a Map, but 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:
Number key = new Integer(5); MultiMap<Number,String> mhm = new MultiHashMap<Number,String>(); mhm.put(key, "A"); mhm.put(key, "B"); mhm.put(key, "C"); Collection<String> coll = mhm.get(key);
list
will be a list containing "A", "B", "C".
Constructor Summary | |
---|---|
MultiHashMap()
Constructor. |
|
MultiHashMap(int initialCapacity)
Constructor. |
|
MultiHashMap(int initialCapacity,
float loadFactor)
Constructor. |
|
MultiHashMap(Map<K,V> mapToCopy)
Constructor that copies the input map creating an independent copy. |
|
MultiHashMap(MultiMap<K,V> mapToCopy)
Constructor that copies the input MultiMap creating an independent copy. |
Method Summary | |
---|---|
void |
clear()
Clear the map. |
Object |
clone()
Clones the map creating an independent copy. |
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified key. |
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<V> |
createCollection(Collection<? extends V> coll)
Creates a new instance of the map value Collection container. |
Set<Map.Entry<K,Collection<V>>> |
entrySet()
Returns a set view of the mappings contained in this map. |
boolean |
equals(Object obj)
Compares the specified object with this map for equality. |
Collection<V> |
get(Object key)
Gets the collection of values associated with the specified key. |
Collection<V> |
getCollection(Object key)
Gets the collection mapped to the specified key. |
int |
hashCode()
Returns the hash code value for this map. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
Iterator<V> |
iterator(Object key)
Gets an iterator for the collection mapped to the specified key. |
Set<K> |
keySet()
Returns a set view of the keys contained in this map. |
Map<K,Collection<V>> |
map()
Returns a java.util.Map<K,Collection<V>> for this MultiMap. |
V |
put(K key,
V value)
Adds the value to the collection associated with the specified key. |
boolean |
putAll(K key,
Collection<? extends V> values)
Adds a collection of values to the collection associated with the specified key. |
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map (optional operation). |
void |
putAll(MultiMap<? extends K,? extends V> map)
Copies all of the mappings from the specified multimap to this multimap (optional operation). |
Collection<V> |
remove(Object key)
Removes all values associated with the specified key. |
V |
remove(Object key,
Object item)
Removes a specific value from map. |
int |
size()
Gets the number of keys in this map. |
int |
size(Object key)
Gets the size of the collection mapped to the specified key. |
String |
toString()
|
int |
totalSize()
Gets the total size of the map by counting all the values. |
Collection<V> |
values()
Gets a collection containing all the values in the map. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
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(Map<K,V> mapToCopy)
mapToCopy
- a Map to copypublic MultiHashMap(MultiMap<K,V> mapToCopy)
mapToCopy
- a Map to copyMethod Detail |
---|
public int totalSize()
public Collection<V> getCollection(Object key)
get(key)
.
key
- the key to retrieve
public int size(Object key)
size
in interface MultiMap<K,V>
key
- the key to get size for
public Iterator<V> iterator(Object key)
iterator
in interface MultiMap<K,V>
key
- the key to get an iterator for
public V put(K key, V value)
Map
the previous value is not replaced.
Instead the new value is added to the collection stored against the key.
put
in interface MultiMap<K,V>
key
- the key to store againstvalue
- the value to add to the collection at the key
public boolean putAll(K key, Collection<? extends V> values)
putAll
in interface MultiMap<K,V>
key
- the key to store againstvalues
- the values to add to the collection at the key, null ignored
public boolean containsValue(Object value)
containsValue
in interface MultiMap<K,V>
value
- the value to search for
public boolean containsValue(Object key, Object value)
containsValue
in interface MultiMap<K,V>
value
- the value to search forkey
- the key against which to search for the value
public V remove(Object key, Object item)
null
will be returned
from a subsequant get(key)
.
remove
in interface MultiMap<K,V>
key
- the key to remove fromitem
- the value to remove
public void clear()
clear
in interface MultiMap<K,V>
public int size()
MultiMap
size
in interface MultiMap<K,V>
public Collection<V> get(Object key)
MultiMap
Collection
. Implementations
are free to declare that they return Collection
subclasses
such as List
or Set
.
Implementations return null
if no values have
been mapped to the key.
Implementations may choose to return a clone of the internal collection.
get
in interface MultiMap<K,V>
key
- the key to retrieve
Collection
of values, implementations should
return null
for no mapping, but may return an empty collectionpublic Collection<V> remove(Object key)
MultiMap
null
from a subsequent
get(Object)
, however they may choose to return an empty collection.
remove
in interface MultiMap<K,V>
key
- the key to remove values from
Collection
of values removed, implementations should
return null
for no mapping found, but may return an empty collectionpublic boolean isEmpty()
MultiMap
isEmpty
in interface MultiMap<K,V>
public boolean containsKey(Object key)
MultiMap
containsKey
in interface MultiMap<K,V>
key
- key whose presence in this map is to be tested.
public void putAll(Map<? extends K,? extends V> map)
MultiMap
put(k, v)
on this map once
for each mapping from key k to value v in the
specified map. The behavior of this operation is unspecified if the
specified map is modified while the operation is in progress.
putAll
in interface MultiMap<K,V>
map
- Mappings to be stored in this map.public void putAll(MultiMap<? extends K,? extends V> map)
MultiMap
put(k, v)
on this map once
for each mapping from key to collections15 of values in the
specified multimap. The behavior of this operation is unspecified if the
specified multimap is modified while the operation is in progress.
putAll
in interface MultiMap<K,V>
map
- Mappings to be stored in this map.public Set<K> keySet()
MultiMap
keySet
in interface MultiMap<K,V>
public Set<Map.Entry<K,Collection<V>>> entrySet()
MultiMap
Map.Entry
. The set is backed by the
map, so changes to the map are reflected in the set, and vice-versa.
If the map is modified while an iteration over the set is in progress
(except through the iterator's own remove operation, or through
the setValue operation on a map entry returned by the iterator)
the results of the iteration are undefined. The set supports element
removal, which removes the corresponding mapping from the map, via the
Iterator.remove, Set.remove, removeAll,
retainAll and clear operations. It does not support
the add or addAll operations.
entrySet
in interface MultiMap<K,V>
public Map<K,Collection<V>> map()
MultiMap
map
in interface MultiMap<K,V>
public Collection<V> values()
values
in interface MultiMap<K,V>
public Object clone()
clone
in class Object
public boolean equals(Object obj)
MultiMap
equals
in interface MultiMap<K,V>
equals
in class Object
obj
- object to be compared for equality with this map.
public int hashCode()
MultiMap
hashCode
in interface MultiMap<K,V>
hashCode
in class Object
Map.Entry#hashCode()
,
Object.hashCode()
,
Object.equals(Object)
,
MultiMap.equals(Object)
protected Collection<V> createCollection(Collection<? extends V> coll)
coll
- the collection to copy, may be null
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |