java.lang.Object
org.docx4j.com.google.common.collect.Maps

@GwtCompatible(emulated=true)
public final class Maps
extends java.lang.Object
Static utility methods pertaining to 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 interface  Maps.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 empty ConcurrentHashMap instance.
    static <K extends java.lang.Enum<K>,​ V>
    java.util.EnumMap<K,​V>
    newEnumMap​(java.lang.Class<K> type)
    Creates an EnumMap instance.
    static <K extends java.lang.Enum<K>,​ V>
    java.util.EnumMap<K,​V>
    newEnumMap​(java.util.Map<K,​? extends V> map)
    Creates an EnumMap with the same mappings as the specified map.
    static <K,​ V> java.util.HashMap<K,​V> newHashMap()
    Creates a mutable, empty HashMap instance.
    static <K,​ V> java.util.HashMap<K,​V> newHashMap​(java.util.Map<? extends K,​? extends V> map)
    Creates a mutable HashMap instance with the same mappings as the specified map.
    static <K,​ V> java.util.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> java.util.IdentityHashMap<K,​V> newIdentityHashMap()
    Creates an IdentityHashMap instance.
    static <K,​ V> java.util.LinkedHashMap<K,​V> newLinkedHashMap()
    Creates a mutable, empty, insertion-ordered LinkedHashMap instance.
    static <K,​ V> java.util.LinkedHashMap<K,​V> newLinkedHashMap​(java.util.Map<? extends K,​? extends V> map)
    Creates a mutable, insertion-ordered LinkedHashMap instance with the same mappings as the specified map.
    static <K,​ V> java.util.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 java.lang.Comparable,​ V>
    java.util.TreeMap<K,​V>
    newTreeMap()
    Creates a mutable, empty TreeMap instance 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, empty TreeMap instance using the given comparator.
    static <K,​ V> java.util.TreeMap<K,​V> newTreeMap​(java.util.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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • newHashMap

      public static <K,​ V> java.util.HashMap<K,​V> newHashMap()
      Creates a mutable, empty HashMap instance.

      Note: if mutability is not required, use Map.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.

      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 mutable 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.

      Parameters:
      map - the mappings to be placed in the new map
      Returns:
      a new HashMap initialized with the mappings from map
    • newHashMapWithExpectedSize

      public static <K,​ V> java.util.HashMap<K,​V> newHashMapWithExpectedSize​(int expectedSize)
      Creates a 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.
      Parameters:
      expectedSize - the number of entries you expect to add to the returned map
      Returns:
      a new, empty HashMap with enough capacity to hold expectedSize entries without resizing
      Throws:
      java.lang.IllegalArgumentException - if expectedSize is negative
    • newLinkedHashMap

      public static <K,​ V> java.util.LinkedHashMap<K,​V> newLinkedHashMap()
      Creates a mutable, empty, insertion-ordered LinkedHashMap instance.

      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 LinkedHashMap constructor 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-ordered 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.

      Parameters:
      map - the mappings to be placed in the new map
      Returns:
      a new, LinkedHashMap initialized with the mappings from map
    • newLinkedHashMapWithExpectedSize

      public static <K,​ V> java.util.LinkedHashMap<K,​V> newLinkedHashMapWithExpectedSize​(int expectedSize)
      Creates a 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.
      Parameters:
      expectedSize - the number of entries you expect to add to the returned map
      Returns:
      a new, empty LinkedHashMap with enough capacity to hold expectedSize entries without resizing
      Throws:
      java.lang.IllegalArgumentException - if expectedSize is negative
      Since:
      19.0
    • newConcurrentMap

      public static <K,​ V> java.util.concurrent.ConcurrentMap<K,​V> newConcurrentMap()
      Creates a new empty ConcurrentHashMap instance.
      Since:
      3.0
    • newTreeMap

      public static <K extends java.lang.Comparable,​ V> java.util.TreeMap<K,​V> newTreeMap()
      Creates a mutable, empty 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.

      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 mutable 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.

      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 TreeMap initialized with the mappings from map and using the comparator of map
    • newTreeMap

      public static <C,​ K extends C,​ V> java.util.TreeMap<K,​V> newTreeMap​(@Nullable java.util.Comparator<C> comparator)
      Creates a mutable, empty 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.

      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 an EnumMap instance.
      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 an 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.

      Parameters:
      map - the map from which to initialize this EnumMap
      Returns:
      a new EnumMap initialized with the mappings from map
      Throws:
      java.lang.IllegalArgumentException - if m is not an EnumMap instance and contains no mappings
    • newIdentityHashMap

      public static <K,​ V> java.util.IdentityHashMap<K,​V> newIdentityHashMap()
      Creates an 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.

      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. The 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.

      Parameters:
      key - the key to be associated with the returned entry
      value - the value to be associated with the returned entry