@GwtCompatible(emulated=true) public final class Maps extends Object
Map instances (including instances of SortedMap, BiMap, etc.). Also see this class's counterparts Lists, Sets
and Queues.
See the Guava User Guide article on Maps.
| Modifier and Type | Class and Description |
|---|---|
static interface |
Maps.EntryTransformer<K,V1,V2>
A transformation of the value of a key-value pair, using both key and value as inputs.
|
| Modifier and Type | Method and Description |
|---|---|
static <K,V> Map.Entry<K,V> |
immutableEntry(K key,
V value)
Returns an immutable map entry with the specified key and value.
|
static <K,V> ConcurrentMap<K,V> |
newConcurrentMap()
Creates a new empty
ConcurrentHashMap instance. |
static <K extends Enum<K>,V> |
newEnumMap(Class<K> type)
Creates an
EnumMap instance. |
static <K extends Enum<K>,V> |
newEnumMap(Map<K,? extends V> map)
Creates an
EnumMap with the same mappings as the specified map. |
static <K,V> HashMap<K,V> |
newHashMap()
Creates a mutable, empty
HashMap instance. |
static <K,V> HashMap<K,V> |
newHashMap(Map<? extends K,? extends V> map)
Creates a mutable
HashMap instance with the same mappings as the specified map. |
static <K,V> HashMap<K,V> |
newHashMapWithExpectedSize(int expectedSize)
Creates a
HashMap instance, with a high enough "initial capacity" that it should
hold expectedSize elements without growth. |
static <K,V> IdentityHashMap<K,V> |
newIdentityHashMap()
Creates an
IdentityHashMap instance. |
static <K,V> LinkedHashMap<K,V> |
newLinkedHashMap()
Creates a mutable, empty, insertion-ordered
LinkedHashMap instance. |
static <K,V> LinkedHashMap<K,V> |
newLinkedHashMap(Map<? extends K,? extends V> map)
Creates a mutable, insertion-ordered
LinkedHashMap instance with the same
mappings as the specified map. |
static <K,V> LinkedHashMap<K,V> |
newLinkedHashMapWithExpectedSize(int expectedSize)
Creates a
LinkedHashMap instance, with a high enough "initial capacity" that it
should hold expectedSize elements without growth. |
static <K extends Comparable,V> |
newTreeMap()
Creates a mutable, empty
TreeMap instance using the natural ordering of its
elements. |
static <C,K extends C,V> |
newTreeMap(@Nullable Comparator<C> comparator)
Creates a mutable, empty
TreeMap instance using the given comparator. |
static <K,V> TreeMap<K,V> |
newTreeMap(SortedMap<K,? extends V> map)
Creates a mutable
TreeMap instance with the same mappings as the specified map
and using the same ordering as the specified map. |
public static <K,V> HashMap<K,V> newHashMap()
HashMap instance.
Note: if mutability is not required, use ImmutableMap#of() instead.
Note: if K is an enum type, use newEnumMap(java.lang.Class<K>) instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the HashMap constructor directly, taking advantage of the new
"diamond" syntax.
HashMappublic static <K,V> HashMap<K,V> newHashMap(Map<? extends K,? extends V> map)
HashMap instance with the same mappings as the specified map.
Note: if mutability is not required, use ImmutableMap.copyOf(Map) instead.
Note: if K is an Enum type, use newEnumMap(java.lang.Class<K>) instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the HashMap constructor directly, taking advantage of the new
"diamond" syntax.
map - the mappings to be placed in the new mapHashMap initialized with the mappings from mappublic static <K,V> HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
HashMap instance, with a high enough "initial capacity" that it should
hold expectedSize elements without growth. This behavior cannot be broadly guaranteed,
but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed that the method
isn't inadvertently oversizing the returned map.expectedSize - the number of entries you expect to add to the returned mapHashMap with enough capacity to hold expectedSize entries
without resizingIllegalArgumentException - if expectedSize is negativepublic static <K,V> LinkedHashMap<K,V> newLinkedHashMap()
LinkedHashMap instance.
Note: if mutability is not required, use ImmutableMap#of() instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the LinkedHashMap constructor directly, taking advantage of
the new "diamond" syntax.
LinkedHashMappublic static <K,V> LinkedHashMap<K,V> newLinkedHashMap(Map<? extends K,? extends V> map)
LinkedHashMap instance with the same
mappings as the specified map.
Note: if mutability is not required, use ImmutableMap.copyOf(Map) instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the LinkedHashMap constructor directly, taking advantage of
the new "diamond" syntax.
map - the mappings to be placed in the new mapLinkedHashMap initialized with the mappings from mappublic static <K,V> LinkedHashMap<K,V> newLinkedHashMapWithExpectedSize(int expectedSize)
LinkedHashMap instance, with a high enough "initial capacity" that it
should hold expectedSize elements without growth. This behavior cannot be
broadly guaranteed, but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed
that the method isn't inadvertently oversizing the returned map.expectedSize - the number of entries you expect to add to the returned mapLinkedHashMap with enough capacity to hold expectedSize
entries without resizingIllegalArgumentException - if expectedSize is negativepublic static <K,V> ConcurrentMap<K,V> newConcurrentMap()
ConcurrentHashMap instance.public static <K extends Comparable,V> TreeMap<K,V> newTreeMap()
TreeMap instance using the natural ordering of its
elements.
Note: if mutability is not required, use ImmutableSortedMap#of() instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the TreeMap constructor directly, taking advantage of the new
"diamond" syntax.
TreeMappublic static <K,V> TreeMap<K,V> newTreeMap(SortedMap<K,? extends V> map)
TreeMap instance with the same mappings as the specified map
and using the same ordering as the specified map.
Note: if mutability is not required, use ImmutableSortedMap#copyOfSorted(SortedMap) instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the TreeMap constructor directly, taking advantage of the new
"diamond" syntax.
map - the sorted map whose mappings are to be placed in the new map and whose comparator
is to be used to sort the new mapTreeMap initialized with the mappings from map and using the
comparator of mappublic static <C,K extends C,V> TreeMap<K,V> newTreeMap(@Nullable Comparator<C> comparator)
TreeMap instance using the given comparator.
Note: if mutability is not required, use ImmutableSortedMap.orderedBy(comparator).build() instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the TreeMap constructor directly, taking advantage of the new
"diamond" syntax.
comparator - the comparator to sort the keys withTreeMappublic static <K extends Enum<K>,V> EnumMap<K,V> newEnumMap(Class<K> type)
EnumMap instance.type - the key type for this mapEnumMappublic static <K extends Enum<K>,V> EnumMap<K,V> newEnumMap(Map<K,? extends V> map)
EnumMap with the same mappings as the specified map.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the EnumMap constructor directly, taking advantage of the new
"diamond" syntax.
map - the map from which to initialize this EnumMapEnumMap initialized with the mappings from mapIllegalArgumentException - if m is not an EnumMap instance and contains
no mappingspublic static <K,V> IdentityHashMap<K,V> newIdentityHashMap()
IdentityHashMap instance.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the IdentityHashMap constructor directly, taking advantage of
the new "diamond" syntax.
IdentityHashMap@GwtCompatible(serializable=true) public static <K,V> Map.Entry<K,V> immutableEntry(K key, V value)
Map.Entry.setValue(V)
operation throws an UnsupportedOperationException.
The returned entry is serializable.
Java 9 users: consider using java.util.Map.entry(key, value) if the key and
value are non-null and the entry does not need to be serializable.
key - the key to be associated with the returned entryvalue - the value to be associated with the returned entryCopyright © 2007-2022. All Rights Reserved.