nl.openedge.baritus.util
Class MultiHashMap

java.lang.Object
  extended byjava.util.AbstractMap
      extended byjava.util.HashMap
          extended bynl.openedge.baritus.util.MultiHashMap
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable

public class MultiHashMap
extends java.util.HashMap
implements java.util.Map

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 always return a Collection, holding all the values put to that key. This implementation uses an ArrayList as the collection.

For example:

 MultiMap mhm = new MultiHashMap();
 mhm.put(key, "A");
 mhm.put(key, "B");
 mhm.put(key, "C");
 Collection coll = mhm.get(key);

coll will be a list containing "A", "B", "C". NOTE: copied this from Commons Collections, as this is the only class used from the Collections package, and it's for internal use. Commons Collections 2.0 Revision: 1.11 $ $Date: 2003/05/16 14:40:56

Author:
Christopher Berry, James Strachan, Steve Downey, Stephen Colebourne, Julien Buret, Serhiy Yevtushenko
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
MultiHashMap()
          Constructor.
MultiHashMap(int initialCapacity)
          Constructor.
MultiHashMap(int initialCapacity, float loadFactor)
          Constructor.
MultiHashMap(java.util.Map mapToCopy)
          Constructor.
 
Method Summary
 void clear()
          Clear the map.
 java.lang.Object clone()
          Clone the map.
 boolean containsValue(java.lang.Object value)
          Does the map contain a specific value.
protected  java.util.Collection createCollection(java.util.Collection coll)
          Creates a new instance of the map value Collection container.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Put a key and value into the map.
 java.lang.Object remove(java.lang.Object key, java.lang.Object value)
          Removes a specific value from map.
 java.util.Collection values()
          Gets a view over 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
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, get, hashCode, isEmpty, keySet, putAll, remove, size
 

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

MultiHashMap

public MultiHashMap(java.util.Map mapToCopy)
Constructor.

Parameters:
mapToCopy - a Map to copy
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Put a key and value into the map.

The value is added to a collection mapped to the key instead of replacing the previous value.

Specified by:
put in interface java.util.Map
Parameters:
key - the key to set
value - the value to set the key to
Returns:
the value added if the add is successful, null otherwise

containsValue

public boolean containsValue(java.lang.Object value)
Does the map contain a specific value.

This searches the collection mapped to each key, and thus could be slow.

Specified by:
containsValue in interface java.util.Map
Parameters:
value - the value to search for
Returns:
true if the list contains the value

remove

public java.lang.Object remove(java.lang.Object key,
                               java.lang.Object value)
Removes a specific value from map.

The item is removed from the collection mapped to the specified key.

Parameters:
key - the key to remove from
value - the value to remove
Returns:
the value removed (which was passed in)

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

values

public java.util.Collection values()
Gets a view over all the values in the map.

The values view includes all the entries in the collections at each map key.

Specified by:
values in interface java.util.Map
Returns:
the collection view of all the values in the map

clone

public java.lang.Object clone()
Clone the map.

The clone will shallow clone the collections as well as the map.

Returns:
the cloned map

createCollection

protected java.util.Collection createCollection(java.util.Collection 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


Copyright © 2003-2004 Open Edge. All Rights Reserved.