Class CollectionsUtil


  • public class CollectionsUtil
    extends Object
    • Field Detail

      • ACCEPT_ALL

        public static final Filter ACCEPT_ALL
    • Method Detail

      • acceptAllFilter

        @NotNull
        public static <T> Filter<T> acceptAllFilter()
        Type safe accept all filter
        Type Parameters:
        T - type
        Returns:
        filter accepting all values
        Since:
        7.1
      • flatten

        @NotNull
        public static <T> Set<T> flatten​(@NotNull
                                         Collection<Pair<T,​T>> data)
        creates set of pair's first and seconds
        Since:
        8.1
      • flatten

        @NotNull
        public static <T> Set<T> flatten​(@NotNull
                                         Map<T,​T> data)
        creates set of pair's first and seconds
        Since:
        8.1
      • filterAndConvertCollection

        public static <ResultType,​SourceType> List<ResultType> filterAndConvertCollection​(Collection<? extends SourceType> source,
                                                                                                @NotNull
                                                                                                Converter<ResultType,​SourceType> converter,
                                                                                                @NotNull
                                                                                                Filter<SourceType> filter)
        Create collection of type ResultType from collection of elements of type SourceType. Elements are filtered basing on Filter interface and conversion from SourceType to ResultType is performed using Converter interface.
        Parameters:
        source - source collection
        converter - converter from one element to another
        filter - filter which allows to skip some elements of the source collection
        Returns:
        converted and filtered collection
      • filterAndConvertCollection

        public static <ResultType,​SourceType> List<ResultType> filterAndConvertCollection​(Iterable<? extends SourceType> source,
                                                                                                @NotNull
                                                                                                Converter<ResultType,​SourceType> converter,
                                                                                                @NotNull
                                                                                                Filter<SourceType> filter)
        Create collection of type ResultType from collection of elements of type SourceType. Elements are filtered basing on Filter interface and conversion from SourceType to ResultType is performed using Converter interface.
        Parameters:
        source - source collection
        converter - converter from one element to another
        filter - filter which allows to skip some elements of the source collection
        Returns:
        converted and filtered collection
      • filterAndConvertCollection

        public static <ResultType,​ResultCollection extends Collection<ResultType>,​SourceType> ResultCollection filterAndConvertCollection​(Iterable<? extends SourceType> sources,
                                                                                                                                                      @NotNull
                                                                                                                                                      ResultCollection collection,
                                                                                                                                                      @NotNull
                                                                                                                                                      Converter<ResultType,​SourceType> converter,
                                                                                                                                                      @NotNull
                                                                                                                                                      Filter<SourceType> filter)
      • join

        public static <T> Collection<T> join​(Collection<T>... a)
        Merges two collections
        Type Parameters:
        T - type of collection elements
        Parameters:
        a - collections to join
        Returns:
        merged collection
        Since:
        5.1
      • join

        public static <T> Collection<T> join​(@NotNull
                                             Collection<T> a,
                                             @NotNull
                                             Collection<T> b)
        Merges two collections
        Type Parameters:
        T - type of collection elements
        Parameters:
        a - collection
        b - collection
        Returns:
        merged collection
        Since:
        5.1
      • join

        public static <T> Collection<T> join​(@NotNull
                                             Collection<T> a,
                                             @NotNull
                                             Collection<T> b,
                                             @NotNull
                                             Collection<T> c)
        Merges two collections
        Type Parameters:
        T - type of collection elements
        Parameters:
        a - collection
        b - collection
        c - collection
        Returns:
        merged collection
        Since:
        5.1
      • filterCollection

        public static <SourceType> List<SourceType> filterCollection​(Collection<? extends SourceType> source,
                                                                     @NotNull
                                                                     Filter<SourceType> filter)
        Elements are filtered basing on Filter interface.
        Parameters:
        source - source collection
        filter - filter which allows to skip some elements of the source collection
        Returns:
        filtered collection
      • filterCollection

        public static <SourceType> List<SourceType> filterCollection​(Iterable<? extends SourceType> source,
                                                                     @NotNull
                                                                     Filter<SourceType> filter)
        Elements are filtered basing on Filter interface.
        Parameters:
        source - source collection
        filter - filter which allows to skip some elements of the source collection
        Returns:
        filtered collection
      • convertCollection

        public static <ResultType,​SourceType> List<ResultType> convertCollection​(Collection<? extends SourceType> source,
                                                                                       Converter<ResultType,​SourceType> converter)
        Create collection of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.
        Parameters:
        source - source collection
        converter - converter from one element to another
        Returns:
        converted and filtered collection
      • convertCollection

        public static <ResultType,​SourceType> List<ResultType> convertCollection​(Iterable<? extends SourceType> source,
                                                                                       Converter<ResultType,​SourceType> converter)
        Create collection of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.
        Parameters:
        source - source collection
        converter - converter from one element to another
        Returns:
        converted and filtered collection
      • convertSet

        public static <ResultType,​SourceType> Set<ResultType> convertSet​(Collection<? extends SourceType> source,
                                                                               Converter<ResultType,​SourceType> converter)
        Create set of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.
        Parameters:
        source - source collection
        converter - converter from one element to another
        Returns:
        converted and filtered collection
      • convertSet

        public static <ResultType,​SourceType> Set<ResultType> convertSet​(Iterable<? extends SourceType> source,
                                                                               Converter<ResultType,​SourceType> converter)
        Create set of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.
        Parameters:
        source - source collection
        converter - converter from one element to another
        Returns:
        converted and filtered collection
      • collectUniqueObjectList

        public static <SourceType> List<SourceType> collectUniqueObjectList​(List<SourceType> list)
      • findFirst

        @Nullable
        public static <T> T findFirst​(Iterable<T> iterable,
                                      Filter<? super T> filter)
        Searches a collection and returns first element which fulfills given filter function
        Parameters:
        iterable - collection of elements
        filter - filter to satisfy
        Returns:
        first element for which filter function returns true or null if no such element exists
        Since:
        7.0
      • findFirst

        @Nullable
        public static <T> T findFirst​(@NotNull
                                      Iterator<T> iterator,
                                      @NotNull
                                      Filter<? super T> filter)
        Traverses with the iterator and returns first element which fulfills given filter function. The iterator state will be changed after this method use!
        Parameters:
        iterator - iterator
        filter - filter to satisfy
        Returns:
        first element for which filter function returns true or null if no such element exists
        Since:
        2021.2
      • contains

        public static <T> boolean contains​(Iterable<T> iterable,
                                           Filter<? super T> filter)
        Checks whether given iterable has at least one element satisfying the filter
        Parameters:
        iterable - collection to check
        Returns:
        true if iterable has elements satisfying the filter, else otherwise
        Since:
        8.1
      • insertWithSorting

        public static <T extends Comparable<T>> void insertWithSorting​(@NotNull
                                                                       T o,
                                                                       @NotNull
                                                                       List<T> list)
        Insert an object into target collection into a position so collection remains sorted ascending
        Parameters:
        o - object to insert
        list - list of objects which should be sorted. All items should be added to the list using this function
      • insertWithSorting

        public static <T> void insertWithSorting​(@NotNull
                                                 T o,
                                                 @NotNull
                                                 List<T> list,
                                                 @NotNull
                                                 Comparator<T> comp)
        Insert an object into target collection into a position so collection remains sorted ascending
        Parameters:
        o - object to insert
        list - list of objects which should be sorted. All items should be added to the list using this function and using same comparator
        comp - comparator for the objects in the list
      • to_s

        @NotNull
        public static String to_s​(@NotNull
                                  Collection c)
        Returns a debug-oriented representation of the collection, when all elements are joined with "\r\n"
        Parameters:
        c - collection to process
        Returns:
        see above
      • split

        @NotNull
        public static <T> List<List<T>> split​(@NotNull
                                              List<T> toSplit,
                                              int numParts)
        Split one collection into a collection of smaller collections
        Type Parameters:
        T - collection items' base type
        Parameters:
        toSplit - collection to split
        numParts - number of parts to have
        Returns:
        collection of small collections
      • retainMatched

        public static void retainMatched​(@Nullable
                                         Collection<String> stringCollection,
                                         @NotNull
                                         Pattern pattern)
      • minus

        @NotNull
        public static <T> Set<T> minus​(Collection<T> minuend,
                                       @NotNull
                                       Collection<T> subtrahend)
        Differs: items from the first collection excluding items from the second collection.
        Type Parameters:
        T - item type.
        Parameters:
        minuend - first collection.
        subtrahend - second collection.
        Returns:
        the remainder of differs; if the minuend is a kind of SortedSet the result will be an instance of SortedSet with the same comparator.
      • minus

        @NotNull
        public static <T> SortedSet<T> minus​(@NotNull
                                             SortedSet<T> minuend,
                                             @NotNull
                                             Set<T> subtrahend)
        Differs: items from the first (sorted) set excluding items from the second set.
        Type Parameters:
        T - item type.
        Parameters:
        minuend - first set.
        subtrahend - second set.
        Returns:
        the remainder of differs; with the comparator from minuend.
        Since:
        8.0
      • intersect

        @NotNull
        public static <T> Set<T> intersect​(@Nullable
                                           Collection<T> set1,
                                           @Nullable
                                           Collection<T> set2)
        Intersection: items present in both sets simultaneously.
        Type Parameters:
        T - item type.
        Parameters:
        set1 - first set.
        set2 - second set.
        Returns:
        the intersection of both sets.
      • sortedListsIntersect

        public static <T extends Comparable<? super T>> boolean sortedListsIntersect​(@NotNull
                                                                                     List<T> list1,
                                                                                     @NotNull
                                                                                     List<T> list2)
        Accepts two sorted lists and returns true if they have at least one common element, or false otherwise
        Parameters:
        list1 - - first list
        list2 - - second list
        Returns:
        see above
        Since:
        2018.1.2
      • removeNulls

        @NotNull
        public static <T,​Coll extends Iterable<T>> Coll removeNulls​(@NotNull
                                                                          Coll collection)
      • convertAndFilterNulls

        @NotNull
        public static <Result,​Source> List<Result> convertAndFilterNulls​(@NotNull
                                                                               Iterable<Source> source,
                                                                               @NotNull
                                                                               Converter<Result,​Source> converter)
        Converts the collection (or any iterable) excluding the nulls afterwards.
        Type Parameters:
        Result - result type
        Source - source type
        Parameters:
        source - the source iterable
        converter - the converter
        Returns:
        a converter collection without nulls
        Since:
        7.1
        See Also:
        convertCollection(Iterable, Converter)
      • applyOrder

        public static <T,​Q> List<T> applyOrder​(@NotNull
                                                     Q[] sortedIds,
                                                     @NotNull
                                                     List<T> allElements,
                                                     @NotNull
                                                     Map<Q,​T> idToElementMap)
        Accepts elements and reorders them according to provided partial order of the elements. Elements must be identifiable by string id. New order of elements is defined as the sorted array of identifiers. Note that array of identifiers size may not be the same as the size of the whole collection.
        Parameters:
        sortedIds - new order of identifiers
        allElements - elements to sort
        idToElementMap - map from element id to element
        Returns:
        reordered elements
      • asMap

        @NotNull
        public static <T> Map<T,​T> asMap​(@NotNull
                                               T... args)
        Creates map from a given even number of parameters
        Type Parameters:
        T - type of parameters
        Parameters:
        args - parameters as sequence of key, value
        Returns:
        map
        Since:
        7.0
      • setOf

        @NotNull
        public static <T> Set<T> setOf​(@NotNull
                                       T... args)
        Creates a set from given parameters
        Type Parameters:
        T - type of element
        Parameters:
        args - elements of set
        Returns:
        set
        Since:
        7.0
      • removeDuplicates

        public static <T> void removeDuplicates​(@NotNull
                                                Collection<T> collection)
        Removes the duplicates from the provided collection of elements.

        Notes:

        • If the collection is ordered (e.g. java.util.List), the result order is not preserved.
        • The elements are required to support hashCode and equals methods.
        Type Parameters:
        T - any type able to store in HashSet
        Parameters:
        collection - the collection to remove duplicates from
      • removeDuplicatesPreservingOrder

        @NotNull
        public static <T> Collection<T> removeDuplicatesPreservingOrder​(@NotNull
                                                                        Collection<T> collection)
        The same as removeDuplicates(java.util.Collection), but preserves the order of non duplicated elements.

        In some cases may return the same collection instance (but without duplicates).

        Type Parameters:
        T - any type able to store in HashSet
        Parameters:
        collection - the collection to remove duplicates from
        Returns:
        a filtered collection
        Since:
        8.0
      • binarySearch

        public static <T> int binarySearch​(@NotNull
                                           List<? extends T> list,
                                           @NotNull
                                           Condition<? super T> condition)
        Returns the index of the first element for which condition is true.

        It is supposed for the list that some index K exists such as:
        1. 0 <= K <= list size
        2. condition is false for all indexes those are less than K
        3. condition is true for all indexes those are equal or greater than K
        Returns:
        K
        Since:
        6.5.2
      • toTypedIterator

        @NotNull
        public static <T> Iterator<T> toTypedIterator​(@NotNull
                                                      Class<T> clazz,
                                                      @NotNull
                                                      Iterator data)
        Converts not-typed iterator to a typed iterator with lazy type check
        Type Parameters:
        T - type of elements
        Parameters:
        clazz - class of items
        data - not-typed iterator
        Returns:
        lazy wrapper for a given iterator of given type
        Since:
        7.0
      • filterMapByKeys

        @NotNull
        public static <K,​V> Map<K,​V> filterMapByKeys​(@NotNull
                                                                 Map<K,​V> map,
                                                                 @NotNull
                                                                 Filter<K> filter)
        Creates a copy of a map filtering it by keys filter, order is preserved
        Type Parameters:
        K - type
        V - type
        Parameters:
        map - map
        filter - keys filter
        Returns:
        copy of map with subset of values
        Since:
        7.1
      • filterMapByValues

        @NotNull
        public static <K,​V> Map<K,​V> filterMapByValues​(@NotNull
                                                                   Map<K,​V> map,
                                                                   @NotNull
                                                                   Filter<V> filter)
        Creates a copy of map filtering it by values filter, order is preserved
        Type Parameters:
        K - type
        V - type
        Parameters:
        map - map
        filter - values filter
        Returns:
        copy of map with subset of values
        Since:
        7.1
      • filterMap

        @NotNull
        public static <K,​V> Map<K,​V> filterMap​(@NotNull
                                                           Map<K,​V> map,
                                                           @NotNull
                                                           Filter<K> keyFilter,
                                                           @NotNull
                                                           Filter<V> valueFilter)
        Creates a copy of map filtering it by values filter, order is preserved
        Type Parameters:
        K - type
        V - type
        Parameters:
        map - map
        keyFilter - keys filter
        valueFilter - values filter
        Returns:
        copy of map with subset of values
        Since:
        7.1
      • count

        public static <K,​V> int count​(@NotNull
                                            Map<K,​V> map,
                                            @NotNull
                                            Filter<K> keyFilter,
                                            @NotNull
                                            Filter<V> valueFilter)
        counts a number of entries in map that matches given filters (by &&)
        Type Parameters:
        K - type
        V - type
        Parameters:
        map - map
        keyFilter - keys filter
        valueFilter - values filter
        Returns:
        number of (key,value) pairs matching both filters
        Since:
        7.1
      • groupBy

        @NotNull
        public static <K,​V> Map<K,​List<V>> groupBy​(@NotNull
                                                               Collection<V> collection,
                                                               @NotNull
                                                               Converter<K,​V> converter)
        Groups values in collection according to some computed attribute. Note: 'null' elements from collection will be stored in group with 'null' key
        Type Parameters:
        K - grouping attribute
        V - value to group
        Parameters:
        collection - collection to group
        converter - converter which computes attribute to group by
        Returns:
        map where key is grouping attribute and value is list of values corresponding to this attribute
        Since:
        8.0
      • convertMap

        public static <K,​V,​K2,​V2> void convertMap​(@NotNull
                                                                    Map<? extends K,​? extends V> source,
                                                                    @NotNull
                                                                    Map<? super K2,​? super V2> target,
                                                                    @NotNull
                                                                    Converter<K2,​K> keyConverter,
                                                                    @NotNull
                                                                    Converter<V2,​V> valueConverter)
        Converts `Map<K, V>` into `Map<K2, V2>` coercing keys and values given respective converters
        Parameters:
        source - source-map
        target - target-map
        keyConverter - keys converter
        valueConverter - values converter
      • toArray

        public static <T> T[] toArray​(@NotNull
                                      Collection<T> collection,
                                      @NotNull
                                      Class<T> clazz)
        Converts the given collection to an array.
        Type Parameters:
        T - items class.
        Parameters:
        collection - the collection to convert.
        clazz - items class.
        Returns:
        the created array.
        Since:
        9.0
      • toStringArray

        public static String[] toStringArray​(@NotNull
                                             Collection<String> strings)
        Converts the given collection of string to an array.
        Parameters:
        strings - the collection to convert.
        Returns:
        created array of string.
        Since:
        9.0
      • ensureModifiable

        @NotNull
        public static <T> List<T> ensureModifiable​(@NotNull
                                                   List<T> items)
      • toArrayList

        @NotNull
        public static <T> ArrayList<T> toArrayList​(@NotNull
                                                   Collection<T> items)
      • emptyHashMapIfNull

        public static <K,​V> Map<K,​V> emptyHashMapIfNull​(Map<K,​V> map)
      • addIfNotNull

        public static <T> boolean addIfNotNull​(@NotNull
                                               Collection<T> collection,
                                               @Nullable
                                               T object)
        Adds an element to the collection unless the element is null.
      • reversed

        @NotNull
        public static <T> Iterable<T> reversed​(@NotNull
                                               List<T> list)
        Creates an iterable over a list looping in reverse direction. Useful in foreach constructions.
        Parameters:
        list - - list to be processed
        Returns:
        an iterable over a list looping in reverse direction. Useful in foreach constructions
      • streamOf

        @NotNull
        public static <T> Stream<T> streamOf​(@Nullable
                                             Collection<T> collection)
        Creates a stream of nullable collection
        Parameters:
        collection - nullable collection
        Returns:
        empty stream if collection is null, collection.stream() otherwise
      • asString

        @NotNull
        public static <T> String asString​(@NotNull
                                          Collection<T> collection,
                                          int maxElems)
      • unmodifiable

        @NotNull
        public static <K,​E> Map<K,​Set<E>> unmodifiable​(@NotNull
                                                                   Map<K,​Set<E>> map)
      • copyWithPadding

        public static <T> void copyWithPadding​(@NotNull
                                               Collection<T> source,
                                               @NotNull
                                               T[] target,
                                               @NotNull
                                               T paddingValue)
        Copies values from the source collection to the target array. If target array is bigger than the source then the rest of the array is filled with the padding value.
        Type Parameters:
        T -
        Parameters:
        source - source collection
        target - target array
        paddingValue - padding value
        Since:
        2023.11
      • copyWithPadding

        public static <T> void copyWithPadding​(@NotNull
                                               Collection<T> source,
                                               @NotNull
                                               T[] target,
                                               int targetStartIdx,
                                               @NotNull
                                               T paddingValue)
        Copies values from the source collection to the target array. If target array is bigger than the source then the rest of the array is filled with the padding value.
        Type Parameters:
        T -
        Parameters:
        source - source collection
        target - target array
        paddingValue - padding value
        Since:
        2024.07