Class MapUtil
java.lang.Object
ru.progrm_jarvis.javacommons.collection.MapUtil
Utilities related to
Map.-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V, M extends Map<K, V>>
MFills the map specified with the values specified.static <M extends Map<?,?>>
MFills the map specified with the values specified.static <K,V, M extends Map<K, V>>
MFills the map specified with the values specified.static <K,V, M extends Map<K, V>>
MFills the map specified with the values specified.static <K,V, M extends Map<K, V>>
MFills the map specified with the values specified.static <K,V, M extends Map<K, V>>
MFills the map specified with the values specified.static <K,V, M extends Map<K, V>>
MfillMapOrdered(M map, @NonNull Stream<? extends Pair<K, V>> entries) Fills the map specified with the values specified keeping order.static <K,V> V getOrDefault(@NonNull Map<K, V> map, K key, @NonNull Supplier<V> defaultValueSupplier) Gets the value from the map otherwise returning the value created by using the provided supplier.static <K,V, R> R getOrDefault(@NonNull Map<K, V> map, K key, Function<V, R> valueTransformer, Supplier<R> defaultValueSupplier) Gets the value from the map applying the provided function to it otherwise returning the value created by using the provided supplier.static <K,V, R> R getOrDefault(@NonNull Map<K, V> map, K key, Function<V, R> valueTransformer, R defaultValue) Gets the value from the map applying the provided function to it otherwise returning the value created by using the provided supplier.
-
Method Details
-
fillMap
public static <M extends Map<?,?>> M fillMap(@NonNull M map, @NonNull @NonNull Object... keyValuePairs) Fills the map specified with the values specified.- Type Parameters:
M- map type- Parameters:
map- map to fill with the valueskeyValuePairs- pairs of keys and values in order key1, value1, key2, value2, key3, value3...- Returns:
- the map passed filled with key-value pairs specified
- Throws:
IllegalArgumentException- ifkeyValuePairs's length is odd- See Also:
-
fillMap
public static <K,V, M fillMapM extends Map<K, V>> (@NonNull M map, K firstValueKey, V firstValue, @NonNull @NonNull Object... keyValuePairs) Fills the map specified with the values specified.- Type Parameters:
K- type of keysV- type of valuesM- map type- Parameters:
map- map to fill with the valuesfirstValueKey- the key of first valuefirstValue- first value to be put to the mapkeyValuePairs- pairs of keys and values in order key1, value1, key2, value2, key3, value3...- Returns:
- the map passed filled with key-value pairs specified
- Throws:
IllegalArgumentException- ifkeyValuePairs's length is odd- See Also:
-
fillMap
@SafeVarargs public static <K,V, M fillMapM extends Map<K, V>> (@NonNull M map, @NonNull @NonNull Pair<K, V>... entries) Fills the map specified with the values specified.- Type Parameters:
K- type of keysV- type of valuesM- map type- Parameters:
map- map to fill with the valuesentries- entries to fill the map with- Returns:
- the map passed filled with key-value pairs specified
-
fillMap
public static <K,V, M fillMapM extends Map<K, V>> (@NonNull M map, @NonNull @NonNull Iterator<? extends Pair<K, V>> entries) Fills the map specified with the values specified.- Type Parameters:
K- type of keysV- type of valuesM- map type- Parameters:
map- map to fill with the valuesentries- entries to fill the map with- Returns:
- the map passed filled with key-value pairs specified
-
fillMap
public static <K,V, M fillMapM extends Map<K, V>> (@NonNull M map, @NonNull @NonNull Iterable<? extends Pair<K, V>> entries) Fills the map specified with the values specified.- Type Parameters:
K- type of keysV- type of valuesM- map type- Parameters:
map- map to fill with the valuesentries- entries to fill the map with- Returns:
- the map passed filled with key-value pairs specified
-
fillMap
public static <K,V, M fillMapM extends Map<K, V>> (@NonNull M map, @NonNull @NonNull Stream<? extends Pair<K, V>> entries) Fills the map specified with the values specified.- Type Parameters:
K- type of keysV- type of valuesM- map type- Parameters:
map- map to fill with the valuesentries- entries to fill the map with- Returns:
- the map passed filled with key-value pairs specified
-
fillMapOrdered
public static <K,V, M fillMapOrderedM extends Map<K, V>> (@NonNull M map, @NonNull @NonNull Stream<? extends Pair<K, V>> entries) Fills the map specified with the values specified keeping order.- Type Parameters:
K- type of keysV- type of valuesM- map type- Parameters:
map- map to fill with the valuesentries- entries to fill the map with- Returns:
- the map passed filled with key-value pairs specified
-
getOrDefault
public static <K,V> V getOrDefault(@NonNull @NonNull Map<K, V> map, K key, @NonNull @NonNull Supplier<V> defaultValueSupplier) Gets the value from the map otherwise returning the value created by using the provided supplier. UnlikeMap.computeIfAbsent(Object, Function)this does not store the newly computed value in the map.- Type Parameters:
K- the type of map's keysV- the type of map's values- Parameters:
map- map from which to try to get the valuekey- key of the searched valuedefaultValueSupplier- supplier of the default value- Returns:
- the value associated with the key in the map or the one produced by the supplier otherwise
-
getOrDefault
public static <K,V, R getOrDefaultR> (@NonNull @NonNull Map<K, V> map, K key, Function<V, R> valueTransformer, R defaultValue) Gets the value from the map applying the provided function to it otherwise returning the value created by using the provided supplier. UnlikeMap.computeIfAbsent(Object, Function)this does not store the newly computed value in the map.- Type Parameters:
K- the type of map's keysV- the type of map's valuesR- the type of the resulting value- Parameters:
map- map from which to try to get the valuekey- key of the searched valuevalueTransformer- function used to transform map valuedefaultValue- default value- Returns:
- the value associated with the key in the map or the one produced by the supplier otherwise
-
getOrDefault
public static <K,V, R getOrDefaultR> (@NonNull @NonNull Map<K, V> map, K key, Function<V, R> valueTransformer, Supplier<R> defaultValueSupplier) Gets the value from the map applying the provided function to it otherwise returning the value created by using the provided supplier. UnlikeMap.computeIfAbsent(Object, Function)this does not store the newly computed value in the map.- Type Parameters:
K- the type of map's keysV- the type of map's valuesR- the type of the resulting value- Parameters:
map- map from which to try to get the valuekey- key of the searched valuevalueTransformer- function used to transform map valuedefaultValueSupplier- supplier of the default value- Returns:
- the value associated with the key in the map or the one produced by the supplier otherwise
-