public class MultiMap<K,V>
extends java.lang.Object
implements java.util.Map<K,java.util.Collection<V>>
MultiMap<K,V>
is, in fact, a subtype of Map<K,Collection<V>>
; this type adds a small number
of methods that accept values of V
directly, rather than
values of Collection<V>
.
MultiMap
uses an underlying map to store the keys and associated
collections, and uses reflection to create the collections when needed.
From the default constructor, the underlying map is a LinkedHashMap
,
and the collections class is ArrayList
; other constructors allow
other classes to be specified (for example, ArraySortedSet
for the
collections class).
If the collection of values associated with a key becomes empty for any
reason, the collection is removed from the map. This ensures, for example,
that isEmpty() == (valuesSize() == 0)
and that
containsKey(key) == (valuesSize(key) > 0)
.Modifier | Constructor and Description |
---|---|
|
MultiMap()
|
|
MultiMap(java.lang.Class<? extends java.util.Collection> collectionClass)
Creates a new
MultiMap based on a map of type LinkedHashMap
and collections of a specified type. |
|
MultiMap(java.lang.Class<? extends java.util.Map> mapClass,
java.lang.Class<? extends java.util.Collection> collectionClass)
Creates a new
MultiMap based on a map of a specified type and
collections of a specified type. |
protected |
MultiMap(java.util.Map<K,java.util.Collection<V>> delegate,
java.lang.Class<? extends java.util.Collection> collectionClass) |
|
MultiMap(MultiMap<K,V> map) |
Modifier and Type | Method and Description |
---|---|
boolean |
add(K key,
V value)
Adds a value to the collection associated with a key.
|
boolean |
addAll(K key,
java.util.Collection<V> values)
Adds a colleciton of values to the collection associated with a key.
|
boolean |
addAll(K key,
V... values)
Adds an array of values to the collection associated with a key.
|
boolean |
addAll(java.util.Map<? extends K,V> map)
Adds each value in a map to the collection associated with its key.
|
void |
clear() |
boolean |
contains(K key,
V value)
Gets whether the collection associated with a key contains a value.
|
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value) |
java.util.Set<java.util.Map.Entry<K,java.util.Collection<V>>> |
entrySet() |
boolean |
equals(java.lang.Object o) |
java.util.Collection<V> |
get(java.lang.Object key) |
static <K,V> MultiMap<K,V> |
getMultiMap()
|
static <K,V> MultiMap<K,V> |
getMultiMap(java.lang.Class<? extends java.util.Collection> collectionClass)
Creates a new
MultiMap based on a map of type LinkedHashMap
and collections of a specified type. |
static <K,V> MultiMap<K,V> |
getMultiMap(java.lang.Class<? extends java.util.Map> mapClass,
java.lang.Class<? extends java.util.Collection> collectionClass)
Creates a new
MultiMap based on a map of a specified type and
collections of a specified type. |
int |
hashCode() |
boolean |
isEmpty() |
java.util.Set<K> |
keySet() |
int |
keysSize()
Gets the number of keys in this map.
|
java.util.Collection<V> |
put(K key,
java.util.Collection<V> values) |
void |
putAll(java.util.Map<? extends K,? extends java.util.Collection<V>> map) |
V |
remove(K key,
V value)
Removes a value from the collection associated with a key.
|
java.util.Collection<V> |
remove(java.lang.Object key) |
int |
size() |
java.lang.String |
toString() |
java.util.Collection<java.util.Collection<V>> |
values() |
int |
valuesSize()
Gets the number of values in this map.
|
int |
valuesSize(K key)
Gets the number of values in this map associated with a key.
|
public MultiMap()
public MultiMap(java.lang.Class<? extends java.util.Collection> collectionClass)
MultiMap
based on a map of type LinkedHashMap
and collections of a specified type.public MultiMap(java.lang.Class<? extends java.util.Map> mapClass, java.lang.Class<? extends java.util.Collection> collectionClass)
MultiMap
based on a map of a specified type and
collections of a specified type.protected MultiMap(java.util.Map<K,java.util.Collection<V>> delegate, java.lang.Class<? extends java.util.Collection> collectionClass)
public static <K,V> MultiMap<K,V> getMultiMap()
public static <K,V> MultiMap<K,V> getMultiMap(java.lang.Class<? extends java.util.Collection> collectionClass)
MultiMap
based on a map of type LinkedHashMap
and collections of a specified type.public static <K,V> MultiMap<K,V> getMultiMap(java.lang.Class<? extends java.util.Map> mapClass, java.lang.Class<? extends java.util.Collection> collectionClass)
MultiMap
based on a map of a specified type and
collections of a specified type.public int keysSize()
size()
, but
provided for symmetry with valuesSize()
.public int valuesSize()
public int valuesSize(K key)
public boolean contains(K key, V value)
Map.containsValue(Object)
,
Collection.contains(java.lang.Object)
public boolean add(K key, V value)
put(K, java.util.Collection<V>)
,
Collection.add(E)
public boolean addAll(K key, java.util.Collection<V> values)
public boolean addAll(K key, V... values)
public boolean addAll(java.util.Map<? extends K,V> map)
putAll(java.util.Map<? extends K, ? extends java.util.Collection<V>>)
,
Collection.addAll(java.util.Collection<? extends E>)
public V remove(K key, V value)
remove(Object)
,
Collection.remove(java.lang.Object)
public boolean isEmpty()
public boolean containsKey(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
Because of the potential ambiguity about whether the value is a V
or a Collection<V>
, this implementation returns true in
either case.
public java.util.Collection<V> get(java.lang.Object key)
public java.util.Collection<V> remove(java.lang.Object key)
public java.util.Set<K> keySet()
public java.util.Collection<java.util.Collection<V>> values()
public boolean equals(java.lang.Object o)
public int hashCode()
public java.lang.String toString()
toString
in class java.lang.Object