annas.graph
Class MultiHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by annas.graph.MultiHashMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class MultiHashMap<K,V>
extends java.util.HashMap

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.

Since:
Commons Collections 2.0
Version:
$Revision: 1.20 $ $Date: 2004/06/09 22:11:54 $
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
MultiHashMap()
          Constructor.
MultiHashMap(int initialCapacity)
          Constructor.
MultiHashMap(int initialCapacity, float loadFactor)
          Constructor.
 
Method Summary
 void clear()
          Clear the map.
 java.lang.Object clone()
          Clones the map creating an independent copy.
 boolean containsValue(java.lang.Object value)
          Checks whether the map contains the value specified.
 boolean containsValue(java.lang.Object key, java.lang.Object value)
          Checks whether the collection at the specified key contains the value.
 java.util.Collection getCollection(java.lang.Object key)
          Gets the collection mapped to the specified key.
 java.util.Iterator iterator(java.lang.Object key)
          Gets an iterator for the collection mapped to the specified key.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Adds the value to the collection associated with the specified key.
 boolean putAll(java.lang.Object key, java.util.Collection values)
          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)
          Removes a specific value from map.
 int size(java.lang.Object 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.
 java.util.Collection<V> values()
          Gets a collection containing all the values in the map.
 
Methods inherited from class java.util.HashMap
containsKey, entrySet, get, isEmpty, keySet, putAll, remove, size
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

MultiHashMap

public MultiHashMap()
Constructor.


MultiHashMap

public MultiHashMap(int initialCapacity)
Constructor.

Parameters:
initialCapacity - the initial map capacity

MultiHashMap

public MultiHashMap(int initialCapacity,
                    float loadFactor)
Constructor.

Parameters:
initialCapacity - the initial map capacity
loadFactor - the amount 0.0-1.0 at which to resize the map
Method Detail

clear

public void clear()
Clear the map. This clears each collection in the map, and so may be slow.

Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.HashMap

clone

public java.lang.Object clone()
Clones the map creating an independent copy. The clone will shallow clone the collections as well as the map.

Overrides:
clone in class java.util.HashMap
Returns:
the cloned map

containsValue

public boolean containsValue(java.lang.Object value)
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 interface java.util.Map
Overrides:
containsValue in class java.util.HashMap
Parameters:
value - the value to search for
Returns:
true if the map contains the value

containsValue

public boolean containsValue(java.lang.Object key,
                             java.lang.Object value)
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

getCollection

public java.util.Collection getCollection(java.lang.Object key)
Gets the collection mapped to the specified key. This method is a convenience method to typecast the result of get(key).

Parameters:
key - the key to retrieve
Returns:
the collection mapped to the key, null if no mapping
Since:
Commons Collections 3.1

iterator

public java.util.Iterator iterator(java.lang.Object key)
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

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object 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.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
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(java.lang.Object key,
                      java.util.Collection values)
Adds a collection of values to the collection associated with the specified key.

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

remove

public java.lang.Object remove(java.lang.Object key,
                               java.lang.Object 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(java.lang.Object 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
Since:
Commons Collections 3.1

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
Since:
Commons Collections 3.1

values

public java.util.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.

Specified by:
values in interface java.util.Map
Overrides:
values in class java.util.HashMap
Returns:
a collection view of the values contained in this map