Class Maps
java.lang.Object
be.imgn.gaia.core.collect.Maps
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
nullkeys and values, and querying them withnull, as inget(null)orcontainsValue(null), throwsNullPointerException. - The
mapmethods andmapOfEntries(Map.Entry...)reject duplicate keys withIllegalArgumentException, and thetoMapcollectors without a merger withIllegalStateException. - They are serializable if all their keys and values are.
- They are value-based: do not rely on their identity.
equalsandhashCodefollow theMapcontract, so a map from this class equals aMap.ofmap with the same entries, whatever their order.
- Since:
- 0.1.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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> Returns an unmodifiable map containing the mappings ofmap, 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.Returns a collector into an unmodifiable map that iterates in encounter order: the ordered counterpart ofCollectors.toUnmodifiableMap(Function, Function).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 ofCollectors.toUnmodifiableMap(Function, Function, BinaryOperator).
-
Method Details
-
map
Returns an unmodifiable map containing zero mappings.- Type Parameters:
K- the key typeV- the value type- Returns:
- an empty map
- See Also:
-
map
Returns an unmodifiable map containing a single mapping.- Type Parameters:
K- the key typeV- the value type- Parameters:
k1- the mapping's keyv1- the mapping's value- Returns:
- a map containing the given mappings
- See Also:
-
map
Returns an unmodifiable map containing two mappings, iterating in the order given.- Type Parameters:
K- the key typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's value- Returns:
- a map containing the given mappings
- Throws:
IllegalArgumentException- if there are any duplicate keys- See Also:
-
map
Returns an unmodifiable map containing three mappings, iterating in the order given.- Type Parameters:
K- the key typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's value- Returns:
- a map containing the given mappings
- Throws:
IllegalArgumentException- if there are any duplicate keys- See Also:
-
map
Returns an unmodifiable map containing four mappings, iterating in the order given.- Type Parameters:
K- the key typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's value- Returns:
- a map containing the given mappings
- Throws:
IllegalArgumentException- if there are any duplicate keys- See Also:
-
map
Returns an unmodifiable map containing five mappings, iterating in the order given.- Type Parameters:
K- the key typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- 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 typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- 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 typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- 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 typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's valuek8- the eighth mapping's keyv8- 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 typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's valuek8- the eighth mapping's keyv8- the eighth mapping's valuek9- the ninth mapping's keyv9- 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 typeV- the value type- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's valuek8- the eighth mapping's keyv8- the eighth mapping's valuek9- the ninth mapping's keyv9- the ninth mapping's valuek10- the tenth mapping's keyv10- 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 typeV- 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
Returns an unmodifiable map containing the mappings ofmap, iterating in the map's iteration order. Ifmapis already a map returned by this class, the copy shares its contents instead of copying them.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map whose mappings to copy- Returns:
- a map containing the mappings of
map - Throws:
IllegalArgumentException- ifmapcontains keys that are duplicates according to theirequalsmethod, as anIdentityHashMapcan- See Also:
-
toMap
public static <T,K, Collector<T,V> ?, toMapMap<K, V>> (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 ofCollectors.toUnmodifiableMap(Function, Function). If two elements map to the same key, collecting throwsIllegalStateException. The resulting map behaves like the other maps of this class.- Type Parameters:
T- the type of the input elementsK- the key typeV- the value type- Parameters:
keyMapper- the function that maps an element to its keyvalueMapper- the function that maps an element to its value- Returns:
- a collector into an ordered, unmodifiable map
-
toMap
public static <T,K, Collector<T,V> ?, toMapMap<K, V>> (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 ofCollectors.toUnmodifiableMap(Function, Function, BinaryOperator). If several elements map to the same key, their values are merged withmerger, asMap.mergedoes, 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 elementsK- the key typeV- the value type- Parameters:
keyMapper- the function that maps an element to its keyvalueMapper- the function that maps an element to its valuemerger- the function that merges the values of the same key- Returns:
- a collector into an ordered, unmodifiable map
-