public final class Maps extends Object
Map interface specified in
JEP 269
"Unmodifiable Map Static Factory Methods".
The Maps.of,
Maps.ofEntries, and
Maps.copyOf
static factory methods provide a convenient way to create unmodifiable 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<K,V> |
copyOf(Map<? extends K,? extends V> map)
Returns an unmodifiable Map containing the entries
of the given Map.
|
static <K,V> Map.Entry<K,V> |
entry(K k,
V v)
Returns an unmodifiable
Map.Entry containing the given key and value. |
static <K,V> Map<K,V> |
of()
Returns an unmodifiable map containing zero mappings.
|
static <K,V> Map<K,V> |
of(K k1,
V v1)
Returns an unmodifiable map containing a single mapping.
|
static <K,V> Map<K,V> |
of(K k1,
V v1,
K k2,
V v2)
Returns an unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable map containing ten mappings.
|
static <K,V> Map<K,V> |
ofEntries(Map.Entry<? extends K,? extends V>... entries)
Returns an unmodifiable 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 null@SafeVarargs public 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()public static <K,V> Map<K,V> copyOf(Map<? extends K,? extends V> map)
Implementation Note: If the given Map is an unmodifiable Map, calling copyOf will generally not create a copy.
K - the Map's key typeV - the Map's value typemap - the map from which entries are drawn, must be non-nullMap containing the entries of the given MapNullPointerException - if map is null, or if it contains any null keys or valuesCopyright © 2020. All rights reserved.