|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.util.MultiValueMap<K,V>
public class MultiValueMap<K,V>
A MultiValueMap
allows storing multiple values for each key.
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.
When there are no values mapped to a key, null
is returned.
This class does not implement the Map
interface because
of the slightly different semantics: put
adds a value of
type V
, but get
returns a Collection of
V
objects instead of a single V
object.
For example:
MultiValueMap<String, String> mm = new MultiValueMap<String, String>(); mm.put(key, "A"); mm.put(key, "B"); mm.put(key, "C"); Collection<String> col = mm.get(key);
col
will be a collection containing "A", "B", "C".
This class has been adapted from the MultiHashMap
in the
Jakarta Commons
Collections.
Constructor Summary | |
---|---|
MultiValueMap()
Creates a new instance, using a HashMap as storage. |
|
MultiValueMap(Map<K,Collection<V>> wrappedMap)
Creates a new instance. |
Method Summary | |
---|---|
void |
clear()
Removes all mappings from this map. |
boolean |
containsKey(K key)
Returns true if this map contains a mapping for the
specified key. |
boolean |
containsValue(K key,
V value)
Checks whether the collection at the specified key contains the value. |
boolean |
containsValue(V value)
Checks whether the map contains the value specified. |
protected Collection<V> |
createCollection(Collection<? extends V> coll)
Creates a new instance of the map value Collection container. |
boolean |
equals(Object o)
Compares the specified object with this map for equality. |
Collection<V> |
get(K key)
Returns the collection of values to which this map maps 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. |
Set<K> |
keySet()
Returns a set view of the keys contained in this map. |
V |
put(K key,
V value)
Adds the value to the collection associated with the specified key. |
boolean |
putAll(K key,
Collection<? extends V> valueCol)
Adds a collection of values to the collection associated with the specified key. |
Collection<V> |
remove(K key)
Removes all mappings for this key from this map if any are present. |
Object |
remove(K key,
V item)
Removes a specific value from map. |
int |
size()
Returns the number of key-value mappings in this map. |
int |
size(K 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<V> |
values()
Gets a collection containing all the values in the map. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MultiValueMap()
HashMap
as storage.
public MultiValueMap(Map<K,Collection<V>> wrappedMap)
wrappedMap
- wrapped map used as storage, e.g. a HashMap
or
a TreeMap
Method Detail |
---|
protected Collection<V> createCollection(Collection<? extends V> coll)
This method can be overridden to use your own collection type.
coll
- the collection to copy, may be null
public void clear()
public boolean containsKey(K key)
true
if this map contains a mapping for the
specified key. More formally, returns true
if and only if
this map contains a mapping for a key k
such that
(key==null ? k==null : key.equals(k))
. (There can be one or
several such mappings.)
key
- key whose presence in this map is to be tested
true
if this map contains a mapping for the
pecified keypublic boolean containsValue(V value)
This checks all collections against all keys for the value, and thus could be slow.
value
- the value to search for
true
if the map contains the valuepublic boolean containsValue(K key, V value)
key
- the key to usevalue
- the value to search for
true
if the map contains the value at the specified
keypublic boolean equals(Object o)
true
if the given object is also a
MultiValueMap
map or a map of collections and the two Maps
represent the same mappings.
equals
in class Object
o
- object to be compared for equality with this map
true
if the specified object is equal to this mappublic Collection<V> get(K key)
key
- key whose associated value is to be returned
null
if the map contains no mapping for this keypublic int hashCode()
hashCode
in class Object
public boolean isEmpty()
true
if this map contains no key-value mappings.
true
if this map contains no key-value mappingspublic Set<K> keySet()
public V put(K key, V value)
Unlike a normal Map
the previous value is not replaced.
Instead the new value is added to the collection stored against the key.
key
- the key to store againstvalue
- the value to add to the collection at the key
null
if the
map did not changepublic boolean putAll(K key, Collection<? extends V> valueCol)
key
- the key to store againstvalueCol
- the values to add to the collection at the key,
ignored if null
true
if this map changedpublic Collection<V> remove(K key)
null
if the map contained no mappings for this
key. The map will not contain any mappings for the specified key once
the call returns.
key
- key whose mappings are to be removed from the map.
null
if there was no mapping for keypublic Object remove(K key, V 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)
.
key
- the key to remove fromitem
- the value to remove
null
if
nothing removedpublic int size()
Integer.MAX_VALUE
elements, returns
Integer.MAX_VALUE
.
public int size(K key)
key
- the key to get size for
public int totalSize()
public Collection<V> values()
This returns a collection containing the combination of values from all keys.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |