Class CollectionHelper


  • public final class CollectionHelper
    extends Object
    Various helper util methods for handling collections.
    • Method Detail

      • mapOfSize

        public static <K,​V> HashMap<K,​V> mapOfSize​(int size)
        Build a properly sized map, especially handling load size and load factor to prevent immediate resizing.

        Especially helpful for copy map contents.

        Parameters:
        size - The size to make the map.
        Returns:
        The sized map.
      • linkedMapOfSize

        public static <K,​V> LinkedHashMap<K,​V> linkedMapOfSize​(int size)
        Build a properly sized linked map, especially handling load size and load factor to prevent immediate resizing.

        Especially helpful for copy map contents.

        Parameters:
        size - The size to make the map.
        Returns:
        The sized linked map.
      • map

        public static <K,​V> HashMap<K,​V> map()
        Build a map whose size is unknown.
        Returns:
        The map.
      • linkedMap

        public static <K,​V> LinkedHashMap<K,​V> linkedMap()
        Build a linked map whose size is unknown.
        Returns:
        The linked map.
      • setOfSize

        public static <K> HashSet<K> setOfSize​(int size)
        Build a properly sized set, especially handling load size and load factor to prevent immediate resizing.

        Especially helpful for copy set contents.

        Parameters:
        size - The size to make the set.
        Returns:
        The sized set.
      • set

        public static <K> HashSet<K> set()
        Build a set whose size is unknown.
        Returns:
        The set.
      • linkedSetOfSize

        public static <K> LinkedHashSet<K> linkedSetOfSize​(int size)
        Build a properly sized linked set, especially handling load size and load factor to prevent immediate resizing.

        Especially helpful for copy set contents.

        Parameters:
        size - The size to make the set.
        Returns:
        The sized linked set.
      • linkedSet

        public static <K> LinkedHashSet<K> linkedSet()
        Build a linked set whose size is unknown.
        Returns:
        The linked set.
      • determineProperSizing

        public static int determineProperSizing​(Map original)
        Given a map, determine the proper initial size for a new Map to hold the same number of values. Specifically we want to account for load size and load factor to prevent immediate resizing.
        Parameters:
        original - The original map
        Returns:
        The proper size.
      • makeCopy

        public static <X,​Y> Map<X,​Y> makeCopy​(Map<X,​Y> map)
      • makeCopy

        public static <K,​V> HashMap<K,​V> makeCopy​(Map<K,​V> original,
                                                              Function<K,​K> keyTransformer,
                                                              Function<V,​V> valueTransformer)
      • makeMap

        public static <K,​V> Map<K,​V> makeMap​(Collection<V> collection,
                                                         Function<V,​K> keyProducer)
      • makeMap

        public static <K,​V,​E> Map<K,​V> makeMap​(Collection<E> collection,
                                                                 Function<E,​K> keyProducer,
                                                                 Function<E,​V> valueProducer)
      • determineProperSizing

        public static int determineProperSizing​(Set original)
        Given a set, determine the proper initial size for a new set to hold the same number of values. Specifically we want to account for load size and load factor to prevent immediate resizing.
        Parameters:
        original - The original set
        Returns:
        The proper size.
      • determineProperSizing

        public static int determineProperSizing​(int numberOfElements)
        Determine the proper initial size for a new collection in order for it to hold the given a number of elements. Specifically we want to account for load size and load factor to prevent immediate resizing.
        Parameters:
        numberOfElements - The number of elements to be stored.
        Returns:
        The proper size.
      • concurrentMap

        public static <K,​V> ConcurrentHashMap<K,​V> concurrentMap​(int expectedNumberOfElements)
        Create a properly sized ConcurrentHashMap based on the given expected number of elements.
        Type Parameters:
        K - The map key type
        V - The map value type
        Parameters:
        expectedNumberOfElements - The expected number of elements for the created map
        Returns:
        The created map.
      • concurrentMap

        public static <K,​V> ConcurrentHashMap<K,​V> concurrentMap​(int expectedNumberOfElements,
                                                                             float loadFactor)
        Create a properly sized ConcurrentHashMap based on the given expected number of elements and an explicit load factor
        Type Parameters:
        K - The map key type
        V - The map value type
        Parameters:
        expectedNumberOfElements - The expected number of elements for the created map
        loadFactor - The collection load factor
        Returns:
        The created map.
      • arrayList

        public static <T> ArrayList<T> arrayList​(int expectedNumberOfElements)
      • makeCopy

        public static <T> Set<T> makeCopy​(Set<T> source)
      • isEmpty

        public static boolean isEmpty​(Collection collection)
      • isEmpty

        public static boolean isEmpty​(Map map)
      • isNotEmpty

        public static boolean isNotEmpty​(Collection collection)
      • isNotEmpty

        public static boolean isNotEmpty​(Map map)
      • isEmpty

        public static boolean isEmpty​(Object[] objects)
      • listOf

        public static <T> List<T> listOf​(T value1)
      • listOf

        public static <T> List<T> listOf​(T... values)
      • setOf

        public static <T> Set<T> setOf​(T... values)
      • asProperties

        public static Properties asProperties​(Map<?,​?> map)
      • toSmallSet

        public static <T> Set<T> toSmallSet​(Set<T> set)
        Use to convert sets which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance. The returned Set might be immutable, but there is no guarantee of this: consider it immutable but don't rely on this. The goal is to save memory.
      • toSmallMap

        public static <K,​V> Map<K,​V> toSmallMap​(Map<K,​V> map)
        Use to convert Maps which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance. The returned Map might be immutable, but there is no guarantee of this: consider it immutable but don't rely on this. The goal is to save memory.
      • toSmallList

        public static <V> List<V> toSmallList​(ArrayList<V> arrayList)
        Use to convert ArrayList instances which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance. The returned List might be immutable, but there is no guarantee of this: consider it immutable but don't rely on this. The goal is to save memory.
      • collectMapEntries

        public static <K,​V> void collectMapEntries​(BiConsumer<K,​V> mapEntryConsumer,
                                                         Object[] mappings)
      • asMap

        public static <K,​S> Map<K,​S> asMap​(Object[] elements)
      • applyToProperties

        public static void applyToProperties​(Properties properties,
                                             Object... pairs)