Class MapExtensions
maps
.- Author:
- Sebastian Zarnekow - Initial contribution and API, Stephane Galland - Add operators on map structures.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Map<K, V> filter
(Map<K, V> original, Functions.Function2<? super K, ? super V, Boolean> predicate) Returns a filtered live view on top of the original map.static <K,
V> void forEach
(Map<K, V> map, Procedures.Procedure2<? super K, ? super V> procedure) static <K,
V> void forEach
(Map<K, V> map, Procedures.Procedure3<? super K, ? super V, ? super Integer> procedure) static <K,
V1, V2> Map<K, V2> mapValues
(Map<K, V1> original, Functions.Function1<? super V1, ? extends V2> transformation) Returns a map that performs the giventransformation
for each value oforiginal
when requested.static <K,
V> void operator_add
(Map<K, V> outputMap, Map<? extends K, ? extends V> inputMap) Add the given entries of the input map into the output map.static <K,
V> V operator_add
(Map<K, V> map, Pair<? extends K, ? extends V> entry) Add the given pair into the map.static <K,
V> Map<K, V> operator_minus
(Map<K, V> map, Iterable<?> keys) Replies the elements of the given map except the pairs with the given keys.static <K,
V> Map<K, V> operator_minus
(Map<K, V> left, Map<? extends K, ? extends V> right) Replies the elements of the left map without the pairs in the right map.static <K,
V> Map<K, V> operator_minus
(Map<K, V> map, K key) Replies the elements of the given map except the pair with the given key.static <K,
V> Map<K, V> operator_minus
(Map<K, V> left, Pair<? extends K, ? extends V> right) Remove the given pair from a given map for obtaining a new map.static <K,
V> Map<K, V> operator_plus
(Map<K, V> left, Map<? extends K, ? extends V> right) Merge the two maps.static <K,
V> Map<K, V> operator_plus
(Map<K, V> left, Pair<? extends K, ? extends V> right) Add the given pair to a given map for obtaining a new map.static <K,
V> void operator_remove
(Map<K, V> map, Iterable<? super K> keysToRemove) Remove pairs with the given keys from the map.static <K,
V> V operator_remove
(Map<K, V> map, K key) Remove a key from the given map.static <K,
V> boolean operator_remove
(Map<K, V> map, Pair<? extends K, ? extends V> entry) Remove the given pair into the map.static <K,
V> Map<K, V> Merge the given maps.
-
Constructor Details
-
MapExtensions
public MapExtensions()
-
-
Method Details
-
forEach
public static <K,V> void forEach(Map<K, V> map, Procedures.Procedure2<? super K, ? super V> procedure) - Parameters:
map
- the map. May not benull
.procedure
- the procedure. May not benull
.
-
forEach
public static <K,V> void forEach(Map<K, V> map, Procedures.Procedure3<? super K, ? super V, ? super Integer> procedure) Applies the givenprocedure
for eachkey value pair
of the givenmap
. The procedure takes the key, the value and a loop counter. If the counter would overflow,Integer.MAX_VALUE
is returned for all subsequent pairs. The first pair is at index zero.- Parameters:
map
- the map. May not benull
.procedure
- the procedure. May not benull
.
-
filter
public static <K,V> Map<K,V> filter(Map<K, V> original, Functions.Function2<? super K, ? super V, Boolean> predicate) Returns a filtered live view on top of the original map. Changes to one affect the other.
The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the original map are reflected in the result map. The results supports removal if the original map supports removal.
- Parameters:
original
- the original map. May not benull
.predicate
- the predicate. May not benull
.- Returns:
- a filtered view on top of the original map. Never
null
.
-
operator_add
Add the given pair into the map.If the pair key already exists in the map, its value is replaced by the value in the pair, and the old value in the map is returned.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
map
- the map to update.entry
- the entry (key, value) to add into the map.- Returns:
- the value previously associated to the key, or
null
if the key was not present in the map before the addition. - Since:
- 2.15
-
operator_add
Add the given entries of the input map into the output map.If a key in the inputMap already exists in the outputMap, its value is replaced in the outputMap by the value from the inputMap.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
outputMap
- the map to update.inputMap
- the entries to add.- Since:
- 2.15
-
operator_plus
@Pure public static <K,V> Map<K,V> operator_plus(Map<K, V> left, Pair<? extends K, ? extends V> right) Add the given pair to a given map for obtaining a new map.The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.
Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
left
- the map to consider.right
- the entry (key, value) to add into the map.- Returns:
- an immutable map with the content of the map and with the given entry.
- Throws:
IllegalArgumentException
- - when the right operand key exists in the left operand.- Since:
- 2.15
-
operator_plus
@Pure public static <K,V> Map<K,V> operator_plus(Map<K, V> left, Map<? extends K, ? extends V> right) Merge the two maps.The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.
If a key exists in the left and right operands, the value in the right operand is preferred.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
left
- the left map.right
- the right map.- Returns:
- a map with the merged contents from the two maps.
- Throws:
IllegalArgumentException
- - when a right operand key exists in the left operand.- Since:
- 2.15
-
operator_remove
Remove a key from the given map.- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
map
- the map to update.key
- the key to remove.- Returns:
- the removed value, or
null
if the key was not present in the map. - Since:
- 2.15
-
operator_remove
Remove the given pair into the map.If the given key is inside the map, but is not mapped to the given value, the map will not be changed.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
map
- the map to update.entry
- the entry (key, value) to remove from the map.- Returns:
true
if the pair was removed.- Since:
- 2.15
-
operator_remove
Remove pairs with the given keys from the map.- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
map
- the map to update.keysToRemove
- the keys of the pairs to remove.- Since:
- 2.15
-
operator_minus
@Pure public static <K,V> Map<K,V> operator_minus(Map<K, V> left, Pair<? extends K, ? extends V> right) Remove the given pair from a given map for obtaining a new map.If the given key is inside the map, but is not mapped to the given value, the map will not be changed.
The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
left
- the map to consider.right
- the entry (key, value) to remove from the map.- Returns:
- an immutable map with the content of the map and with the given entry.
- Throws:
IllegalArgumentException
- - when the right operand key exists in the left operand.- Since:
- 2.15
-
operator_minus
Replies the elements of the given map except the pair with the given key.The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
map
- the map to update.key
- the key to remove.- Returns:
- the map with the content of the map except the key.
- Since:
- 2.15
-
operator_minus
@Pure public static <K,V> Map<K,V> operator_minus(Map<K, V> left, Map<? extends K, ? extends V> right) Replies the elements of the left map without the pairs in the right map. If the pair's values differ from the value within the map, the map entry is not removed.The difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.
Since this method uses
HashMap
instances internally, the keys of the supplied maps must be well-behaved with respect toObject.equals(java.lang.Object)
andObject.hashCode()
.- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
left
- the map to update.right
- the pairs to remove.- Returns:
- the map with the content of the left map except the pairs of the right map.
- Since:
- 2.15
-
operator_minus
Replies the elements of the given map except the pairs with the given keys.The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
map
- the map to update.keys
- the keys of the pairs to remove.- Returns:
- the map with the content of the map except the pairs.
- Since:
- 2.15
-
union
@Pure public static <K,V> Map<K,V> union(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) Merge the given maps.The replied map is a view on the given two maps. If a key exists in the two maps, the replied value is the value of the right operand.
Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.
The replied map is unmodifiable.
- Type Parameters:
K
- type of the map keys.V
- type of the map values.- Parameters:
left
- the left map.right
- the right map.- Returns:
- a map with the merged contents from the two maps.
- Since:
- 2.15
-
mapValues
@Pure public static <K,V1, Map<K,V2> V2> mapValues(Map<K, V1> original, Functions.Function1<? super V1, ? extends V2> transformation) Returns a map that performs the given
transformation
for each value oforiginal
when requested.The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the original map are reflected in the result map. The results supports removal if the original map supports removal.
- Parameters:
original
- the original map. May not benull
.transformation
- the transformation. May not benull
.- Returns:
- a map with equal keys but transformed values. Never
null
. - Since:
- 2.4
-