Class CollectionUtil


  • public class CollectionUtil
    extends Object
    Utility class to create Collection instances.
    Author:
    Hannes Wellmann
    • Method Detail

      • newHashMapWithExpectedSize

        public static <K,​V> HashMap<K,​V> newHashMapWithExpectedSize​(int expectedSize)
        Returns a HashMap with an initial capacity that is sufficient to hold expectedSize mappings without rehashing its internal backing storage.

        The returned HashMap has a capacity that is the specified expected size divided by the load factor of the Map, which is sufficient to hold expectedSize mappings without rehashing. As the Javadoc of HashMap states: "If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur".

        Type Parameters:
        K - the type of keys in the returned HashMap
        V - the type of values in the returned HashMap
        Parameters:
        expectedSize - of mappings that will be put into the returned HashMap
        Returns:
        an empty HashMap with sufficient capacity to hold expectedSize mappings
        See Also:
        HashMap
      • newLinkedHashMapWithExpectedSize

        public static <K,​V> LinkedHashMap<K,​V> newLinkedHashMapWithExpectedSize​(int expectedSize)
        Returns a LinkedHashMap with an initial capacity that is sufficient to hold expectedSize mappings without rehashing its internal backing storage.

        Because LinkedHashMap extends HashMap it inherits the issue that the capacity is not equivalent to the number of mappings it can hold without rehashing. See newHashMapWithExpectedSize(int) for details.

        Type Parameters:
        K - the type of keys in the returned LinkedHashMap
        V - the type of values in the returned LinkedHashMap
        Parameters:
        expectedSize - of mappings that will be put into the returned LinkedHashMap
        Returns:
        an empty LinkedHashMap with sufficient capacity to hold expectedSize mappings
        See Also:
        HashMap
      • newHashSetWithExpectedSize

        public static <E> HashSet<E> newHashSetWithExpectedSize​(int expectedSize)
        Returns a HashSet with an initial capacity that is sufficient to hold expectedSize elements without rehashing its internal backing storage.

        Because a HashSet is backed by a HashMap it inherits the issue that the capacity is not equivalent to the number of elements it can hold without rehashing. See newHashMapWithExpectedSize(int) for details.

        Type Parameters:
        E - the type of elements in the returned HashSet
        Parameters:
        expectedSize - of elements that will be add to the returned HashSet
        Returns:
        an empty HashSet with sufficient capacity to hold expectedSize elements
        See Also:
        HashMap
      • newLinkedHashSetWithExpectedSize

        public static <E> LinkedHashSet<E> newLinkedHashSetWithExpectedSize​(int expectedSize)
        Returns a LinkedHashSet with an initial capacity that is sufficient to hold expectedSize elements without rehashing its internal backing storage.

        Because a LinkedHashSet is backed by a HashMap it inherits the issue that the capacity is not equivalent to the number of elements it can hold without rehashing. See newHashMapWithExpectedSize(int) for details.

        Type Parameters:
        E - the type of elements in the returned LinkedHashSet
        Parameters:
        expectedSize - of elements that will be add to the returned LinkedHashSet
        Returns:
        an empty LinkedHashSet with sufficient capacity to hold expectedSize elements
        See Also:
        HashMap