K
- Key type of mapV
- Type of the Collection that is the Map's valuepublic class CollectionValuedMap<K,V>
extends java.lang.Object
implements java.util.Map<K,java.util.Collection<V>>, java.io.Serializable
Collection
s. Important methods are the add(K, V)
and remove(java.lang.Object)
methods for adding and removing a value to/from the
Collection associated with the key, and the get(java.lang.Object)
method for getting
the Collection associated with a key. The class is quite general, because on
construction, it is possible to pass a MapFactory
which will be used
to create the underlying map and a CollectionFactory
which will be
used to create the Collections. Thus this class can be configured to act like
a "HashSetValuedMap" or a "ListValuedMap", or even a
"HashSetValuedIdentityHashMap". The possibilities are endless!Modifier and Type | Field and Description |
---|---|
protected CollectionFactory<V> |
cf |
protected MapFactory<K,java.util.Collection<V>> |
mf |
protected boolean |
treatCollectionsAsImmutable |
Constructor and Description |
---|
CollectionValuedMap()
Creates a new empty CollectionValuedMap which uses a HashMap as the
underlying Map, and HashSets as the Collections in each mapping.
|
CollectionValuedMap(CollectionFactory<V> cf)
Creates a new empty CollectionValuedMap which uses a HashMap as the
underlying Map.
|
CollectionValuedMap(CollectionValuedMap<K,V> cvm)
Creates a new CollectionValuedMap with all of the mappings from cvm.
|
CollectionValuedMap(MapFactory<K,java.util.Collection<V>> mf,
CollectionFactory<V> cf,
boolean treatCollectionsAsImmutable)
Creates a new empty CollectionValuedMap.
|
Modifier and Type | Method and Description |
---|---|
void |
add(K key,
V value)
Adds the value to the Collection mapped to by the key.
|
void |
addAll(CollectionValuedMap<K,V> cvm) |
void |
addAll(K key,
java.util.Collection<V> values)
Adds the values to the Collection mapped to by the key.
|
void |
addAll(java.util.Map<K,V> m)
Adds all of the mappings in m to this CollectionValuedMap.
|
void |
addKey(K key)
Just add the key (empty collection, but key is in the keySet).
|
java.util.Collection<V> |
allValues() |
void |
clear()
Clears this Map.
|
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value)
Unsupported.
|
CollectionValuedMap<K,V> |
deltaCopy()
Creates a "delta copy" of this Map, where only the differences
from the original Map are represented.
|
java.util.Set<java.util.Map.Entry<K,java.util.Collection<V>>> |
entrySet()
Each element of the Set is a Map.Entry object, where getKey() returns the
key of the mapping, and getValue() returns the Collection mapped to by the
key.
|
boolean |
equals(java.lang.Object o) |
java.util.Collection<V> |
get(java.lang.Object key) |
int |
hashCode() |
boolean |
isEmpty() |
java.util.Set<K> |
keySet() |
java.util.Collection<V> |
put(K key,
java.util.Collection<V> collection)
Replaces current Collection mapped to key with the specified Collection.
|
void |
putAll(java.util.Map<? extends K,? extends java.util.Collection<V>> m)
Unsupported.
|
java.util.Collection<V> |
remove(java.lang.Object key)
Removes the mapping associated with this key from this Map.
|
void |
removeAll(java.util.Collection<K> keys)
Removes the mappings associated with the keys from this map.
|
void |
removeMapping(K key,
V value)
Removes the value from the Collection mapped to by this key, leaving the
rest of the collection intact.
|
int |
size()
The number of keys in this map.
|
java.lang.String |
toString() |
java.util.Collection<java.util.Collection<V>> |
values() |
protected final CollectionFactory<V> cf
protected final boolean treatCollectionsAsImmutable
protected final MapFactory<K,java.util.Collection<V>> mf
public CollectionValuedMap(MapFactory<K,java.util.Collection<V>> mf, CollectionFactory<V> cf, boolean treatCollectionsAsImmutable)
mf
- A MapFactory which will be used to generate the underlying Mapcf
- A CollectionFactory which will be used to generate the Collections
in each mappingtreatCollectionsAsImmutable
- If true, forces this Map to create new a Collection every time a
new value is added to or deleted from the Collection a mapping.public CollectionValuedMap(CollectionValuedMap<K,V> cvm)
cvm
- The CollectionValueMap to copy as this object.public CollectionValuedMap()
public CollectionValuedMap(CollectionFactory<V> cf)
cf
- A CollectionFactory which will be used to generate the Collections
in each mappingpublic java.util.Collection<V> put(K key, java.util.Collection<V> collection)
public void putAll(java.util.Map<? extends K,? extends java.util.Collection<V>> m)
addAll(Map)
instead.public java.util.Collection<V> get(java.lang.Object key)
public void addAll(K key, java.util.Collection<V> values)
public void addKey(K key)
public void addAll(java.util.Map<K,V> m)
public void addAll(CollectionValuedMap<K,V> cvm)
public java.util.Collection<V> remove(java.lang.Object key)
public void removeAll(java.util.Collection<K> keys)
keys
- They keys to removepublic void removeMapping(K key, V value)
key
- The key to the Collection to remove the value fromvalue
- The value to removepublic void clear()
public boolean containsKey(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
public boolean isEmpty()
public java.util.Set<java.util.Map.Entry<K,java.util.Collection<V>>> entrySet()
public java.util.Set<K> keySet()
public int size()
public java.util.Collection<java.util.Collection<V>> values()
public java.util.Collection<V> allValues()
public boolean equals(java.lang.Object o)
public int hashCode()
public CollectionValuedMap<K,V> deltaCopy()
public java.lang.String toString()
toString
in class java.lang.Object