Class MultiMapUtils
MultiValuedMap instances.
It contains various type safe and null safe methods. Additionally, it provides the following decorators:
- Since:
- 4.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MultiValuedMapAn emptyUnmodifiableMultiValuedMap. -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> MultiValuedMap <K, V> emptyIfNull(MultiValuedMap<K, V> map) Returns an immutable emptyMultiValuedMapif the argument isnull, or the argument itself otherwise.static <K,V> MultiValuedMap <K, V> Returns immutable EMPTY_MULTI_VALUED_MAP with generic type safety.static <K,V> Collection <V> getCollection(MultiValuedMap<K, V> map, K key) Gets a Collection fromMultiValuedMapin a null-safe manner.static <K,V> Bag <V> getValuesAsBag(MultiValuedMap<K, V> map, K key) Deprecated.static <K,V> List <V> getValuesAsList(MultiValuedMap<K, V> map, K key) Gets a List fromMultiValuedMapin a null-safe manner.static <K,V> MultiSet <V> getValuesAsMultiSet(MultiValuedMap<K, V> map, K key) Gets a MultiSet fromMultiValuedMapin a null-safe manner.static <K,V> Set <V> getValuesAsSet(MultiValuedMap<K, V> map, K key) Gets a Set fromMultiValuedMapin a null-safe manner.static <K,V, M extends MultiValuedMap<K, V>>
Minvert(MultiValuedMap<? extends V, ? extends K> input, M output) Inverts the mappings from an input MultiValuedMap by adding entries to an output MultiValuedMap.static booleanisEmpty(MultiValuedMap<?, ?> map) Null-safe check if the specifiedMultiValuedMapis empty.static <K,V> ListValuedMap <K, V> Creates aListValuedMapwith anArrayListas collection class to store the values mapped to a key.static <K,V> SetValuedMap <K, V> Creates aSetValuedMapwith anHashSetas collection class to store the values mapped to a key.static <K,V> MultiValuedMap <K, V> transformedMultiValuedMap(MultiValuedMap<K, V> map, Transformer<? super K, ? extends K> keyTransformer, Transformer<? super V, ? extends V> valueTransformer) Returns aTransformedMultiValuedMapbacked by the given map.static <K,V> MultiValuedMap <K, V> unmodifiableMultiValuedMap(MultiValuedMap<? extends K, ? extends V> map) Returns anUnmodifiableMultiValuedMapbacked by the given map.
-
Field Details
-
EMPTY_MULTI_VALUED_MAP
An emptyUnmodifiableMultiValuedMap.
-
-
Method Details
-
emptyIfNull
Returns an immutable emptyMultiValuedMapif the argument isnull, or the argument itself otherwise.- Type Parameters:
K- The type of key in the map.V- The type of value in the map.- Parameters:
map- The map, may be null.- Returns:
- An empty
MultiValuedMapif the argument is null.
-
emptyMultiValuedMap
Returns immutable EMPTY_MULTI_VALUED_MAP with generic type safety.- Type Parameters:
K- The type of key in the map.V- The type of value in the map.- Returns:
- immutable and empty
MultiValuedMap.
-
getCollection
Gets a Collection fromMultiValuedMapin a null-safe manner.- Type Parameters:
K- The key type.V- The value type.- Parameters:
map- TheMultiValuedMapto use.key- The key to look up.- Returns:
- The Collection in the
MultiValuedMap, or null if input map is null.
-
getValuesAsBag
Deprecated.Since 4.6.0, usegetValuesAsMultiSet(MultiValuedMap, Object)instead.Gets a Bag fromMultiValuedMapin a null-safe manner.- Type Parameters:
K- The key type.V- The value type.- Parameters:
map- TheMultiValuedMapto use.key- The key to look up.- Returns:
- A new Bag containing the values from the
MultiValuedMap, or null if input map is null.
-
getValuesAsList
Gets a List fromMultiValuedMapin a null-safe manner.- Type Parameters:
K- The key type.V- The value type.- Parameters:
map- TheMultiValuedMapto use.key- The key to look up.- Returns:
- A new List containing the values from the
MultiValuedMap, or null if input map is null.
-
getValuesAsMultiSet
Gets a MultiSet fromMultiValuedMapin a null-safe manner.- Type Parameters:
K- The key type.V- The value type.- Parameters:
map- TheMultiValuedMapto use.key- The key to look up.- Returns:
- A new MultiSet containing the values from the
MultiValuedMap, or null if input map is null. - Since:
- 4.6.0
-
getValuesAsSet
Gets a Set fromMultiValuedMapin a null-safe manner.- Type Parameters:
K- The key type.V- The value type.- Parameters:
map- TheMultiValuedMapto use.key- The key to look up.- Returns:
- A new Set containing the values from the
MultiValuedMap, or null if input map is null.
-
invert
public static <K,V, M invertM extends MultiValuedMap<K, V>> (MultiValuedMap<? extends V, ? extends K> input, M output) Inverts the mappings from an input MultiValuedMap by adding entries to an output MultiValuedMap. The input is unchanged.Use this method to have complete control of the output MultiValuedMap or when merging several inverse mappings. In simple cases, consider using
MultiValuedMap.inverted()method.- Type Parameters:
K- the input value type and output key type.V- the input key type and output value type.M- the output MultiValuedMap type whereKis the key type andVis the value type.- Parameters:
input- The input key-value mappings of type<V, K>.output- The output value-key mappings of type<K, V>.- Returns:
- The updated output MultiValuedMap of type
<K, V> - Since:
- 4.6.0
- See Also:
-
isEmpty
Null-safe check if the specifiedMultiValuedMapis empty.If the provided map is null, returns true.
- Parameters:
map- The map to check, may be null.- Returns:
- true if the map is empty or null.
-
newListValuedHashMap
Creates aListValuedMapwith anArrayListas collection class to store the values mapped to a key.- Type Parameters:
K- The key type.V- The value type.- Returns:
- A new
ListValuedMap.
-
newSetValuedHashMap
Creates aSetValuedMapwith anHashSetas collection class to store the values mapped to a key.- Type Parameters:
K- The key type.V- The value type.- Returns:
- A new
SetValuedMap.
-
transformedMultiValuedMap
public static <K,V> MultiValuedMap<K,V> transformedMultiValuedMap(MultiValuedMap<K, V> map, Transformer<? super K, ? extends K> keyTransformer, Transformer<? super V, ? extends V> valueTransformer) Returns aTransformedMultiValuedMapbacked by the given map.This method returns a new
MultiValuedMap(decorating the specified map) that will transform any new entries added to it. Existing entries in the specified map will not be transformed. If you want that behavior, seeTransformedMultiValuedMap.transformedMap(org.apache.commons.collections4.MultiValuedMap<K, V>, org.apache.commons.collections4.Transformer<? super K, ? extends K>, org.apache.commons.collections4.Transformer<? super V, ? extends V>).Each object is passed through the transformers as it is added to the Map. It is important not to use the original map after invoking this method, as it is a back door for adding untransformed objects.
If there are any elements already in the map being decorated, they are NOT transformed.
- Type Parameters:
K- the key type.V- the value type.- Parameters:
map- TheMultiValuedMapto transform, must not be null, typically empty.keyTransformer- The transformer for the map keys, null means no transformation.valueTransformer- The transformer for the map values, null means no transformation.- Returns:
- A transformed
MultiValuedMapbacked by the given map. - Throws:
NullPointerException- if map is null.
-
unmodifiableMultiValuedMap
public static <K,V> MultiValuedMap<K,V> unmodifiableMultiValuedMap(MultiValuedMap<? extends K, ? extends V> map) Returns anUnmodifiableMultiValuedMapbacked by the given map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
map- TheMultiValuedMapto decorate, must not be null.- Returns:
- An unmodifiable
MultiValuedMapbacked by the provided map. - Throws:
NullPointerException- if map is null.
-
getValuesAsMultiSet(MultiValuedMap, Object)instead.