public final class Maps2 extends Object
"Immutable Map Static Factory Methods" in the Map
interface.
The Maps.of() and Maps.ofEntries() static factory methods provide a convenient way to create
immutable maps. The Map instances created by these methods have the
following characteristics:
UnsupportedOperationException to be thrown.
However, if the contained keys or values are themselves mutable, this may cause
the Map to behave inconsistently or its contents to appear to change.
null keys and values. Attempts to create them with
null keys or values result in NullPointerException.
IllegalArgumentException.
==), identity hash code, and synchronization) are unreliable and
should be avoided.
| Modifier and Type | Method and Description |
|---|---|
static <K,V> Map.Entry<K,V> |
entry(K k,
V v)
Returns an immutable
Map.Entry containing the given key and value. |
static <K,V> Map<K,V> |
of()
Returns an immutable map containing zero mappings.
|
static <K,V> Map<K,V> |
of(K k1,
V v1)
Returns an immutable map containing a single mapping.
|
static <K,V> Map<K,V> |
of(K k1,
V v1,
K k2,
V v2)
Returns an immutable map containing two mappings.
|
static <K,V> Map<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3)
Returns an immutable map containing three mappings.
|
static <K,V> Map<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4)
Returns an immutable map containing four mappings.
|
static <K,V> Map<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5)
Returns an immutable map containing five mappings.
|
static <K,V> Map<K,V> |
of(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 immutable map containing six mappings.
|
static <K,V> Map<K,V> |
of(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 immutable map containing seven mappings.
|
static <K,V> Map<K,V> |
of(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 immutable map containing eight mappings.
|
static <K,V> Map<K,V> |
of(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 immutable map containing nine mappings.
|
static <K,V> Map<K,V> |
of(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 immutable map containing ten mappings.
|
static <K,V> Map<K,V> |
ofEntries(Map.Entry<? extends K,? extends V>... entries)
Returns an immutable map containing keys and values extracted from the given entries.
|
public static <K,V> Map<K,V> of()
K - the Map's key typeV - the Map's value typeMappublic static <K,V> Map<K,V> of(K k1, V v1)
K - the Map's key typeV - the Map's value typek1 - the mapping's keyv1 - the mapping's valueMap containing the specified mappingNullPointerException - if the key or the value is nullpublic static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)
K - the Map's key typeV - the Map's value typek1 - the first mapping's keyv1 - the first mapping's valuek2 - the second mapping's keyv2 - the second mapping's valueMap containing the specified mappingsIllegalArgumentException - if the keys are duplicatesNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
K - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
K - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
K - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(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 - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(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 - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(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 - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(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 - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> of(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)
K - the Map's key typeV - the Map's value typek1 - 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 valueMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any key or value is nullpublic static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
API Note:
It is convenient to create the map entries using the Maps.entry() method.
For example,
import static java.util.Maps.entry;
Map<Integer,String> map = Maps.ofEntries(
entry(1, "a"),
entry(2, "b"),
entry(3, "c"),
...
entry(26, "z"));
K - the Map's key typeV - the Map's value typeentries - Map.Entrys containing the keys and values from which the map is populatedMap containing the specified mappingsIllegalArgumentException - if there are any duplicate keysNullPointerException - if any entry, key, or value is null, or if
the entries array is nullMaps.entry()public static <K,V> Map.Entry<K,V> entry(K k, V v)
Map.Entry containing the given key and value.
These entries are suitable for populating Map instances using the
Maps.ofEntries() method.
The Entry instances created by this method have the following characteristics:
null keys and values. Attempts to create them using a null
key or value result in NullPointerException.
Entry.setValue()
on a returned Entry result in UnsupportedOperationException.
==),
identity hash code, and synchronization) are unreliable and should be avoided.
API Note:
For a serializable Entry, see AbstractMap.SimpleEntry or
AbstractMap.SimpleImmutableEntry.
K - the key's typeV - the value's typek - the keyv - the valueEntry containing the specified key and valueNullPointerException - if the key or value is nullMaps.ofEntries()Copyright © 2016. All rights reserved.