Class Maps
@GwtCompatible(emulated=true) public final class Maps extends java.lang.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.
- Since:
- 2.0
- Author:
- Kevin Bourrillion, Mike Bostock, Isaac Shum, Louis Wasserman
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMaps.EntryTransformer<K,V1,V2>A transformation of the value of a key-value pair, using both key and value as inputs. -
Method Summary
Modifier and Type Method Description static <K, V> java.util.Map.Entry<K,V>immutableEntry(@Nullable K key, @Nullable V value)Returns an immutable map entry with the specified key and value.static <K, V> java.util.concurrent.ConcurrentMap<K,V>newConcurrentMap()Creates a new emptyConcurrentHashMapinstance.static <K extends java.lang.Enum<K>, V>
java.util.EnumMap<K,V>newEnumMap(java.lang.Class<K> type)Creates anEnumMapinstance.static <K extends java.lang.Enum<K>, V>
java.util.EnumMap<K,V>newEnumMap(java.util.Map<K,? extends V> map)Creates anEnumMapwith the same mappings as the specified map.static <K, V> java.util.HashMap<K,V>newHashMap()Creates a mutable, emptyHashMapinstance.static <K, V> java.util.HashMap<K,V>newHashMap(java.util.Map<? extends K,? extends V> map)Creates a mutableHashMapinstance with the same mappings as the specified map.static <K, V> java.util.HashMap<K,V>newHashMapWithExpectedSize(int expectedSize)Creates aHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth.static <K, V> java.util.IdentityHashMap<K,V>newIdentityHashMap()Creates anIdentityHashMapinstance.static <K, V> java.util.LinkedHashMap<K,V>newLinkedHashMap()Creates a mutable, empty, insertion-orderedLinkedHashMapinstance.static <K, V> java.util.LinkedHashMap<K,V>newLinkedHashMap(java.util.Map<? extends K,? extends V> map)Creates a mutable, insertion-orderedLinkedHashMapinstance with the same mappings as the specified map.static <K, V> java.util.LinkedHashMap<K,V>newLinkedHashMapWithExpectedSize(int expectedSize)Creates aLinkedHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth.static <K extends java.lang.Comparable, V>
java.util.TreeMap<K,V>newTreeMap()Creates a mutable, emptyTreeMapinstance using the natural ordering of its elements.static <C, K extends C, V>
java.util.TreeMap<K,V>newTreeMap(@Nullable java.util.Comparator<C> comparator)Creates a mutable, emptyTreeMapinstance using the given comparator.static <K, V> java.util.TreeMap<K,V>newTreeMap(java.util.SortedMap<K,? extends V> map)Creates a mutableTreeMapinstance with the same mappings as the specified map and using the same ordering as the specified map.
-
Method Details
-
newHashMap
public static <K, V> java.util.HashMap<K,V> newHashMap()Creates a mutable, emptyHashMapinstance.Note: if mutability is not required, use
Map.of()instead.Note: if
Kis anenumtype, usenewEnumMap(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
HashMapconstructor directly, taking advantage of the new "diamond" syntax.- Returns:
- a new, empty
HashMap
-
newHashMap
public static <K, V> java.util.HashMap<K,V> newHashMap(java.util.Map<? extends K,? extends V> map)Creates a mutableHashMapinstance with the same mappings as the specified map.Note: if mutability is not required, use
ImmutableMap.copyOf(Map)instead.Note: if
Kis anEnumtype, usenewEnumMap(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
HashMapconstructor directly, taking advantage of the new "diamond" syntax.- Parameters:
map- the mappings to be placed in the new map- Returns:
- a new
HashMapinitialized with the mappings frommap
-
newHashMapWithExpectedSize
public static <K, V> java.util.HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)Creates aHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements 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.- Parameters:
expectedSize- the number of entries you expect to add to the returned map- Returns:
- a new, empty
HashMapwith enough capacity to holdexpectedSizeentries without resizing - Throws:
java.lang.IllegalArgumentException- ifexpectedSizeis negative
-
newLinkedHashMap
public static <K, V> java.util.LinkedHashMap<K,V> newLinkedHashMap()Creates a mutable, empty, insertion-orderedLinkedHashMapinstance.Note: if mutability is not required, use
Map.of()instead.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
LinkedHashMapconstructor directly, taking advantage of the new "diamond" syntax.- Returns:
- a new, empty
LinkedHashMap
-
newLinkedHashMap
public static <K, V> java.util.LinkedHashMap<K,V> newLinkedHashMap(java.util.Map<? extends K,? extends V> map)Creates a mutable, insertion-orderedLinkedHashMapinstance 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
LinkedHashMapconstructor directly, taking advantage of the new "diamond" syntax.- Parameters:
map- the mappings to be placed in the new map- Returns:
- a new,
LinkedHashMapinitialized with the mappings frommap
-
newLinkedHashMapWithExpectedSize
public static <K, V> java.util.LinkedHashMap<K,V> newLinkedHashMapWithExpectedSize(int expectedSize)Creates aLinkedHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements 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.- Parameters:
expectedSize- the number of entries you expect to add to the returned map- Returns:
- a new, empty
LinkedHashMapwith enough capacity to holdexpectedSizeentries without resizing - Throws:
java.lang.IllegalArgumentException- ifexpectedSizeis negative- Since:
- 19.0
-
newConcurrentMap
public static <K, V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentMap()Creates a new emptyConcurrentHashMapinstance.- Since:
- 3.0
-
newTreeMap
public static <K extends java.lang.Comparable, V> java.util.TreeMap<K,V> newTreeMap()Creates a mutable, emptyTreeMapinstance 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
TreeMapconstructor directly, taking advantage of the new "diamond" syntax.- Returns:
- a new, empty
TreeMap
-
newTreeMap
public static <K, V> java.util.TreeMap<K,V> newTreeMap(java.util.SortedMap<K,? extends V> map)Creates a mutableTreeMapinstance 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
TreeMapconstructor directly, taking advantage of the new "diamond" syntax.- Parameters:
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 map- Returns:
- a new
TreeMapinitialized with the mappings frommapand using the comparator ofmap
-
newTreeMap
public static <C, K extends C, V> java.util.TreeMap<K,V> newTreeMap(@Nullable java.util.Comparator<C> comparator)Creates a mutable, emptyTreeMapinstance 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
TreeMapconstructor directly, taking advantage of the new "diamond" syntax.- Parameters:
comparator- the comparator to sort the keys with- Returns:
- a new, empty
TreeMap
-
newEnumMap
public static <K extends java.lang.Enum<K>, V> java.util.EnumMap<K,V> newEnumMap(java.lang.Class<K> type)Creates anEnumMapinstance.- Parameters:
type- the key type for this map- Returns:
- a new, empty
EnumMap
-
newEnumMap
public static <K extends java.lang.Enum<K>, V> java.util.EnumMap<K,V> newEnumMap(java.util.Map<K,? extends V> map)Creates anEnumMapwith 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
EnumMapconstructor directly, taking advantage of the new "diamond" syntax.- Parameters:
map- the map from which to initialize thisEnumMap- Returns:
- a new
EnumMapinitialized with the mappings frommap - Throws:
java.lang.IllegalArgumentException- ifmis not anEnumMapinstance and contains no mappings
-
newIdentityHashMap
public static <K, V> java.util.IdentityHashMap<K,V> newIdentityHashMap()Creates anIdentityHashMapinstance.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
IdentityHashMapconstructor directly, taking advantage of the new "diamond" syntax.- Returns:
- a new, empty
IdentityHashMap
-
immutableEntry
@GwtCompatible(serializable=true) public static <K, V> java.util.Map.Entry<K,V> immutableEntry(@Nullable K key, @Nullable V value)Returns an immutable map entry with the specified key and value. TheMap.Entry.setValue(V)operation throws anUnsupportedOperationException.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.- Parameters:
key- the key to be associated with the returned entryvalue- the value to be associated with the returned entry
-