K
- Key type of mapV
- Type of the Collection that is the Map's valuepublic class CollectionValuedMap<K,V> extends Object implements Map<K,Collection<V>>, 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,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,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,
Collection<V> values)
Adds the values to the Collection mapped to by the key.
|
void |
addAll(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).
|
Collection<V> |
allValues() |
void |
clear()
Clears this Map.
|
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value)
Unsupported.
|
CollectionValuedMap<K,V> |
deltaCopy()
Creates a "delta copy" of this Map, where only the differences
from the original Map are represented.
|
Set<Map.Entry<K,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(Object o) |
Collection<V> |
get(Object key) |
int |
hashCode() |
boolean |
isEmpty() |
Set<K> |
keySet() |
Collection<V> |
put(K key,
Collection<V> collection)
Replaces current Collection mapped to key with the specified Collection.
|
void |
putAll(Map<? extends K,? extends Collection<V>> m)
Unsupported.
|
Collection<V> |
remove(Object key)
Removes the mapping associated with this key from this Map.
|
void |
removeAll(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.
|
String |
toString() |
Collection<Collection<V>> |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
protected final CollectionFactory<V> cf
protected final boolean treatCollectionsAsImmutable
protected final MapFactory<K,Collection<V>> mf
public CollectionValuedMap(MapFactory<K,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 Collection<V> put(K key, Collection<V> collection)
put
in interface Map<K,Collection<V>>
public void putAll(Map<? extends K,? extends Collection<V>> m)
addAll(Map)
instead.putAll
in interface Map<K,Collection<V>>
public Collection<V> get(Object key)
get
in interface Map<K,Collection<V>>
public void addAll(K key, Collection<V> values)
public void addKey(K key)
public void addAll(Map<K,V> m)
public void addAll(CollectionValuedMap<K,V> cvm)
public Collection<V> remove(Object key)
remove
in interface Map<K,Collection<V>>
public void removeAll(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()
clear
in interface Map<K,Collection<V>>
public boolean containsKey(Object key)
containsKey
in interface Map<K,Collection<V>>
public boolean containsValue(Object value)
containsValue
in interface Map<K,Collection<V>>
public boolean isEmpty()
isEmpty
in interface Map<K,Collection<V>>
public Set<Map.Entry<K,Collection<V>>> entrySet()
entrySet
in interface Map<K,Collection<V>>
public Set<K> keySet()
keySet
in interface Map<K,Collection<V>>
public int size()
size
in interface Map<K,Collection<V>>
public Collection<Collection<V>> values()
values
in interface Map<K,Collection<V>>
public Collection<V> allValues()
public boolean equals(Object o)
public int hashCode()
public CollectionValuedMap<K,V> deltaCopy()