Class CollectionsUtil


  • public class CollectionsUtil
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> Filter<T> acceptAllFilter()
      Type safe accept all filter
      static <T> boolean addIfNotNull​(java.util.Collection<T> collection, T object)
      Adds an element to the collection unless the element is null.
      static <T,​Q>
      java.util.List<T>
      applyOrder​(Q[] sortedIds, java.util.List<T> allElements, java.util.Map<Q,​T> idToElementMap)
      Accepts elements and reorders them according to provided partial order of the elements.
      static <T> java.util.Map<T,​T> asMap​(T... args)
      Creates map from a given even number of parameters
      static <T> java.lang.String asString​(java.util.Collection<T> collection, int maxElems)  
      static <T> int binarySearch​(java.util.List<? extends T> list, com.intellij.openapi.util.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.
      static <SourceType>
      java.util.List<SourceType>
      collectUniqueObjectList​(java.util.List<SourceType> list)  
      static <T> boolean contains​(java.lang.Iterable<T> iterable, Filter<? super T> filter)
      Checks whether given iterable has at least one element satisfying the filter
      static <Result,​Source>
      java.util.List<Result>
      convertAndFilterNulls​(java.lang.Iterable<Source> source, Converter<Result,​Source> converter)
      Converts the collection (or any iterable) excluding the nulls afterwards.
      static <ResultType,​SourceType>
      java.util.List<ResultType>
      convertCollection​(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,​SourceType> converter)
      Create collection of type ResultType from collection of elements of type SourceType.
      static <ResultType,​SourceType>
      java.util.List<ResultType>
      convertCollection​(java.util.Collection<? extends SourceType> source, Converter<ResultType,​SourceType> converter)
      Create collection of type ResultType from collection of elements of type SourceType.
      static <K,​V,​K2,​V2>
      void
      convertMap​(java.util.Map<? extends K,​? extends V> source, java.util.Map<? super K2,​? super V2> target, Converter<K2,​K> keyConverter, Converter<V2,​V> valueConverter)
      Converts `Map<K, V>` into `Map<K2, V2>` coercing keys and values given respective converters
      static <ResultType,​SourceType>
      java.util.Set<ResultType>
      convertSet​(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,​SourceType> converter)
      Create set of type ResultType from collection of elements of type SourceType.
      static <ResultType,​SourceType>
      java.util.Set<ResultType>
      convertSet​(java.util.Collection<? extends SourceType> source, Converter<ResultType,​SourceType> converter)
      Create set of type ResultType from collection of elements of type SourceType.
      static <T extends java.lang.Enum<T>>
      java.util.EnumSet<T>
      copyOf​(java.lang.Class<T> type, java.util.Collection<T> from)  
      static <T> void copyWithPadding​(java.util.Collection<T> source, T[] target, T paddingValue)
      Copies values from the source collection to the target array.
      static <K,​V>
      int
      count​(java.util.Map<K,​V> map, Filter<K> keyFilter, Filter<V> valueFilter)
      counts a number of entries in map that matches given filters (by &&)
      static <K,​V>
      java.util.Map<K,​V>
      emptyHashMapIfNull​(java.util.Map<K,​V> map)  
      static <T> java.util.List<T> ensureModifiable​(java.util.List<T> items)  
      static <ResultType,​SourceType>
      java.util.List<ResultType>
      filterAndConvertCollection​(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,​SourceType> converter, Filter<SourceType> filter)
      Create collection of type ResultType from collection of elements of type SourceType.
      static <ResultType,​ResultCollection extends java.util.Collection<ResultType>,​SourceType>
      ResultCollection
      filterAndConvertCollection​(java.lang.Iterable<? extends SourceType> sources, ResultCollection collection, Converter<ResultType,​SourceType> converter, Filter<SourceType> filter)  
      static <ResultType,​SourceType>
      java.util.List<ResultType>
      filterAndConvertCollection​(java.util.Collection<? extends SourceType> source, Converter<ResultType,​SourceType> converter, Filter<SourceType> filter)
      Create collection of type ResultType from collection of elements of type SourceType.
      static <SourceType>
      java.util.List<SourceType>
      filterCollection​(java.lang.Iterable<? extends SourceType> source, Filter<SourceType> filter)
      Elements are filtered basing on Filter interface.
      static <SourceType>
      java.util.List<SourceType>
      filterCollection​(java.util.Collection<? extends SourceType> source, Filter<SourceType> filter)
      Elements are filtered basing on Filter interface.
      static <K,​V>
      java.util.Map<K,​V>
      filterMap​(java.util.Map<K,​V> map, Filter<K> keyFilter, Filter<V> valueFilter)
      Creates a copy of map filtering it by values filter, order is preserved
      static <K,​V>
      java.util.Map<K,​V>
      filterMapByKeys​(java.util.Map<K,​V> map, Filter<K> filter)
      Creates a copy of a map filtering it by keys filter, order is preserved
      static <K,​V>
      java.util.Map<K,​V>
      filterMapByValues​(java.util.Map<K,​V> map, Filter<V> filter)
      Creates a copy of map filtering it by values filter, order is preserved
      static <T> java.util.List<T> filterNulls​(java.util.Collection<T> collection)
      Filters the collection, excluding all nulls from it.
      static <T> T findFirst​(java.lang.Iterable<T> iterable, Filter<? super T> filter)
      Searches a collection and returns first element which fulfills given filter function
      static <T> T findFirst​(java.util.Iterator<T> iterator, Filter<? super T> filter)
      Traverses with the iterator and returns first element which fulfills given filter function.
      static <T> java.util.Set<T> flatten​(java.util.Collection<com.intellij.openapi.util.Pair<T,​T>> data)
      creates set of pair's first and seconds
      static <T> java.util.Set<T> flatten​(java.util.Map<T,​T> data)
      creates set of pair's first and seconds
      static <K,​V>
      java.util.Map<K,​java.util.List<V>>
      groupBy​(java.util.Collection<V> collection, Converter<K,​V> converter)
      Groups values in collection according to some computed attribute.
      static <T extends java.lang.Comparable<T>>
      void
      insertWithSorting​(T o, java.util.List<T> list)
      Insert an object into target collection into a position so collection remains sorted ascending
      static <T> void insertWithSorting​(T o, java.util.List<T> list, java.util.Comparator<T> comp)
      Insert an object into target collection into a position so collection remains sorted ascending
      static <T> java.util.Set<T> intersect​(java.util.Collection<T> set1, java.util.Collection<T> set2)
      Intersection: items present in both sets simultaneously.
      static <T> java.util.Collection<T> join​(java.util.Collection<T>... a)
      Merges two collections
      static <T> java.util.Collection<T> join​(java.util.Collection<T> a, java.util.Collection<T> b)
      Merges two collections
      static <T> java.util.Collection<T> join​(java.util.Collection<T> a, java.util.Collection<T> b, java.util.Collection<T> c)
      Merges two collections
      static <T> java.util.Set<T> minus​(java.util.Collection<T> minuend, java.util.Collection<T> subtrahend)
      Differs: items from the first collection excluding items from the second collection.
      static <T> java.util.SortedSet<T> minus​(java.util.SortedSet<T> minuend, java.util.Set<T> subtrahend)
      Differs: items from the first (sorted) set excluding items from the second set.
      static <T> void removeDuplicates​(java.util.Collection<T> collection)
      Removes the duplicates from the provided collection of elements.
      static <T> java.util.Collection<T> removeDuplicatesPreservingOrder​(java.util.Collection<T> collection)
      The same as removeDuplicates(java.util.Collection), but preserves the order of non duplicated elements.
      static <T,​Coll extends java.lang.Iterable<T>>
      Coll
      removeNulls​(Coll collection)  
      static void retainMatched​(java.util.Collection<java.lang.String> stringCollection, java.util.regex.Pattern pattern)  
      static <T> java.lang.Iterable<T> reversed​(java.util.List<T> list)
      Creates an iterable over a list looping in reverse direction.
      static <T> java.util.Set<T> setOf​(T... args)
      Creates a set from given parameters
      static <T extends java.lang.Comparable<? super T>>
      boolean
      sortedListsIntersect​(java.util.List<T> list1, java.util.List<T> list2)
      Accepts two sorted lists and returns true if they have at least one common element, or false otherwise
      static <T> java.util.List<java.util.List<T>> split​(java.util.List<T> toSplit, int numParts)
      Split one collection into a collection of smaller collections
      static <T> java.util.stream.Stream<T> streamOf​(java.util.Collection<T> collection)
      Creates a stream of nullable collection
      static java.lang.String to_s​(java.util.Collection c)
      Returns a debug-oriented representation of the collection, when all elements are joined with "\r\n"
      static <T> T[] toArray​(java.util.Collection<T> collection, java.lang.Class<T> clazz)
      Converts the given collection to an array.
      static <T> java.util.ArrayList<T> toArrayList​(java.util.Collection<T> items)  
      static java.lang.String[] toStringArray​(java.util.Collection<java.lang.String> strings)
      Converts the given collection of string to an array.
      static <T> java.util.Iterator<T> toTypedIterator​(java.lang.Class<T> clazz, java.util.Iterator data)
      Converts not-typed iterator to a typed iterator with lazy type check
      static <T> java.util.Collection<T> unite​(java.util.Collection<? extends T>... col)
      static <K,​E>
      java.util.Map<K,​java.util.Set<E>>
      unmodifiable​(java.util.Map<K,​java.util.Set<E>> map)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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> java.util.Set<T> flatten​(@NotNull
                                                   java.util.Collection<com.intellij.openapi.util.Pair<T,​T>> data)
        creates set of pair's first and seconds
        Since:
        8.1
      • flatten

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

        public static <ResultType,​SourceType> java.util.List<ResultType> filterAndConvertCollection​(java.util.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> java.util.List<ResultType> filterAndConvertCollection​(java.lang.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 java.util.Collection<ResultType>,​SourceType> ResultCollection filterAndConvertCollection​(java.lang.Iterable<? extends SourceType> sources,
                                                                                                                                                                @NotNull
                                                                                                                                                                ResultCollection collection,
                                                                                                                                                                @NotNull
                                                                                                                                                                Converter<ResultType,​SourceType> converter,
                                                                                                                                                                @NotNull
                                                                                                                                                                Filter<SourceType> filter)
      • join

        public static <T> java.util.Collection<T> join​(java.util.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> java.util.Collection<T> join​(@NotNull
                                                       java.util.Collection<T> a,
                                                       @NotNull
                                                       java.util.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> java.util.Collection<T> join​(@NotNull
                                                       java.util.Collection<T> a,
                                                       @NotNull
                                                       java.util.Collection<T> b,
                                                       @NotNull
                                                       java.util.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> java.util.List<SourceType> filterCollection​(java.util.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> java.util.List<SourceType> filterCollection​(java.lang.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> java.util.List<ResultType> convertCollection​(java.util.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> java.util.List<ResultType> convertCollection​(java.lang.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> java.util.Set<ResultType> convertSet​(java.util.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> java.util.Set<ResultType> convertSet​(java.lang.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> java.util.List<SourceType> collectUniqueObjectList​(java.util.List<SourceType> list)
      • findFirst

        @Nullable
        public static <T> T findFirst​(java.lang.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
                                      java.util.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​(java.lang.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 java.lang.Comparable<T>> void insertWithSorting​(@NotNull
                                                                                 T o,
                                                                                 @NotNull
                                                                                 java.util.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
                                                 java.util.List<T> list,
                                                 @NotNull
                                                 java.util.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 java.lang.String to_s​(@NotNull
                                            java.util.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> java.util.List<java.util.List<T>> split​(@NotNull
                                                                  java.util.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
                                         java.util.Collection<java.lang.String> stringCollection,
                                         @NotNull
                                         java.util.regex.Pattern pattern)
      • minus

        @NotNull
        public static <T> java.util.Set<T> minus​(java.util.Collection<T> minuend,
                                                 @NotNull
                                                 java.util.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> java.util.SortedSet<T> minus​(@NotNull
                                                       java.util.SortedSet<T> minuend,
                                                       @NotNull
                                                       java.util.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> java.util.Set<T> intersect​(@Nullable
                                                     java.util.Collection<T> set1,
                                                     @Nullable
                                                     java.util.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 java.lang.Comparable<? super T>> boolean sortedListsIntersect​(@NotNull
                                                                                               java.util.List<T> list1,
                                                                                               @NotNull
                                                                                               java.util.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 java.lang.Iterable<T>> Coll removeNulls​(@NotNull
                                                                                    Coll collection)
      • convertAndFilterNulls

        @NotNull
        public static <Result,​Source> java.util.List<Result> convertAndFilterNulls​(@NotNull
                                                                                         java.lang.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)
      • unite

        @NotNull
        @Deprecated
        public static <T> java.util.Collection<T> unite​(@NotNull
                                                        java.util.Collection<? extends T>... col)
        Parameters:
        col - collection(s)
        Returns:
        Returns a new collection containing all the elements from the given collections
        Since:
        7.0
      • applyOrder

        public static <T,​Q> java.util.List<T> applyOrder​(@NotNull
                                                               Q[] sortedIds,
                                                               @NotNull
                                                               java.util.List<T> allElements,
                                                               @NotNull
                                                               java.util.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> java.util.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> java.util.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
                                                java.util.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> java.util.Collection<T> removeDuplicatesPreservingOrder​(@NotNull
                                                                                  java.util.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
                                           java.util.List<? extends T> list,
                                           @NotNull
                                           com.intellij.openapi.util.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> java.util.Iterator<T> toTypedIterator​(@NotNull
                                                                java.lang.Class<T> clazz,
                                                                @NotNull
                                                                java.util.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> java.util.Map<K,​V> filterMapByKeys​(@NotNull
                                                                           java.util.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> java.util.Map<K,​V> filterMapByValues​(@NotNull
                                                                             java.util.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> java.util.Map<K,​V> filterMap​(@NotNull
                                                                     java.util.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
                                            java.util.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> java.util.Map<K,​java.util.List<V>> groupBy​(@NotNull
                                                                                   java.util.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
                                                                    java.util.Map<? extends K,​? extends V> source,
                                                                    @NotNull
                                                                    java.util.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
                                      java.util.Collection<T> collection,
                                      @NotNull
                                      java.lang.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 java.lang.String[] toStringArray​(@NotNull
                                                       java.util.Collection<java.lang.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> java.util.List<T> ensureModifiable​(@NotNull
                                                             java.util.List<T> items)
      • toArrayList

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

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

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

        @NotNull
        public static <T> java.lang.Iterable<T> reversed​(@NotNull
                                                         java.util.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> java.util.stream.Stream<T> streamOf​(@Nullable
                                                              java.util.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> java.lang.String asString​(@NotNull
                                                    java.util.Collection<T> collection,
                                                    int maxElems)
      • copyOf

        public static <T extends java.lang.Enum<T>> java.util.EnumSet<T> copyOf​(@NotNull
                                                                                java.lang.Class<T> type,
                                                                                @NotNull
                                                                                java.util.Collection<T> from)
      • unmodifiable

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

        public static <T> void copyWithPadding​(@NotNull
                                               java.util.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