K
- the key type.V
- the value type (i.e. the values stored in the collections).C
- the collection type, which is made explicit at the interface.public abstract class CollectionMap<K,V,C extends java.util.Collection<V>> extends java.lang.Object implements java.io.Serializable, java.lang.Iterable<java.util.Map.Entry<K,C>>
If you deal with the basic case of Map
s and List
s, use the
ListMap
, which is much easier to apply.
Constructor and Description |
---|
CollectionMap()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(K key,
V value)
Adds a value to the collection associated with the given key.
|
void |
addAll(CollectionMap<K,V,C> other)
Adds all elements from another collection map.
|
boolean |
addAll(K key,
java.util.Collection<? extends V> values)
Adds all values to the collection associated with the given key.
|
void |
addAll(java.util.Map<K,C> other)
Adds all elements from a Java map.
|
void |
clear()
Clears the underlying map and thus all contents.
|
java.util.Map<K,V[]> |
collectionsToArrays(java.lang.Class<V> type)
Converts the
CollectionMap to a map with arrays |
boolean |
contains(K key,
V value)
Returns whether an element is contained.
|
boolean |
containsCollection(K key)
Check if a (possibly empty) collection is present for a given key.
|
protected abstract C |
createNewCollection()
Returns a new instance of the underlying collection to be used as value for
the underlaying map.
|
protected java.util.Map<K,C> |
createUnderlyingMap()
Create a new instance of the underlying map, called exactly once during the
constructor.
|
static <K,V,C extends java.util.Collection<V>> |
createWithCollectionSupplier(java.util.function.Supplier<C> collectionInitializer)
Helper method to create a new CollectionMap with a given type of Collection
initializer.
|
C |
getCollection(K key)
Returns the collection stored under the given key (or null).
|
C |
getCollectionOrElse(K key,
C defaultCollection)
Returns the collection stored under the given key (or the given default if no
collection is stored).
|
C |
getCollectionOrEmpty(K key)
Returns the collection stored under the given key (or an empty collection).
|
UnmodifiableSet<K> |
getKeys()
Get the keys.
|
int |
getValueCount()
Return the total count of values over all collections.
|
C |
getValues()
Return all values from all collections.
|
java.util.Iterator<java.util.Map.Entry<K,C>> |
iterator() |
boolean |
remove(K key,
V value)
Removes an element.
|
boolean |
removeCollection(K key)
Removes the collection stored for a key.
|
int |
size()
Returns the size of this map, i.e.
|
java.lang.String |
toString() |
public CollectionMap()
protected java.util.Map<K,C> createUnderlyingMap()
protected abstract C createNewCollection()
public C getCollection(K key)
public C getCollectionOrElse(K key, C defaultCollection)
public C getCollectionOrEmpty(K key)
public boolean add(K key, V value)
true
if the collection associated with the given key
changed as a result of the call.public boolean addAll(K key, java.util.Collection<? extends V> values)
true
if the collection associated with the given key
changed as a result of the call.public void addAll(CollectionMap<K,V,C> other)
public boolean remove(K key, V value)
public boolean containsCollection(K key)
public boolean removeCollection(K key)
public UnmodifiableSet<K> getKeys()
public int size()
public int getValueCount()
public void clear()
public java.util.Map<K,V[]> collectionsToArrays(java.lang.Class<V> type)
CollectionMap
to a map with arraystype
- Type of the target arraypublic java.lang.String toString()
toString
in class java.lang.Object
public static <K,V,C extends java.util.Collection<V>> CollectionMap<K,V,C> createWithCollectionSupplier(java.util.function.Supplier<C> collectionInitializer)
CollectionMap.createWithCollectionInitializer(() -> new LinkedList());