Class CollectionHelper


  • public final class CollectionHelper
    extends Object
    Since:
    1.0.0
    • Method Detail

      • map

        @SafeVarargs
        public static <K,​V> Map<K,​V> map​(Map.Entry<K,​V>... entries)
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        entries - the final set of entries to add to the newly created unmodifiable map
        Returns:
        an unmodifiable map with all given entries
      • set

        @SafeVarargs
        public static <T> Set<T> set​(T... elements)
        Type Parameters:
        T - the element type
        Parameters:
        elements - the elements
        Returns:
        an UNMODIFIABLE Set<T>
      • list

        @SafeVarargs
        public static <T> List<T> list​(T... elements)
        Type Parameters:
        T - the element type
        Parameters:
        elements - the elements
        Returns:
        an UNMODIFIABLE List<T>
      • union

        public static <T> Set<T> union​(Iterable<Set<T>> elements)
      • intersection

        @SafeVarargs
        public static <T> Set<T> intersection​(Set<T>... elements)
      • intersection

        public static <T> Set<T> intersection​(Iterable<Set<T>> sets)
      • unmodifiableSet

        public static <T> Set<? extends T> unmodifiableSet​(Set<? extends T> s)
        Type Parameters:
        T - the element type
        Parameters:
        s - the set
        Returns:
        an UNMODIFIABLE Set<T>
      • unmodifiableMap

        public static <K,​V> Map<? extends K,​? extends V> unmodifiableMap​(Map<? extends K,​? extends V> m)
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        m - the map
        Returns:
        an UNMODIFIABLE Map<K, V>
      • unmodifiableCollection

        public static <T> Collection<? extends T> unmodifiableCollection​(Collection<? extends T> c)
        Type Parameters:
        T - the element type
        Parameters:
        c - the collection
        Returns:
        an UNMODIFIABLE Collection<T>
      • unmodifiableList

        public static <T> List<? extends T> unmodifiableList​(List<? extends T> l)
        Type Parameters:
        T - the element type
        Parameters:
        l - the list
        Returns:
        an UNMODIFIABLE List<T>
      • conjunctCollectionsToSet

        public static <T> Set<T> conjunctCollectionsToSet​(Collection<T> list1,
                                                          Collection<T> list2)
      • synchronizedInitialSizeMapWithLoadFactor1

        public static <K,​V> Map<K,​V> synchronizedInitialSizeMapWithLoadFactor1​(int capacity)
      • synchronizedMap

        public static <K,​V> Map<K,​V> synchronizedMap()
      • synchronizedMap

        public static <K,​V> Map<K,​V> synchronizedMap​(int initialCapacity)
      • synchronizedMap

        public static <K,​V> Map<K,​V> synchronizedMap​(int initialCapacity,
                                                                 float loadFactor)
      • synchronizedSet

        public static <T> Set<T> synchronizedSet()
        Constructs a new synchronized Set based on a HashSet.
        Type Parameters:
        T - the element type
        Returns:
        a synchronized Set
      • synchronizedSet

        public static <T> Set<T> synchronizedSet​(int initialCapacity)
        Constructs a new synchronized Set based on a HashSet with the specified initialCapacity.
        Type Parameters:
        T - the element type
        Parameters:
        initialCapacity - the initial capacity of the set
        Returns:
        a synchronized Set
      • synchronizedList

        public static <E> List<E> synchronizedList()
        Constructs a new synchronized List based on a LinkedList.
        Type Parameters:
        E - the element type
        Returns:
        a synchronized List
      • synchronizedList

        public static <E> List<E> synchronizedList​(int initialCapacity)
        Constructs a new synchronized List based on a ArrayList with the specified initialCapacity.
        Type Parameters:
        E - the element type
        Parameters:
        initialCapacity - the initial capacity of the array list
        Returns:
        a synchronized List
      • unionOfListOfLists

        public static <T> Set<T> unionOfListOfLists​(Collection<? extends Collection<? extends T>> collectionOfCollection)
        Type Parameters:
        T - the element type
        Parameters:
        collectionOfCollection - a Collection&lt;Collection&lt;T&gt;&gt;
        Returns:
        a Set<T> containing all values of all Collections<T> without any duplicates
      • isNotEmpty

        public static <T> boolean isNotEmpty​(Collection<T> collection)
        Check if collection is not null and not empty
        Type Parameters:
        T - the element type
        Parameters:
        collection - Collection to check
        Returns:
        empty or not
      • isNotEmpty

        public static <K,​V> boolean isNotEmpty​(Map<K,​V> map)
        Check if collection is not null and not empty
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - Map to check
        Returns:
        false, if map is null or empty, else true.
      • isEmptyOrNull

        public static <T> boolean isEmptyOrNull​(Collection<T> collection)
      • isEmpty

        public static <T> boolean isEmpty​(Collection<T> collection)
        Check if collection is not null and empty
        Type Parameters:
        T - the element type
        Parameters:
        collection - Collection to check
        Returns:
        true, if collection is not null and empty, else false
      • isEmpty

        public static <K,​V> boolean isEmpty​(Map<K,​V> map)
        Check if map is not null and empty
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - map to check
        Returns:
        true, if map is not null and empty, else false
      • reverse

        public static <K,​V> Map<V,​K> reverse​(Map<K,​V> map)
        Reverses a map (switches key and value types).
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - the map
        Returns:
        the reversed map
      • nullEmptyOrContainsOnlyNulls

        public static boolean nullEmptyOrContainsOnlyNulls​(Collection<? extends Object> collection)
        Examine a collection and determines if it is null, empty, or contains only null values
        Parameters:
        collection - Collection to examine
        Returns:
        whether the collection is null, empty, or contains only nulls
      • isNotNullOrEmpty

        public static boolean isNotNullOrEmpty​(Object[] array)
        Check if array is not null and not empty
        Parameters:
        array - Array to check
        Returns:
        true, if array is not null and not empty
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(Object[] array)
        Check if array is not null or not empty
        Parameters:
        array - Array to check
        Returns:
        true, if array is null or empty
      • collectionToString

        public static String collectionToString​(Collection<?> collection)
      • addToCollectionMap

        public static <K,​V> void addToCollectionMap​(K key,
                                                          V valueToAdd,
                                                          Map<K,​Collection<V>> map)
        Add a value to a map collection, initializing the key's collection if needed
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        key - Key whose value collection should be added to
        valueToAdd - Vale to add to the key's collection
        map - Map holding collections
      • sortByValue

        public static <K,​V extends Comparable<? super V>> Map<K,​V> sortByValue​(Map<K,​V> map)
      • csvStringToList

        public static List<String> csvStringToList​(String csv)
        Parse CSV string to List
        Parameters:
        csv - CSV string
        Returns:
        List with separated values
      • svStringToList

        public static List<String> svStringToList​(String sv,
                                                  String separator)
        Parse separated value string to List
        Parameters:
        sv - Separated value string
        separator - Separator character
        Returns:
        List with separated values
      • csvStringToSet

        public static Set<String> csvStringToSet​(String csv)
        Parse CSV string to Set
        Parameters:
        csv - CSV string
        Returns:
        Set with separated values
      • svStringToSet

        public static Set<String> svStringToSet​(String sv,
                                                String separator)
        Parse separated value string to Set
        Parameters:
        sv - Separated value string
        separator - Seperator character
        Returns:
        Set with separated values
      • svStringToArray

        public static String[] svStringToArray​(String sv,
                                               String separator)
        Parse separated value string to array with trimmed values
        Parameters:
        sv - Separated value string
        separator - Separator character
        Returns:
        Array with separated values