Top Description Fields Constructors Methods
org.apache.commons.collections4

public Class MultiMapUtils

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Collection, .HashSet, .List, .Set, org.apache.commons.collections4.bag.HashBag, org.apache.commons.collections4.multimap.ArrayListValuedHashMap, .HashSetValuedHashMap, .TransformedMultiValuedMap, .UnmodifiableMultiValuedMap

Provides utility methods and decorators for MultiValuedMap instances.

It contains various type safe and null safe methods. Additionally, it provides the following decorators:

Since
4.1

Field Summary

Modifier and TypeField and Description
public static final MultiValuedMap<K, V>

Constructor Summary

AccessConstructor and Description
private
MultiMapUtils()

MultiMapUtils should not normally be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of key in the map
K
,
the type of value in the map
V
>
MultiValuedMap<K, V>

Returns:

an empty MultiValuedMap if the argument is null
emptyIfNull
(final MultiValuedMap<K, V>
the map, may be null
map
)

Returns an immutable empty MultiValuedMap if the argument is null, or the argument itself otherwise.

public static <
the type of key in the map
K
,
the type of value in the map
V
>
MultiValuedMap<K, V>

Returns:

immutable and empty MultiValuedMap
emptyMultiValuedMap
()

Returns immutable EMPTY_MULTI_VALUED_MAP with generic type safety.

public static <
the key type
K
,
the value type
V
>
Collection<V>

Returns:

the Collection in the MultiValuedMap, or null if input map is null
getCollection
(final MultiValuedMap<K, V>
the MultiValuedMap to use
map
,
final K
the key to look up
key
)

Gets a Collection from MultiValuedMap in a null-safe manner.

public static <
the key type
K
,
the value type
V
>
Bag<V>

Returns:

the Collection in the MultiValuedMap as Bag, or null if input map is null
getValuesAsBag
(final MultiValuedMap<K, V>
the MultiValuedMap to use
map
,
final K
the key to look up
key
)

Gets a Bag from MultiValuedMap in a null-safe manner.

public static <
the key type
K
,
the value type
V
>
List<V>

Returns:

the Collection in the MultiValuedMap as List, or null if input map is null
getValuesAsList
(final MultiValuedMap<K, V>
the MultiValuedMap to use
map
,
final K
the key to look up
key
)

Gets a List from MultiValuedMap in a null-safe manner.

public static <
the key type
K
,
the value type
V
>
Set<V>

Returns:

the Collection in the MultiValuedMap as Set, or null if input map is null
getValuesAsSet
(final MultiValuedMap<K, V>
the MultiValuedMap to use
map
,
final K
the key to look up
key
)

Gets a Set from MultiValuedMap in a null-safe manner.

public static boolean

Returns:

true if the map is empty or null
isEmpty
(final MultiValuedMap<?, ?>
the map to check, may be null
map
)

Null-safe check if the specified MultiValuedMap is empty.

public static <
the key type
K
,
the value type
V
>
ListValuedMap<K, V>

Returns:

a new ListValuedMap
newListValuedHashMap
()

Creates a ListValuedMap with an ArrayList as collection class to store the values mapped to a key.

public static <
the key type
K
,
the value type
V
>
SetValuedMap<K, V>

Returns:

a new SetValuedMap
newSetValuedHashMap
()

Creates a SetValuedMap with an HashSet as collection class to store the values mapped to a key.

public static <
the key type
K
,
the value type
V
>
MultiValuedMap<K, V>

Returns:

a transformed MultiValuedMap backed by the given map
transformedMultiValuedMap
(final MultiValuedMap<K, V>
the MultiValuedMap to transform, must not be null, typically empty
map
,
final Transformer<? super K, ? extends K>
the transformer for the map keys, null means no transformation
keyTransformer
,
final Transformer<? super V, ? extends V>
the transformer for the map values, null means no transformation
valueTransformer
)

Returns a TransformedMultiValuedMap backed by the given map.

public static <
the key type
K
,
the value type
V
>
MultiValuedMap<K, V>

Returns:

an unmodifiable MultiValuedMap backed by the provided map
unmodifiableMultiValuedMap
(final MultiValuedMap<? extends K, ? extends V>
the MultiValuedMap to decorate, must not be null
map
)

Returns an UnmodifiableMultiValuedMap backed by the given map.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_MULTI_VALUED_MAPback to summary
public static final MultiValuedMap<K, V> EMPTY_MULTI_VALUED_MAP

An empty UnmodifiableMultiValuedMap.

Annotations
@SuppressWarnings:rawtypes, unchecked

Constructor Detail

MultiMapUtilsback to summary
private MultiMapUtils()

MultiMapUtils should not normally be instantiated.

Method Detail

emptyIfNullback to summary
public static <K, V> MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map)

Returns an immutable empty MultiValuedMap if the argument is null, or the argument itself otherwise.

Parameters
<K>
the type of key in the map
<V>
the type of value in the map
map:MultiValuedMap<K, V>

the map, may be null

Returns:MultiValuedMap<K, V>

an empty MultiValuedMap if the argument is null

Annotations
@SuppressWarnings:unchecked
emptyMultiValuedMapback to summary
public static <K, V> MultiValuedMap<K, V> emptyMultiValuedMap()

Returns immutable EMPTY_MULTI_VALUED_MAP with generic type safety.

Parameters
<K>
the type of key in the map
<V>
the type of value in the map
Returns:MultiValuedMap<K, V>

immutable and empty MultiValuedMap

Annotations
@SuppressWarnings:unchecked
getCollectionback to summary
public static <K, V> Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key)

Gets a Collection from MultiValuedMap in a null-safe manner.

Parameters
<K>
the key type
<V>
the value type
map:MultiValuedMap<K, V>

the MultiValuedMap to use

key:K

the key to look up

Returns:Collection<V>

the Collection in the MultiValuedMap, or null if input map is null

getValuesAsBagback to summary
public static <K, V> Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key)

Gets a Bag from MultiValuedMap in a null-safe manner.

Parameters
<K>
the key type
<V>
the value type
map:MultiValuedMap<K, V>

the MultiValuedMap to use

key:K

the key to look up

Returns:Bag<V>

the Collection in the MultiValuedMap as Bag, or null if input map is null

getValuesAsListback to summary
public static <K, V> List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key)

Gets a List from MultiValuedMap in a null-safe manner.

Parameters
<K>
the key type
<V>
the value type
map:MultiValuedMap<K, V>

the MultiValuedMap to use

key:K

the key to look up

Returns:List<V>

the Collection in the MultiValuedMap as List, or null if input map is null

getValuesAsSetback to summary
public static <K, V> Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key)

Gets a Set from MultiValuedMap in a null-safe manner.

Parameters
<K>
the key type
<V>
the value type
map:MultiValuedMap<K, V>

the MultiValuedMap to use

key:K

the key to look up

Returns:Set<V>

the Collection in the MultiValuedMap as Set, or null if input map is null

isEmptyback to summary
public static boolean isEmpty(final MultiValuedMap<?, ?> map)

Null-safe check if the specified MultiValuedMap is empty.

If the provided map is null, returns true.

Parameters
map:MultiValuedMap<?, ?>

the map to check, may be null

Returns:boolean

true if the map is empty or null

newListValuedHashMapback to summary
public static <K, V> ListValuedMap<K, V> newListValuedHashMap()

Creates a ListValuedMap with an ArrayList as collection class to store the values mapped to a key.

Parameters
<K>
the key type
<V>
the value type
Returns:ListValuedMap<K, V>

a new ListValuedMap

newSetValuedHashMapback to summary
public static <K, V> SetValuedMap<K, V> newSetValuedHashMap()

Creates a SetValuedMap with an HashSet as collection class to store the values mapped to a key.

Parameters
<K>
the key type
<V>
the value type
Returns:SetValuedMap<K, V>

a new SetValuedMap

transformedMultiValuedMapback to summary
public static <K, V> MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map, final Transformer<? super K, ? extends K> keyTransformer, final Transformer<? super V, ? extends V> valueTransformer)

Returns a TransformedMultiValuedMap backed 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 behaviour, see TransformedMultiValuedMap#transformedMap.

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.

Parameters
<K>
the key type
<V>
the value type
map:MultiValuedMap<K, V>

the MultiValuedMap to transform, must not be null, typically empty

keyTransformer:Transformer<? super K, ? extends K>

the transformer for the map keys, null means no transformation

valueTransformer:Transformer<? super V, ? extends V>

the transformer for the map values, null means no transformation

Returns:MultiValuedMap<K, V>

a transformed MultiValuedMap backed by the given map

Exceptions
NullPointerException:
if map is null
unmodifiableMultiValuedMapback to summary
public static <K, V> MultiValuedMap<K, V> unmodifiableMultiValuedMap(final MultiValuedMap<? extends K, ? extends V> map)

Returns an UnmodifiableMultiValuedMap backed by the given map.

Parameters
<K>
the key type
<V>
the value type
map:MultiValuedMap<? extends K, ? extends V>

the MultiValuedMap to decorate, must not be null

Returns:MultiValuedMap<K, V>

an unmodifiable MultiValuedMap backed by the provided map

Exceptions
NullPointerException:
if map is null