de.fu_berlin.ties.util
Class MultiValueMap<K,V>

java.lang.Object
  extended by de.fu_berlin.ties.util.MultiValueMap<K,V>
Direct Known Subclasses:
SortedMultiValueMap

public class MultiValueMap<K,V>
extends Object

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.

Version:
$Revision: 1.5 $, $Date: 2004/11/05 12:16:16 $, $Author: siefkes $
Author:
Christian Siefkes

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

MultiValueMap

public MultiValueMap()
Creates a new instance, using a HashMap as storage.


MultiValueMap

public MultiValueMap(Map<K,Collection<V>> wrappedMap)
Creates a new instance.

Parameters:
wrappedMap - wrapped map used as storage, e.g. a HashMap or a TreeMap
Method Detail

createCollection

protected Collection<V> createCollection(Collection<? extends V> coll)
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

clear

public void clear()
Removes all mappings from this map.


containsKey

public boolean containsKey(K key)
Returns 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.)

Parameters:
key - key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the pecified key

containsValue

public boolean containsValue(V value)
Checks whether the map contains the value specified.

This checks all collections against all keys for the value, and thus could be slow.

Parameters:
value - the value to search for
Returns:
true if the map contains the value

containsValue

public boolean containsValue(K key,
                             V value)
Checks whether the collection at the specified key contains the value.

Parameters:
key - the key to use
value - the value to search for
Returns:
true if the map contains the value at the specified key

equals

public boolean equals(Object o)
Compares the specified object with this map for equality. Returns true if the given object is also a MultiValueMap map or a map of collections and the two Maps represent the same mappings.

Overrides:
equals in class Object
Parameters:
o - object to be compared for equality with this map
Returns:
true if the specified object is equal to this map

get

public Collection<V> get(K key)
Returns the collection of values to which this map maps the specified key.

Parameters:
key - key whose associated value is to be returned
Returns:
the collection of values to which this map maps the specified key, or null if the map contains no mapping for this key

hashCode

public int hashCode()
Returns the hash code value for this map.

Overrides:
hashCode in class Object
Returns:
the hash code value for this map.

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Returns:
true if this map contains no key-value mappings

keySet

public Set<K> keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

Returns:
a set view of the keys contained in this map

put

public V put(K key,
             V value)
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.

Parameters:
key - the key to store against
value - the value to add to the collection at the key
Returns:
the value added if the map changed and null if the map did not change

putAll

public boolean putAll(K key,
                      Collection<? extends V> valueCol)
Adds a collection of values to the collection associated with the specified key.

Parameters:
key - the key to store against
valueCol - the values to add to the collection at the key, ignored if null
Returns:
true if this map changed

remove

public Collection<V> remove(K key)
Removes all mappings for this key from this map if any are present. Returns the collection of value to which the map previously associated the key, or 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.

Parameters:
key - key whose mappings are to be removed from the map.
Returns:
collection of values previously associated with specified key, or null if there was no mapping for key

remove

public Object remove(K key,
                     V item)
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 subsequant get(key).

Parameters:
key - the key to remove from
item - the value to remove
Returns:
the value removed (which was passed in), null if nothing removed

size

public int size()
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:
the number of key-value mappings in this map

size

public int size(K key)
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

totalSize

public int totalSize()
Gets the total size of the map by counting all the values.

Returns:
the total size of the map counting all values

values

public Collection<V> values()
Gets a collection containing all the values in the map.

This returns a collection containing the combination of values from all keys.

Returns:
a collection view of the values contained in this map


Copyright © 2003-2004 Christian Siefkes. All Rights Reserved.