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

@GwtCompatible(emulated=true) public final class Maps extends 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
  • Constructor Details

    • Maps

      private Maps()
  • Method Details

    • keyFunction

      static <K> Function<Map.Entry<K,?>,K> keyFunction()
    • valueFunction

      static <V> Function<Map.Entry<?,V>,V> valueFunction()
    • keyIterator

      static <K, V> Iterator<K> keyIterator(Iterator<Map.Entry<K,V>> entryIterator)
    • newHashMap

      public static <K, V> 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> HashMap<K,V> newHashMap(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> 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:
      IllegalArgumentException - if expectedSize is negative
    • capacity

      static int capacity(int expectedSize)
      Returns a capacity that is sufficient to keep the map from being resized as long as it grows no larger than expectedSize and the load factor is ≥ its default (0.75).
    • newLinkedHashMap

      public static <K, V> 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> 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.

      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> 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:
      IllegalArgumentException - if expectedSize is negative
      Since:
      19.0
    • newConcurrentMap

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

      public static <K extends Comparable, V> 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> 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.

      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> TreeMap<K,V> newTreeMap(@Nullable 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 Enum<K>, V> EnumMap<K,V> newEnumMap(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 Enum<K>, V> EnumMap<K,V> newEnumMap(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:
      IllegalArgumentException - if m is not an EnumMap instance and contains no mappings
    • newIdentityHashMap

      public static <K, V> 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> 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
    • asEntryTransformer

      static <K, V1, V2> Maps.EntryTransformer<K,V1,V2> asEntryTransformer(Function<? super V1,V2> function)
      Views a function as an entry transformer that ignores the entry key.
    • asValueToValueFunction

      static <K, V1, V2> Function<V1,V2> asValueToValueFunction(Maps.EntryTransformer<? super K,V1,V2> transformer, K key)
    • asEntryToValueFunction

      static <K, V1, V2> Function<Map.Entry<K,V1>,V2> asEntryToValueFunction(Maps.EntryTransformer<? super K,? super V1,V2> transformer)
      Views an entry transformer as a function from Entry to values.
    • safeGet

      static <V> V safeGet(Map<?,V> map, @Nullable Object key)
      Delegates to Map.get(java.lang.Object). Returns null on ClassCastException and NullPointerException.
    • safeContainsKey

      static boolean safeContainsKey(Map<?,?> map, Object key)
      Delegates to Map.containsKey(java.lang.Object). Returns false on ClassCastException and NullPointerException.
    • safeRemove

      static <V> V safeRemove(Map<?,V> map, Object key)
      Delegates to Map.remove(java.lang.Object). Returns null on ClassCastException and NullPointerException.
    • equalsImpl

      static boolean equalsImpl(Map<?,?> map, Object object)
      An implementation of Map.equals(java.lang.Object).
    • putAllImpl

      static <K, V> void putAllImpl(Map<K,V> self, Map<? extends K,? extends V> map)
    • keyOrNull

      static <K> @Nullable K keyOrNull(@Nullable Map.Entry<K,?> entry)
    • valueOrNull

      static <V> @Nullable V valueOrNull(@Nullable Map.Entry<?,V> entry)