Class Maps
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 ClassesModifier and TypeClassDescriptionstatic interfaceA transformation of the value of a key-value pair, using both key and value as inputs. -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> 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> ConcurrentMap<K, V> Creates a new emptyConcurrentHashMapinstance.newEnumMap(Class<K> type) Creates anEnumMapinstance.newEnumMap(Map<K, ? extends V> map) Creates anEnumMapwith the same mappings as the specified map.static <K,V> HashMap<K, V> Creates a mutable, emptyHashMapinstance.static <K,V> HashMap<K, V> newHashMap(Map<? extends K, ? extends V> map) Creates a mutableHashMapinstance with the same mappings as the specified map.static <K,V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) Creates aHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth.static <K,V> IdentityHashMap<K, V> Creates anIdentityHashMapinstance.static <K,V> LinkedHashMap<K, V> Creates a mutable, empty, insertion-orderedLinkedHashMapinstance.static <K,V> LinkedHashMap<K, V> newLinkedHashMap(Map<? extends K, ? extends V> map) Creates a mutable, insertion-orderedLinkedHashMapinstance with the same mappings as the specified map.static <K,V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) Creates aLinkedHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth.static <K extends Comparable,V>
TreeMap<K,V> Creates a mutable, emptyTreeMapinstance using the natural ordering of its elements.static <C,K extends C, V>
TreeMap<K,V> newTreeMap(@Nullable Comparator<C> comparator) Creates a mutable, emptyTreeMapinstance using the given comparator.static <K,V> TreeMap<K, V> newTreeMap(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
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
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
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:
IllegalArgumentException- ifexpectedSizeis negative
-
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
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
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:
IllegalArgumentException- ifexpectedSizeis negative- Since:
- 19.0
-
newConcurrentMap
Creates a new emptyConcurrentHashMapinstance.- Since:
- 3.0
-
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
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
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
Creates anEnumMapinstance.- Parameters:
type- the key type for this map- Returns:
- a new, empty
EnumMap
-
newEnumMap
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:
IllegalArgumentException- ifmis not anEnumMapinstance and contains no mappings
-
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> 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
-