Class Maps

java.lang.Object
be.imgn.gaia.core.collect.Maps

public final class Maps extends Object
Static factories and collectors for unmodifiable maps that iterate in the order their keys are given.

The map methods behave exactly like their Map.of counterparts, mapOfEntries(Map.Entry...) like Map.ofEntries, mapCopy(Map) like Map.copyOf, and the toMap collectors like Collectors.toUnmodifiableMap, except for iteration order and one query. Map.of and Map.copyOf leave the order unspecified, and it changes from one run to the next. These maps iterate over their keys, values and entries in the order the keys were given. And entrySet().contains(null) throws NullPointerException, like every other query with null, where a Map.of map returns false. Otherwise they are the same:

  • They are unmodifiable: every mutator method throws UnsupportedOperationException, even when the call would not change the map. That includes the methods of their views and their iterators.
  • They reject null keys and values, and querying them with null, as in get(null) or containsValue(null), throws NullPointerException.
  • The map methods and mapOfEntries(Map.Entry...) reject duplicate keys with IllegalArgumentException, and the toMap collectors without a merger with IllegalStateException.
  • They are serializable if all their keys and values are.
  • They are value-based: do not rely on their identity.
  • equals and hashCode follow the Map contract, so a map from this class equals a Map.of map with the same entries, whatever their order.
Since:
0.1.2
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K,V> Map<K,V>
    map()
    Returns an unmodifiable map containing zero mappings.
    static <K,V> Map<K,V>
    map(K k1, V v1)
    Returns an unmodifiable map containing a single mapping.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2)
    Returns an unmodifiable map containing two mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3)
    Returns an unmodifiable map containing three mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
    Returns an unmodifiable map containing four mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
    Returns an unmodifiable map containing five mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
    Returns an unmodifiable map containing six mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
    Returns an unmodifiable map containing seven mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
    Returns an unmodifiable map containing eight mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
    Returns an unmodifiable map containing nine mappings, iterating in the order given.
    static <K,V> Map<K,V>
    map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
    Returns an unmodifiable map containing ten mappings, iterating in the order given.
    static <K,V> Map<K,V>
    mapCopy(Map<? extends K, ? extends V> map)
    Returns an unmodifiable map containing the mappings of map, iterating in the map's iteration order.
    static <K,V> Map<K,V>
    mapOfEntries(Map.Entry<? extends K, ? extends V>... entries)
    Returns an unmodifiable map containing the keys and values of the given entries, iterating in the order given.
    static <T,K,V> Collector<T,?,Map<K,V>>
    toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper)
    Returns a collector into an unmodifiable map that iterates in encounter order: the ordered counterpart of Collectors.toUnmodifiableMap(Function, Function).
    static <T,K,V> Collector<T,?,Map<K,V>>
    toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> merger)
    Returns a collector into an unmodifiable map that iterates in encounter order: the ordered counterpart of Collectors.toUnmodifiableMap(Function, Function, BinaryOperator).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • map

      public static <K,V> Map<K,V> map()
      Returns an unmodifiable map containing zero mappings.
      Type Parameters:
      K - the key type
      V - the value type
      Returns:
      an empty map
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1)
      Returns an unmodifiable map containing a single mapping.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the mapping's key
      v1 - the mapping's value
      Returns:
      a map containing the given mappings
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2)
      Returns an unmodifiable map containing two mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3)
      Returns an unmodifiable map containing three mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Returns an unmodifiable map containing four mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
      Returns an unmodifiable map containing five mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
      Returns an unmodifiable map containing six mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
      Returns an unmodifiable map containing seven mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
      Returns an unmodifiable map containing eight mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      k8 - the eighth mapping's key
      v8 - the eighth mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
      Returns an unmodifiable map containing nine mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      k8 - the eighth mapping's key
      v8 - the eighth mapping's value
      k9 - the ninth mapping's key
      v9 - the ninth mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • map

      public static <K,V> Map<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
      Returns an unmodifiable map containing ten mappings, iterating in the order given.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      k8 - the eighth mapping's key
      v8 - the eighth mapping's value
      k9 - the ninth mapping's key
      v9 - the ninth mapping's value
      k10 - the tenth mapping's key
      v10 - the tenth mapping's value
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • mapOfEntries

      @SafeVarargs public static <K,V> Map<K,V> mapOfEntries(Map.Entry<? extends K, ? extends V>... entries)
      Returns an unmodifiable map containing the keys and values of the given entries, iterating in the order given. The entries themselves are not stored.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      entries - the entries containing the keys and values
      Returns:
      a map containing the given mappings
      Throws:
      IllegalArgumentException - if there are any duplicate keys
      See Also:
    • mapCopy

      public static <K,V> Map<K,V> mapCopy(Map<? extends K, ? extends V> map)
      Returns an unmodifiable map containing the mappings of map, iterating in the map's iteration order. If map is already a map returned by this class, the copy shares its contents instead of copying them.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      map - the map whose mappings to copy
      Returns:
      a map containing the mappings of map
      Throws:
      IllegalArgumentException - if map contains keys that are duplicates according to their equals method, as an IdentityHashMap can
      See Also:
    • toMap

      public static <T,K,V> Collector<T,?,Map<K,V>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper)
      Returns a collector into an unmodifiable map that iterates in encounter order: the ordered counterpart of Collectors.toUnmodifiableMap(Function, Function). If two elements map to the same key, collecting throws IllegalStateException. The resulting map behaves like the other maps of this class.
      Type Parameters:
      T - the type of the input elements
      K - the key type
      V - the value type
      Parameters:
      keyMapper - the function that maps an element to its key
      valueMapper - the function that maps an element to its value
      Returns:
      a collector into an ordered, unmodifiable map
    • toMap

      public static <T,K,V> Collector<T,?,Map<K,V>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> merger)
      Returns a collector into an unmodifiable map that iterates in encounter order: the ordered counterpart of Collectors.toUnmodifiableMap(Function, Function, BinaryOperator). If several elements map to the same key, their values are merged with merger, as Map.merge does, and the key keeps the position of its first element. The resulting map behaves like the other maps of this class.
      Type Parameters:
      T - the type of the input elements
      K - the key type
      V - the value type
      Parameters:
      keyMapper - the function that maps an element to its key
      valueMapper - the function that maps an element to its value
      merger - the function that merges the values of the same key
      Returns:
      a collector into an ordered, unmodifiable map