Class Maps


  • 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 (imported from Google Collections Library)
    Author:
    Kevin Bourrillion, Mike Bostock, Isaac Shum, Louis Wasserman
    • Method Detail

      • 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.6. It also can't be guaranteed that the method isn't inadvertently oversizing the returned map.
        Parameters:
        expectedSize - the number of elements you expect to add to the returned map
        Returns:
        a new, empty HashMap with enough capacity to hold expectedSize elements without resizing
        Throws:
        IllegalArgumentException - if expectedSize is negative
      • immutableEntry

        public static <K,​V> Map.Entry<K,​V> immutableEntry​(K key,
                                                                      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.

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