K
- the key type used by the mapV
- the value type used by the mappublic class MultiMap<K,V> extends Object
Provides an implementation which simulates a Map<K, Collection<V>>
by providing
specific put, get and remove methods.
Modifier and Type | Field and Description |
---|---|
protected Map<K,Collection<V>> |
base |
Modifier | Constructor and Description |
---|---|
protected |
MultiMap(Map<K,Collection<V>> base)
Used the static factory methods create or createdSynchronized to obtain an instance.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all entries from this map
|
static <K,V> MultiMap<K,V> |
create()
Creates a new MultiMap for the specified types which is not thread safe.
|
static <K,V> MultiMap<K,V> |
createOrdered()
Creates a new MultiMap for the specified types which is not thread safe but keeps its insertion order.
|
static <K,V> MultiMap<K,V> |
createSynchronized()
Creates a new MultiMap for the specified types which is thread safe.
|
protected List<V> |
createValueList()
Can be overridden to specify the subclass of List used to store value lists.
|
Collection<V> |
get(K key)
Returns the value list for the given key.
|
Map<K,Collection<V>> |
getUnderlyingMap()
Provides direct access to the underlying map.
|
static <K,V> Collector<V,MultiMap<K,V>,MultiMap<K,V>> |
groupingBy(Supplier<MultiMap<K,V>> supplier,
Function<V,K> classifier)
Creates a
Collector which can be used to group a stream into a multi map. |
static <K,V> Collector<V,MultiMap<K,V>,MultiMap<K,V>> |
groupingByMultiple(Supplier<MultiMap<K,V>> supplier,
Function<V,Collection<K>> classifier)
Creates a
Collector which can be used to group a stream into a multi map. |
Set<K> |
keySet()
Returns the set of known keys.
|
MultiMap<K,V> |
merge(MultiMap<K,V> other)
Merges the given multi map into this one.
|
void |
put(K key,
V value)
Adds the given value to the list of values kept for the given key.
|
void |
remove(K key,
V value)
Removes all occurrences of the given value in the value list of the given key.
|
void |
set(K key,
V value)
Sets the given value to the given name.
|
Stream<Map.Entry<K,Collection<V>>> |
stream()
Boilerplate method to access all entries of this map as
Stream . |
String |
toString() |
List<V> |
values()
Returns a list of all values for all keys.
|
protected Map<K,Collection<V>> base
protected MultiMap(Map<K,Collection<V>> base)
base
- the underlying map to usepublic static <K,V> MultiMap<K,V> create()
K
- the type of the keys used in the mapV
- the type of the values used withing the value lists of the mappublic static <K,V> MultiMap<K,V> createOrdered()
K
- the type of the keys used in the mapV
- the type of the values used withing the value lists of the mappublic static <K,V> MultiMap<K,V> createSynchronized()
K
- the type of the keys used in the mapV
- the type of the values used withing the value lists of the mappublic void put(@Nonnull K key, @Nullable V value)
Note that the values for a given key don't from a Set. Therefore adding the same value twice for the same key, will result in having a value list containing the added element twice.
key
- the key for which the value is added to the mapvalue
- the value which is added to the list of values for this keypublic void set(@Nonnull K key, @Nullable V value)
All previously set values will be removed.
key
- the key for which the value is added to the mapvalue
- the name (and only) value for the given keyprotected List<V> createValueList()
public void remove(@Nonnull K key, @Nullable V value)
If the value does not occur in the value list or if the key is completely unknown, nothing will happen.
key
- the key of which value list the value will be removed fromvalue
- the value which will be removed from the value list@Nonnull public Collection<V> get(@Nonnull K key)
If the key is completely unknown, an empty list will be returned.
key
- the key which value list is to be returned@Nonnull public Set<K> keySet()
@Nonnull public Map<K,Collection<V>> getUnderlyingMap()
For the sake of simplicity and extensibility, the original map is returned. Therefore manipulations should be well considered.
@Nonnull public List<V> values()
Note that this list has no Set like behaviour. Therefore the same value might occur several times if it was added more than once for the same or for different keys.
public void clear()
public MultiMap<K,V> merge(MultiMap<K,V> other)
If both maps contain values for the same key, to lists will be joined together.
Note: This will modify the callee instead of creating a new result map
other
- the other map to merge into this onepublic static <K,V> Collector<V,MultiMap<K,V>,MultiMap<K,V>> groupingBy(Supplier<MultiMap<K,V>> supplier, Function<V,K> classifier)
Collector
which can be used to group a stream into a multi map.K
- the extracted key type of the mapV
- the value type of the incoming stream and outgoing mapsupplier
- the factory for creating the result mapclassifier
- the method used to extract the key from the elementsStream.collect(java.util.stream.Collector)
public static <K,V> Collector<V,MultiMap<K,V>,MultiMap<K,V>> groupingByMultiple(Supplier<MultiMap<K,V>> supplier, Function<V,Collection<K>> classifier)
Collector
which can be used to group a stream into a multi map.
This method permits the classifier function to return multiple keys for a single element. The element will be added for all returned keys.
K
- the extracted key type of the mapV
- the value type of the incoming stream and outgoing mapsupplier
- the factory for creating the result mapclassifier
- the method used to extract the keys from the elementsStream.collect(java.util.stream.Collector)
public Stream<Map.Entry<K,Collection<V>>> stream()
Stream
.
Note: Calling Tuple.flatten(java.util.Map.Entry)
via
Stream.flatMap(java.util.function.Function)
will transform the resulting stream into a stream of
all pairs represented by this map.
Copyright © 2018. All rights reserved.