Class MapUtils
java.lang.Object
no.mnemonic.commons.utilities.collections.MapUtils
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Map<K, V> Adds a key-value pair to a map unless the key or the value are null.static <K,
V> Map<K, V> concatenate
(Map<K, V>... maps) Concatenates multiple maps into one new map.static <K,
V> Map<K, V> filterMap
(Map<K, V> originMap, Collection<K> retainKeys) Creates a subset of the given map based on a collection of keys.static <K,
V> boolean Null-safe operation to test if a map is empty.static <K,
V> Map<K, V> map
(Collection<MapUtils.Pair<K, V>> collection) Creates a map from a collection containing key-value pairs.static <K,
V, T> Map<K, V> map
(Collection<T> collection, Function<T, MapUtils.Pair<K, V>> mapping) Creates a map from a collection using a mapping function to extract key-value pairs.static <K,
V, T> Map<K, V> map
(Function<T, MapUtils.Pair<K, V>> mapping, T... values) Creates a map from its arguments using a mapping function to extract key-value pairs.static <K,
V> Map<K, V> map
(Iterator<MapUtils.Pair<K, V>> iterator) Creates a map from an iterator supplying key-value pairs.static <K,
V, T> Map<K, V> map
(Iterator<T> iterator, Function<T, MapUtils.Pair<K, V>> mapping) Creates a map from an iterator using a mapping function to extract key-value pairs.static <K,
V> Map<K, V> Creates a map from a mapstatic <K,
V> Map<K, V> map
(MapUtils.Pair<K, V>... pairs) Creates a map from key-value pairs.static <K,
V> Map<K, V> modifyMap
(Map<K, V> originMap, Map<K, V> addMap, Collection<K> removeKeys) Applies add/remove modifications to a map.static <K,
V> MapUtils.Pair<K, V> pair
(K key, V val) Convenience method to create a key/valueMapUtils.Pair
.static <K,
V> int Null-safe operation to determine the size of a map.
-
Method Details
-
map
Creates a map from a map- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
map
- Map to create new map from- Returns:
- A new map containing the entries of the provided map, or an empty map if the provided argument was null
-
map
Creates a map from key-value pairs.- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
pairs
- Pairs converted to key-value entries in the map.- Returns:
- A map containing the key-value pairs.
-
map
Creates a map from an iterator supplying key-value pairs.- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
iterator
- An iterator supplying key-value pairs which are added to the map.- Returns:
- A map containing all key-value pairs supplied by the given iterator.
-
map
Creates a map from a collection containing key-value pairs.- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
collection
- A collection containing key-value pairs which are added to the map.- Returns:
- A map containing all key-value pairs contained in the given collection.
-
map
@SafeVarargs public static <K,V, Map<K,T> V> map(Function<T, MapUtils.Pair<K, V>> mapping, T... values) Creates a map from its arguments using a mapping function to extract key-value pairs.- Type Parameters:
K
- Type of keys.V
- Type of values.T
- Type of argument values.- Parameters:
mapping
- A mapping function applied to all values to extract key-value pairs.values
- Values which are added to the map.- Returns:
- A map containing all key-value pairs extracted from the values.
-
map
Creates a map from an iterator using a mapping function to extract key-value pairs.- Type Parameters:
K
- Type of keys.V
- Type of values.T
- Type of elements supplied by 'iterator'.- Parameters:
iterator
- An iterator which values are added to the map.mapping
- A mapping function applied to all values supplied by 'iterator' to extract key-value pairs.- Returns:
- A map containing all key-value pairs extracted from the given iterator.
-
map
public static <K,V, Map<K,T> V> map(Collection<T> collection, Function<T, MapUtils.Pair<K, V>> mapping) Creates a map from a collection using a mapping function to extract key-value pairs.- Type Parameters:
K
- Type of keys.V
- Type of values.T
- Type of elements in 'collection'.- Parameters:
collection
- A collection which values are added to the map.mapping
- A mapping function applied to all values contained in 'collection' to extract key-value pairs.- Returns:
- A map containing all key-value pairs extracted from the given collection.
-
addToMap
Adds a key-value pair to a map unless the key or the value are null.A new map is created if the provided map is null.
- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
map
- Map to which the key-value pair will be added.key
- Key in the map.value
- Value added for the given key.- Returns:
- Map including added key-value pair.
-
concatenate
Concatenates multiple maps into one new map.If the same key exists in multiple maps the value stored in the last map will be used.
- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
maps
- Multiple maps which will be concatenated.- Returns:
- A map containing all key-value pairs from the given maps.
-
modifyMap
public static <K,V> Map<K,V> modifyMap(Map<K, V> originMap, Map<K, V> addMap, Collection<K> removeKeys) Applies add/remove modifications to a map.This operation permits adding to and removing from one map in a single operation.
- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
originMap
- Map to be modified.addMap
- Map containing key-value pairs to be added.removeKeys
- Collection containing keys to be removed.- Returns:
- Same instance of 'originMap' passed in after modification.
-
filterMap
Creates a subset of the given map based on a collection of keys. The original map is not altered.- Type Parameters:
K
- Type of keys.V
- Type of values.- Parameters:
originMap
- Map to be filtered.retainKeys
- Keys to retain in subset map.- Returns:
- A new map containing only the keys listed in 'retainKeys'.
-
isEmpty
Null-safe operation to test if a map is empty.- Type Parameters:
K
- Type of keys in map.V
- Type of values in map.- Parameters:
map
- Map to be tested.- Returns:
- Returns true if the map is null or contains no elements.
-
size
Null-safe operation to determine the size of a map.- Type Parameters:
K
- Type of keys in map.V
- Type of values in map.- Parameters:
map
- Map to compute size for.- Returns:
- Returns 0 if the map is null, otherwise the number of elements in the map.
-
pair
Convenience method to create a key/valueMapUtils.Pair
.- Type Parameters:
K
- Type of keyV
- Type of value- Parameters:
key
- Keyval
- Value- Returns:
- A key/value pair
-