Interface MultiMap
-
- All Superinterfaces:
java.util.Map
- All Known Implementing Classes:
MultiHashMap,MultiValueMap
public interface MultiMap extends java.util.MapDefines a map that holds a collection of values against each key.A
MultiMapis 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.For example:
MultiMap mhm = new MultiHashMap(); mhm.put(key, "A"); mhm.put(key, "B"); mhm.put(key, "C"); Collection coll = (Collection) mhm.get(key);
collwill be a collection containing "A", "B", "C".NOTE: Additional methods were added to this interface in Commons Collections 3.1. These were added solely for documentation purposes and do not change the interface as they were defined in the superinterface
Mapanyway.- Since:
- Commons Collections 2.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleancontainsValue(java.lang.Object value)Checks whether the map contains the value specified.java.lang.Objectget(java.lang.Object key)Gets the collection of values associated with the specified key.java.lang.Objectput(java.lang.Object key, java.lang.Object value)Adds the value to the collection associated with the specified key.java.lang.Objectremove(java.lang.Object key)Removes all values associated with the specified key.java.lang.Objectremove(java.lang.Object key, java.lang.Object item)Removes a specific value from map.intsize()Gets the number of keys in this map.java.util.Collectionvalues()Gets a collection containing all the values in the map.
-
-
-
Method Detail
-
remove
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, implementations typically return
nullfrom a subsequantget(Object), however they may choose to return an empty collection.- Specified by:
removein interfacejava.util.Map- Parameters:
key- the key to remove fromitem- the item to remove- Returns:
- the value removed (which was passed in), null if nothing removed
- Throws:
java.lang.UnsupportedOperationException- if the map is unmodifiablejava.lang.ClassCastException- if the key or value is of an invalid typejava.lang.NullPointerException- if the key or value is null and null is invalid
-
size
int size()
Gets the number of keys in this map.Implementations typically return only the count of keys in the map This cannot be mandated due to backwards compatability of this interface.
- Specified by:
sizein interfacejava.util.Map- Returns:
- the number of key-collection mappings in this map
-
get
java.lang.Object get(java.lang.Object key)
Gets the collection of values associated with the specified key.The returned value will implement
Collection. Implementations are free to declare that they returnCollectionsubclasses such asListorSet.Implementations typically return
nullif no values have been mapped to the key, however the implementation may choose to return an empty collection.Implementations may choose to return a clone of the internal collection.
- Specified by:
getin interfacejava.util.Map- Parameters:
key- the key to retrieve- Returns:
- the
Collectionof values, implementations should returnnullfor no mapping, but may return an empty collection - Throws:
java.lang.ClassCastException- if the key is of an invalid typejava.lang.NullPointerException- if the key is null and null keys are invalid
-
containsValue
boolean containsValue(java.lang.Object value)
Checks whether the map contains the value specified.Implementations typically check all collections against all keys for the value. This cannot be mandated due to backwards compatability of this interface.
- Specified by:
containsValuein interfacejava.util.Map- Parameters:
value- the value to search for- Returns:
- true if the map contains the value
- Throws:
java.lang.ClassCastException- if the value is of an invalid typejava.lang.NullPointerException- if the value is null and null value are invalid
-
put
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
Mapthe previous value is not replaced. Instead the new value is added to the collection stored against the key. The collection may be aList,Setor other collection dependent on implementation.- Specified by:
putin interfacejava.util.Map- Parameters:
key- the key to store againstvalue- the value to add to the collection at the key- Returns:
- typically the value added if the map changed and null if the map did not change
- Throws:
java.lang.UnsupportedOperationException- if the map is unmodifiablejava.lang.ClassCastException- if the key or value is of an invalid typejava.lang.NullPointerException- if the key or value is null and null is invalidjava.lang.IllegalArgumentException- if the key or value is invalid
-
remove
java.lang.Object remove(java.lang.Object key)
Removes all values associated with the specified key.Implementations typically return
nullfrom a subsequantget(Object), however they may choose to return an empty collection.- Specified by:
removein interfacejava.util.Map- Parameters:
key- the key to remove values from- Returns:
- the
Collectionof values removed, implementations should returnnullfor no mapping found, but may return an empty collection - Throws:
java.lang.UnsupportedOperationException- if the map is unmodifiablejava.lang.ClassCastException- if the key is of an invalid typejava.lang.NullPointerException- if the key is null and null keys are invalid
-
values
java.util.Collection values()
Gets a collection containing all the values in the map.Inplementations typically return a collection containing the combination of values from all keys. This cannot be mandated due to backwards compatability of this interface.
- Specified by:
valuesin interfacejava.util.Map- Returns:
- a collection view of the values contained in this map
-
-