java.lang.Object
ru.progrm_jarvis.javacommons.collection.MapUtil

public final class MapUtil extends Object
Utilities related to Map.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V, M extends Map<K, V>>
    M
    fillMap(M map, @NonNull Iterable<? extends Pair<K,V>> entries)
    Fills the map specified with the values specified.
    static <M extends Map<?, ?>>
    M
    fillMap(M map, @NonNull Object... keyValuePairs)
    Fills the map specified with the values specified.
    static <K, V, M extends Map<K, V>>
    M
    fillMap(M map, @NonNull Iterator<? extends Pair<K,V>> entries)
    Fills the map specified with the values specified.
    static <K, V, M extends Map<K, V>>
    M
    fillMap(M map, @NonNull Stream<? extends Pair<K,V>> entries)
    Fills the map specified with the values specified.
    static <K, V, M extends Map<K, V>>
    M
    fillMap(M map, @NonNull Pair<K,V>... entries)
    Fills the map specified with the values specified.
    static <K, V, M extends Map<K, V>>
    M
    fillMap(M map, K firstValueKey, V firstValue, @NonNull Object... keyValuePairs)
    Fills the map specified with the values specified.
    static <K, V, M extends Map<K, V>>
    M
    fillMapOrdered(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 values
      keyValuePairs - 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 - if keyValuePairs's length is odd
      See Also:
    • fillMap

      public static <K, V, M extends Map<K, V>> M fillMap(@NonNull M map, K firstValueKey, V firstValue, @NonNull @NonNull Object... keyValuePairs)
      Fills the map specified with the values specified.
      Type Parameters:
      K - type of keys
      V - type of values
      M - map type
      Parameters:
      map - map to fill with the values
      firstValueKey - the key of first value
      firstValue - first value to be put to the map
      keyValuePairs - 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 - if keyValuePairs's length is odd
      See Also:
    • fillMap

      @SafeVarargs public static <K, V, M extends Map<K, V>> M fillMap(@NonNull M map, @NonNull @NonNull Pair<K,V>... entries)
      Fills the map specified with the values specified.
      Type Parameters:
      K - type of keys
      V - type of values
      M - map type
      Parameters:
      map - map to fill with the values
      entries - entries to fill the map with
      Returns:
      the map passed filled with key-value pairs specified
    • fillMap

      public static <K, V, M extends Map<K, V>> M fillMap(@NonNull M map, @NonNull @NonNull Iterator<? extends Pair<K,V>> entries)
      Fills the map specified with the values specified.
      Type Parameters:
      K - type of keys
      V - type of values
      M - map type
      Parameters:
      map - map to fill with the values
      entries - entries to fill the map with
      Returns:
      the map passed filled with key-value pairs specified
    • fillMap

      public static <K, V, M extends Map<K, V>> M fillMap(@NonNull M map, @NonNull @NonNull Iterable<? extends Pair<K,V>> entries)
      Fills the map specified with the values specified.
      Type Parameters:
      K - type of keys
      V - type of values
      M - map type
      Parameters:
      map - map to fill with the values
      entries - entries to fill the map with
      Returns:
      the map passed filled with key-value pairs specified
    • fillMap

      public static <K, V, M extends Map<K, V>> M fillMap(@NonNull M map, @NonNull @NonNull Stream<? extends Pair<K,V>> entries)
      Fills the map specified with the values specified.
      Type Parameters:
      K - type of keys
      V - type of values
      M - map type
      Parameters:
      map - map to fill with the values
      entries - entries to fill the map with
      Returns:
      the map passed filled with key-value pairs specified
    • fillMapOrdered

      public static <K, V, M extends Map<K, V>> M fillMapOrdered(@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 keys
      V - type of values
      M - map type
      Parameters:
      map - map to fill with the values
      entries - 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. Unlike Map.computeIfAbsent(Object, Function) this does not store the newly computed value in the map.
      Type Parameters:
      K - the type of map's keys
      V - the type of map's values
      Parameters:
      map - map from which to try to get the value
      key - key of the searched value
      defaultValueSupplier - 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> R getOrDefault(@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. Unlike Map.computeIfAbsent(Object, Function) this does not store the newly computed value in the map.
      Type Parameters:
      K - the type of map's keys
      V - the type of map's values
      R - the type of the resulting value
      Parameters:
      map - map from which to try to get the value
      key - key of the searched value
      valueTransformer - function used to transform map value
      defaultValue - 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> R getOrDefault(@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. Unlike Map.computeIfAbsent(Object, Function) this does not store the newly computed value in the map.
      Type Parameters:
      K - the type of map's keys
      V - the type of map's values
      R - the type of the resulting value
      Parameters:
      map - map from which to try to get the value
      key - key of the searched value
      valueTransformer - function used to transform map value
      defaultValueSupplier - supplier of the default value
      Returns:
      the value associated with the key in the map or the one produced by the supplier otherwise