Class MoreCollectors


  • public final class MoreCollectors
    extends Object
    Implementations of several collectors in addition to ones available in JDK.
    Since:
    0.3.2
    Author:
    Tagir Valeev
    See Also:
    Collectors, Joining
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Collector<T,​?,​OptionalInt> andingInt​(ToIntFunction<T> mapper)
      Returns a Collector which performs the bitwise-and operation of a integer-valued function applied to the input elements.
      static <T> Collector<T,​?,​OptionalLong> andingLong​(ToLongFunction<T> mapper)
      Returns a Collector which performs the bitwise-and operation of a long-valued function applied to the input elements.
      static <T,​A,​R,​RR>
      Collector<T,​A,​RR>
      collectingAndThen​(Collector<T,​A,​R> downstream, Function<R,​RR> finisher)
      Adapts a Collector to perform an additional finishing transformation.
      static Collector<CharSequence,​?,​String> commonPrefix()
      Returns a Collector which computes a common prefix of input CharSequence objects returning the result as String.
      static Collector<CharSequence,​?,​String> commonSuffix()
      Returns a Collector which computes a common suffix of input CharSequence objects returning the result as String.
      static <T> Collector<T,​?,​Integer> countingInt()
      Returns a Collector accepting elements of type T that counts the number of input elements and returns result as Integer .
      static <T> Collector<T,​?,​List<T>> distinctBy​(Function<? super T,​?> mapper)
      Returns a Collector which collects into the List the input elements for which given mapper function returns distinct results.
      static <T> Collector<T,​?,​Integer> distinctCount​(Function<? super T,​?> mapper)
      Returns a Collector which counts a number of distinct values the mapper function returns for the stream elements.
      static <T> Collector<T,​?,​List<T>> dominators​(BiPredicate<? super T,​? super T> isDominator)
      Returns a collector which collects input elements into List removing the elements following their dominator element.
      static <K,​V,​M extends Map<K,​V>>
      Collector<Map.Entry<? extends K,​? extends V>,​?,​M>
      entriesToCustomMap​(BinaryOperator<V> combiner, Supplier<M> mapSupplier)
      Returns a Collector that accumulates elements into a result Map defined by mapSupplier function whose keys and values are taken from Map.Entry and combining them using the provided combiner function to the input elements.
      static <K,​V,​M extends Map<K,​V>>
      Collector<Map.Entry<? extends K,​? extends V>,​?,​M>
      entriesToCustomMap​(Supplier<M> mapSupplier)
      Returns a Collector that accumulates elements into a result Map defined by mapSupplier function whose keys and values are taken from Map.Entry.
      static <K,​V>
      Collector<Map.Entry<? extends K,​? extends V>,​?,​Map<K,​V>>
      entriesToMap()
      Returns a Collector that accumulates elements into a Map whose keys and values are taken from Map.Entry.
      static <K,​V>
      Collector<Map.Entry<? extends K,​? extends V>,​?,​Map<K,​V>>
      entriesToMap​(BinaryOperator<V> combiner)
      Returns a Collector that accumulates elements into a Map whose keys and values are taken from Map.Entry and combining them using the provided combiner function to the input elements.
      static <T> Collector<T,​?,​List<T>> filtering​(Predicate<? super T> predicate)
      Returns a Collector which filters input elements by the supplied predicate, collecting them to the list.
      static <T,​A,​R>
      Collector<T,​?,​R>
      filtering​(Predicate<? super T> predicate, Collector<T,​A,​R> downstream)
      Returns a Collector which passes only those elements to the specified downstream collector which match given predicate.
      static <T> Collector<T,​?,​Optional<T>> first()
      Returns a Collector which collects only the first stream element if any.
      static <T,​U>
      Collector<T,​?,​List<U>>
      flatMapping​(Function<? super T,​? extends Stream<? extends U>> mapper)
      Returns a collector which launches a flat mapping function for each input element and collects the elements of the resulting streams to the flat List.
      static <T,​U,​A,​R>
      Collector<T,​?,​R>
      flatMapping​(Function<? super T,​? extends Stream<? extends U>> mapper, Collector<? super U,​A,​R> downstream)
      Adapts a Collector accepting elements of type U to one accepting elements of type T by applying a flat mapping function to each input element before accumulation.
      static <T extends Comparable<? super T>>
      Collector<T,​?,​List<T>>
      greatest​(int n)
      Returns a Collector which collects at most specified number of the greatest stream elements according to the natural order into the List.
      static <T> Collector<T,​?,​List<T>> greatest​(Comparator<? super T> comparator, int n)
      Returns a Collector which collects at most specified number of the greatest stream elements according to the specified Comparator into the List.
      static <T,​K,​D,​A,​M extends Map<K,​D>>
      Collector<T,​?,​M>
      groupingBy​(Function<? super T,​? extends K> classifier, Set<K> domain, Supplier<M> mapFactory, Collector<? super T,​A,​D> downstream)
      Returns a Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector.
      static <T,​K,​D,​A>
      Collector<T,​?,​Map<K,​D>>
      groupingBy​(Function<? super T,​? extends K> classifier, Set<K> domain, Collector<? super T,​A,​D> downstream)
      Returns a Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector.
      static <T,​K extends Enum<K>,​A,​D>
      Collector<T,​?,​EnumMap<K,​D>>
      groupingByEnum​(Class<K> enumClass, Function<? super T,​K> classifier, Collector<? super T,​A,​D> downstream)
      Returns a Collector implementing a cascaded "group by" operation on input elements of type T, for classification function which maps input elements to the enum values.
      static <T> Collector<T,​?,​List<T>> head​(int n)
      Returns a Collector which collects at most specified number of the first stream elements into the List.
      static <T,​A,​R>
      Collector<T,​?,​Optional<R>>
      ifAllMatch​(Predicate<T> predicate, Collector<T,​A,​R> downstream)
      Returns a Collector which performs downstream reduction if all elements satisfy the Predicate.
      static <T,​S extends Collection<T>>
      Collector<S,​?,​Set<T>>
      intersecting()
      Returns a Collector which collects the intersection of the input collections into the newly-created Set.
      static <T> Collector<T,​?,​Optional<T>> last()
      Returns a Collector which collects only the last stream element if any.
      static <T extends Comparable<? super T>>
      Collector<T,​?,​List<T>>
      least​(int n)
      Returns a Collector which collects at most specified number of the least stream elements according to the natural order into the List.
      static <T> Collector<T,​?,​List<T>> least​(Comparator<? super T> comparator, int n)
      Returns a Collector which collects at most specified number of the least stream elements according to the specified Comparator into the List.
      static <T,​U>
      Collector<T,​?,​List<U>>
      mapping​(Function<? super T,​? extends U> mapper)
      Returns a collector which collects input elements to the new List transforming them with the supplied function beforehand.
      static <T,​U,​A,​R>
      Collector<T,​?,​R>
      mapping​(Function<? super T,​? extends U> mapper, Collector<? super U,​A,​R> downstream)
      Adapts a Collector accepting elements of type U to one accepting elements of type T by applying a mapping function to each input element before accumulation.
      static <T extends Comparable<? super T>>
      Collector<T,​?,​List<T>>
      maxAll()
      Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the natural order.
      static <T> Collector<T,​?,​List<T>> maxAll​(Comparator<? super T> comparator)
      Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the specified Comparator.
      static <T,​A,​D>
      Collector<T,​?,​D>
      maxAll​(Comparator<? super T> comparator, Collector<? super T,​A,​D> downstream)
      Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the specified Comparator.
      static <T extends Comparable<? super T>,​A,​D>
      Collector<T,​?,​D>
      maxAll​(Collector<T,​A,​D> downstream)
      Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the natural order.
      static <T extends Comparable<? super T>>
      Collector<T,​?,​OptionalLong>
      maxIndex()
      Returns a Collector which finds the index of the maximal stream element according to the elements natural order.
      static <T> Collector<T,​?,​OptionalLong> maxIndex​(Comparator<? super T> comparator)
      Returns a Collector which finds the index of the maximal stream element according to the specified Comparator.
      static <T extends Comparable<? super T>>
      Collector<T,​?,​List<T>>
      minAll()
      Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the natural order.
      static <T> Collector<T,​?,​List<T>> minAll​(Comparator<? super T> comparator)
      Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the specified Comparator.
      static <T,​A,​D>
      Collector<T,​?,​D>
      minAll​(Comparator<? super T> comparator, Collector<T,​A,​D> downstream)
      Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the specified Comparator.
      static <T extends Comparable<? super T>,​A,​D>
      Collector<T,​?,​D>
      minAll​(Collector<T,​A,​D> downstream)
      Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the natural order.
      static <T extends Comparable<? super T>>
      Collector<T,​?,​OptionalLong>
      minIndex()
      Returns a Collector which finds the index of the minimal stream element according to the elements natural order.
      static <T> Collector<T,​?,​OptionalLong> minIndex​(Comparator<? super T> comparator)
      Returns a Collector which finds the index of the minimal stream element according to the specified Comparator.
      static <T,​R>
      Collector<T,​?,​Optional<R>>
      minMax​(Comparator<? super T> comparator, BiFunction<? super T,​? super T,​? extends R> finisher)
      Returns a Collector which finds the minimal and maximal element according to the supplied comparator, then applies finisher function to them producing the final result.
      static <T> Collector<T,​?,​Optional<T>> onlyOne()
      Returns a Collector which collects the stream element if stream contains exactly one element.
      static <T> Collector<T,​?,​Optional<T>> onlyOne​(Predicate<? super T> predicate)
      Returns a Collector which collects the stream element satisfying the predicate if there is only one such element.
      static <T,​A1,​A2,​R1,​R2,​R>
      Collector<T,​?,​R>
      pairing​(Collector<? super T,​A1,​R1> c1, Collector<? super T,​A2,​R2> c2, BiFunction<? super R1,​? super R2,​? extends R> finisher)
      Returns a Collector which aggregates the results of two supplied collectors using the supplied finisher function.
      static <T,​D,​A>
      Collector<T,​?,​Map<Boolean,​D>>
      partitioningBy​(Predicate<? super T> predicate, Collector<? super T,​A,​D> downstream)
      Returns a Collector which partitions the input elements according to a Predicate, reduces the values in each partition according to another Collector, and organizes them into a Map<Boolean, D> whose values are the result of the downstream reduction.
      static <T> Collector<T,​?,​Optional<T>> reducingWithZero​(T zero, BinaryOperator<T> op)
      Returns a Collector which performs a possibly short-circuiting reduction of its input elements under a specified BinaryOperator.
      static <T> Collector<T,​?,​T> reducingWithZero​(T zero, T identity, BinaryOperator<T> op)
      Returns a Collector which performs a possibly short-circuiting reduction of its input elements using the provided identity value and a BinaryOperator.
      static <T> Collector<T,​?,​List<T>> tail​(int n)
      Returns a Collector which collects at most specified number of the last stream elements into the List.
      static <T> Collector<T,​?,​T[]> toArray​(IntFunction<T[]> generator)
      Returns a Collector that accumulates the input elements into a new array.
      static <T> Collector<T,​?,​boolean[]> toBooleanArray​(Predicate<T> predicate)
      Returns a Collector which produces a boolean array containing the results of applying the given predicate to the input elements, in encounter order.
      static <T extends Enum<T>>
      Collector<T,​?,​EnumSet<T>>
      toEnumSet​(Class<T> enumClass)
      Returns a Collector that accumulates the input enum values into a new EnumSet.
    • Method Detail

      • toArray

        public static <T> Collector<T,​?,​T[]> toArray​(IntFunction<T[]> generator)
        Returns a Collector that accumulates the input elements into a new array. The operation performed by the returned collector is equivalent to stream.toArray(generator). This collector is mostly useful as a downstream collector.
        Type Parameters:
        T - the type of the input elements
        Parameters:
        generator - a function which produces a new array of the desired type and the provided length
        Returns:
        a Collector which collects all the input elements into an array, in encounter order
        Throws:
        NullPointerException - if generator is null.
      • toBooleanArray

        public static <T> Collector<T,​?,​boolean[]> toBooleanArray​(Predicate<T> predicate)
        Returns a Collector which produces a boolean array containing the results of applying the given predicate to the input elements, in encounter order.
        Type Parameters:
        T - the type of the input elements
        Parameters:
        predicate - a non-interfering, stateless predicate to apply to each input element. The result values of this predicate are collected to the resulting boolean array.
        Returns:
        a Collector which collects the results of the predicate function to the boolean array, in encounter order.
        Throws:
        NullPointerException - if predicate is null.
        Since:
        0.3.8
      • toEnumSet

        public static <T extends Enum<T>> Collector<T,​?,​EnumSet<T>> toEnumSet​(Class<T> enumClass)
        Returns a Collector that accumulates the input enum values into a new EnumSet.

        This method returns a short-circuiting collector: it may not process all the elements if the resulting set contains all possible enum values.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        enumClass - the class of input enum values
        Returns:
        a Collector which collects all the input elements into a EnumSet
        Throws:
        NullPointerException - if enumClass is null.
      • entriesToMap

        public static <K,​V> Collector<Map.Entry<? extends K,​? extends V>,​?,​Map<K,​V>> entriesToMap​(BinaryOperator<V> combiner)
        Returns a Collector that accumulates elements into a Map whose keys and values are taken from Map.Entry and combining them using the provided combiner function to the input elements.

        There are no guarantees on the type or serializability of the Map returned; if more control over the returned Map is required, use entriesToCustomMap(BinaryOperator, Supplier)

        Returned Map is guaranteed to be modifiable. See EntryStream.toMap().

        If the mapped keys contains duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided combiner function.

        Type Parameters:
        K - the type of the map keys
        V - the type of the map values
        Parameters:
        combiner - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)
        Returns:
        Collector which collects elements into a Map whose keys and values are taken from Map.Entry and combining them using the combiner function
        Throws:
        NullPointerException - if combiner is null.
        Since:
        0.7.3
        See Also:
        entriesToMap(), Collectors.toMap(Function, Function, BinaryOperator)
      • entriesToCustomMap

        public static <K,​V,​M extends Map<K,​V>> Collector<Map.Entry<? extends K,​? extends V>,​?,​M> entriesToCustomMap​(BinaryOperator<V> combiner,
                                                                                                                                                        Supplier<M> mapSupplier)
        Returns a Collector that accumulates elements into a result Map defined by mapSupplier function whose keys and values are taken from Map.Entry and combining them using the provided combiner function to the input elements.

        If the mapped keys contains duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided combiner function.

        Type Parameters:
        K - the type of the map keys
        V - the type of the map values
        M - the type of the resulting Map
        Parameters:
        combiner - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)
        mapSupplier - a function which returns a new, empty Map into which the results will be inserted
        Returns:
        Collector which collects elements into a Map whose keys and values are taken from Map.Entry and combining them using the combiner function
        Throws:
        NullPointerException - if combiner is null.
        NullPointerException - if mapSupplier is null.
        Since:
        0.7.3
        See Also:
        entriesToCustomMap(Supplier), Collectors.toMap(Function, Function, BinaryOperator, Supplier)
      • distinctCount

        public static <T> Collector<T,​?,​Integer> distinctCount​(Function<? super T,​?> mapper)
        Returns a Collector which counts a number of distinct values the mapper function returns for the stream elements.

        The operation performed by the returned collector is equivalent to stream.map(mapper).distinct().count(). This collector is mostly useful as a downstream collector.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        mapper - a function which classifies input elements.
        Returns:
        a collector which counts a number of distinct classes the mapper function returns for the stream elements.
        Throws:
        NullPointerException - if mapper is null.
      • distinctBy

        public static <T> Collector<T,​?,​List<T>> distinctBy​(Function<? super T,​?> mapper)
        Returns a Collector which collects into the List the input elements for which given mapper function returns distinct results.

        For ordered source the order of collected elements is preserved. If the same result is returned by mapper function for several elements, only the first element is included into the resulting list.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        The operation performed by the returned collector is equivalent to stream.distinct(mapper).toList(), but may work faster.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        mapper - a function which classifies input elements.
        Returns:
        a collector which collects distinct elements to the List.
        Throws:
        NullPointerException - if mapper is null.
        Since:
        0.3.8
      • countingInt

        public static <T> Collector<T,​?,​Integer> countingInt()
        Returns a Collector accepting elements of type T that counts the number of input elements and returns result as Integer . If no elements are present, the result is 0.
        Type Parameters:
        T - the type of the input elements
        Returns:
        a Collector that counts the input elements
        Since:
        0.3.3
        See Also:
        Collectors.counting()
      • pairing

        public static <T,​A1,​A2,​R1,​R2,​R> Collector<T,​?,​R> pairing​(Collector<? super T,​A1,​R1> c1,
                                                                                                           Collector<? super T,​A2,​R2> c2,
                                                                                                           BiFunction<? super R1,​? super R2,​? extends R> finisher)
        Returns a Collector which aggregates the results of two supplied collectors using the supplied finisher function.

        This method returns a short-circuiting collector if both downstream collectors are short-circuiting. The collection might stop when both downstream collectors report that the collection is complete.

        This collector is similar to the teeing collector available since JDK 12. The only difference is that this collector correctly combines short-circuiting collectors.

        Type Parameters:
        T - the type of the input elements
        A1 - the intermediate accumulation type of the first collector
        A2 - the intermediate accumulation type of the second collector
        R1 - the result type of the first collector
        R2 - the result type of the second collector
        R - the final result type
        Parameters:
        c1 - the first collector
        c2 - the second collector
        finisher - the function which merges two results into the single one.
        Returns:
        a Collector which aggregates the results of two supplied collectors.
        Throws:
        NullPointerException - if c1 is null, or c2 is null, or finisher is null.
      • minMax

        public static <T,​R> Collector<T,​?,​Optional<R>> minMax​(Comparator<? super T> comparator,
                                                                                BiFunction<? super T,​? super T,​? extends R> finisher)
        Returns a Collector which finds the minimal and maximal element according to the supplied comparator, then applies finisher function to them producing the final result.

        This collector produces stable result for ordered stream: if several minimal or maximal elements appear, the collector always selects the first encountered.

        If there are no input elements, the finisher method is not called and empty Optional is returned. Otherwise the finisher result is wrapped into Optional.

        Type Parameters:
        T - the type of the input elements
        R - the type of the result wrapped into Optional
        Parameters:
        comparator - comparator which is used to find minimal and maximal element
        finisher - a BiFunction which takes minimal and maximal element and produces the final result.
        Returns:
        a Collector which finds minimal and maximal elements.
        Throws:
        NullPointerException - if comparator is null, finisher is null, or finisher returns null.
      • maxAll

        public static <T,​A,​D> Collector<T,​?,​D> maxAll​(Comparator<? super T> comparator,
                                                                              Collector<? super T,​A,​D> downstream)
        Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the specified Comparator. The found elements are reduced using the specified downstream Collector.
        Type Parameters:
        T - the type of the input elements
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        comparator - a Comparator to compare the elements
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector which finds all the maximal elements.
        Throws:
        NullPointerException - if comparator is null, or downstream is null.
        See Also:
        maxAll(Comparator), maxAll(Collector), maxAll()
      • maxAll

        public static <T> Collector<T,​?,​List<T>> maxAll​(Comparator<? super T> comparator)
        Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the specified Comparator. The found elements are collected to List.
        Type Parameters:
        T - the type of the input elements
        Parameters:
        comparator - a Comparator to compare the elements
        Returns:
        a Collector which finds all the maximal elements and collects them to the List.
        Throws:
        NullPointerException - if comparator is null.
        See Also:
        maxAll(Comparator, Collector), maxAll()
      • maxAll

        public static <T extends Comparable<? super T>,​A,​D> Collector<T,​?,​D> maxAll​(Collector<T,​A,​D> downstream)
        Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the natural order. The found elements are reduced using the specified downstream Collector.
        Type Parameters:
        T - the type of the input elements
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector which finds all the maximal elements.
        Throws:
        NullPointerException - if downstream is null.
        See Also:
        maxAll(Comparator, Collector), maxAll(Comparator), maxAll()
      • maxAll

        public static <T extends Comparable<? super T>> Collector<T,​?,​List<T>> maxAll()
        Returns a Collector which finds all the elements which are equal to each other and bigger than any other element according to the natural order. The found elements are collected to List.
        Type Parameters:
        T - the type of the input elements
        Returns:
        a Collector which finds all the maximal elements and collects them to the List.
        See Also:
        maxAll(Comparator), maxAll(Collector)
      • minAll

        public static <T,​A,​D> Collector<T,​?,​D> minAll​(Comparator<? super T> comparator,
                                                                              Collector<T,​A,​D> downstream)
        Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the specified Comparator. The found elements are reduced using the specified downstream Collector.
        Type Parameters:
        T - the type of the input elements
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        comparator - a Comparator to compare the elements
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector which finds all the minimal elements.
        Throws:
        NullPointerException - if comparator is null, or downstream is null.
        See Also:
        minAll(Comparator), minAll(Collector), minAll()
      • minAll

        public static <T> Collector<T,​?,​List<T>> minAll​(Comparator<? super T> comparator)
        Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the specified Comparator. The found elements are collected to List.
        Type Parameters:
        T - the type of the input elements
        Parameters:
        comparator - a Comparator to compare the elements
        Returns:
        a Collector which finds all the minimal elements and collects them to the List.
        Throws:
        NullPointerException - if comparator is null.
        See Also:
        minAll(Comparator, Collector), minAll()
      • minAll

        public static <T extends Comparable<? super T>,​A,​D> Collector<T,​?,​D> minAll​(Collector<T,​A,​D> downstream)
        Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the natural order. The found elements are reduced using the specified downstream Collector.
        Type Parameters:
        T - the type of the input elements
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector which finds all the minimal elements.
        Throws:
        NullPointerException - if downstream is null.
        See Also:
        minAll(Comparator, Collector), minAll(Comparator), minAll()
      • minAll

        public static <T extends Comparable<? super T>> Collector<T,​?,​List<T>> minAll()
        Returns a Collector which finds all the elements which are equal to each other and smaller than any other element according to the natural order. The found elements are collected to List.
        Type Parameters:
        T - the type of the input elements
        Returns:
        a Collector which finds all the minimal elements and collects them to the List.
        See Also:
        minAll(Comparator), minAll(Collector)
      • onlyOne

        public static <T> Collector<T,​?,​Optional<T>> onlyOne()
        Returns a Collector which collects the stream element if stream contains exactly one element.

        This method returns a short-circuiting collector.

        Type Parameters:
        T - the type of the input elements
        Returns:
        a collector which returns an Optional describing the only element of the stream. For empty stream or stream containing more than one element an empty Optional is returned.
        Throws:
        NullPointerException - if the only stream element is null.
        Since:
        0.4.0
      • onlyOne

        public static <T> Collector<T,​?,​Optional<T>> onlyOne​(Predicate<? super T> predicate)
        Returns a Collector which collects the stream element satisfying the predicate if there is only one such element.

        This method returns a short-circuiting collector.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        predicate - a predicate to be applied to the stream elements
        Returns:
        a collector which returns an Optional describing the only element of the stream satisfying the predicate. If stream contains no elements satisfying the predicate, or more than one such element, an empty Optional is returned.
        Throws:
        NullPointerException - if predicate is null or the only stream element is null.
        Since:
        0.6.7
      • first

        public static <T> Collector<T,​?,​Optional<T>> first()
        Returns a Collector which collects only the first stream element if any.

        This method returns a short-circuiting collector.

        The operation performed by the returned collector is equivalent to stream.findFirst(). This collector is mostly useful as a downstream collector.

        Type Parameters:
        T - the type of the input elements
        Returns:
        a collector which returns an Optional which describes the first element of the stream. For empty stream an empty Optional is returned.
        Throws:
        NullPointerException - if the first stream element is null.
      • last

        public static <T> Collector<T,​?,​Optional<T>> last()
        Returns a Collector which collects only the last stream element if any.
        Type Parameters:
        T - the type of the input elements
        Returns:
        a collector which returns an Optional which describes the last element of the stream. For empty stream an empty Optional is returned.
        Throws:
        NullPointerException - if the last stream element is null.
      • head

        public static <T> Collector<T,​?,​List<T>> head​(int n)
        Returns a Collector which collects at most specified number of the first stream elements into the List.

        This method returns a short-circuiting collector.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        The operation performed by the returned collector is equivalent to stream.limit(n).collect(Collectors.toList()). This collector is mostly useful as a downstream collector.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        n - maximum number of stream elements to preserve
        Returns:
        a collector which returns a List containing the first n stream elements or less if the stream was shorter.
      • tail

        public static <T> Collector<T,​?,​List<T>> tail​(int n)
        Returns a Collector which collects at most specified number of the last stream elements into the List.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        When supplied n is less or equal to zero, this method returns a short-circuiting collector which ignores the input and produces an empty list.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        n - maximum number of stream elements to preserve
        Returns:
        a collector which returns a List containing the last n stream elements or less if the stream was shorter.
      • greatest

        public static <T> Collector<T,​?,​List<T>> greatest​(Comparator<? super T> comparator,
                                                                      int n)
        Returns a Collector which collects at most specified number of the greatest stream elements according to the specified Comparator into the List. The resulting List is sorted in comparator reverse order (greatest element is the first). The order of equal elements is the same as in the input stream.

        The operation performed by the returned collector is equivalent to stream.sorted(comparator.reversed()).limit(n).collect(Collectors.toList()) , but usually performed much faster if n is much less than the stream size.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        When supplied n is less or equal to zero, this method returns a short-circuiting collector which ignores the input and produces an empty list.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        comparator - the comparator to compare the elements by
        n - maximum number of stream elements to preserve
        Returns:
        a collector which returns a List containing the greatest n stream elements or less if the stream was shorter.
        Throws:
        NullPointerException - if comparator is null.
      • greatest

        public static <T extends Comparable<? super T>> Collector<T,​?,​List<T>> greatest​(int n)
        Returns a Collector which collects at most specified number of the greatest stream elements according to the natural order into the List. The resulting List is sorted in reverse order (greatest element is the first). The order of equal elements is the same as in the input stream.

        The operation performed by the returned collector is equivalent to stream.sorted(Comparator.reverseOrder()).limit(n).collect(Collectors.toList()) , but usually performed much faster if n is much less than the stream size.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        When supplied n is less or equal to zero, this method returns a short-circuiting collector which ignores the input and produces an empty list.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        n - maximum number of stream elements to preserve
        Returns:
        a collector which returns a List containing the greatest n stream elements or less if the stream was shorter.
      • least

        public static <T> Collector<T,​?,​List<T>> least​(Comparator<? super T> comparator,
                                                                   int n)
        Returns a Collector which collects at most specified number of the least stream elements according to the specified Comparator into the List. The resulting List is sorted in comparator order (least element is the first). The order of equal elements is the same as in the input stream.

        The operation performed by the returned collector is equivalent to stream.sorted(comparator).limit(n).collect(Collectors.toList()), but usually performed much faster if n is much less than the stream size.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        When supplied n is less or equal to zero, this method returns a short-circuiting collector which ignores the input and produces an empty list.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        comparator - the comparator to compare the elements by
        n - maximum number of stream elements to preserve
        Returns:
        a collector which returns a List containing the least n stream elements or less if the stream was shorter.
        Throws:
        NullPointerException - if comparator is null.
      • least

        public static <T extends Comparable<? super T>> Collector<T,​?,​List<T>> least​(int n)
        Returns a Collector which collects at most specified number of the least stream elements according to the natural order into the List. The resulting List is sorted in natural order (least element is the first). The order of equal elements is the same as in the input stream.

        The operation performed by the returned collector is equivalent to stream.sorted().limit(n).collect(Collectors.toList()), but usually performed much faster if n is much less than the stream size.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        When supplied n is less or equal to zero, this method returns a short-circuiting collector which ignores the input and produces an empty list.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        n - maximum number of stream elements to preserve
        Returns:
        a collector which returns a List containing the least n stream elements or less if the stream was shorter.
      • minIndex

        public static <T> Collector<T,​?,​OptionalLong> minIndex​(Comparator<? super T> comparator)
        Returns a Collector which finds the index of the minimal stream element according to the specified Comparator. If there are several minimal elements, the index of the first one is returned.
        Type Parameters:
        T - the type of the input elements
        Parameters:
        comparator - a Comparator to compare the elements
        Returns:
        a Collector which finds the index of the minimal element.
        Throws:
        NullPointerException - if comparator is null.
        Since:
        0.3.5
        See Also:
        minIndex()
      • minIndex

        public static <T extends Comparable<? super T>> Collector<T,​?,​OptionalLong> minIndex()
        Returns a Collector which finds the index of the minimal stream element according to the elements natural order. If there are several minimal elements, the index of the first one is returned.
        Type Parameters:
        T - the type of the input elements
        Returns:
        a Collector which finds the index of the minimal element.
        Since:
        0.3.5
        See Also:
        minIndex(Comparator)
      • maxIndex

        public static <T> Collector<T,​?,​OptionalLong> maxIndex​(Comparator<? super T> comparator)
        Returns a Collector which finds the index of the maximal stream element according to the specified Comparator. If there are several maximal elements, the index of the first one is returned.
        Type Parameters:
        T - the type of the input elements
        Parameters:
        comparator - a Comparator to compare the elements
        Returns:
        a Collector which finds the index of the maximal element.
        Throws:
        NullPointerException - if comparator is null.
        Since:
        0.3.5
        See Also:
        maxIndex()
      • maxIndex

        public static <T extends Comparable<? super T>> Collector<T,​?,​OptionalLong> maxIndex()
        Returns a Collector which finds the index of the maximal stream element according to the elements natural order. If there are several maximal elements, the index of the first one is returned.
        Type Parameters:
        T - the type of the input elements
        Returns:
        a Collector which finds the index of the maximal element.
        Since:
        0.3.5
        See Also:
        maxIndex(Comparator)
      • groupingByEnum

        public static <T,​K extends Enum<K>,​A,​D> Collector<T,​?,​EnumMap<K,​D>> groupingByEnum​(Class<K> enumClass,
                                                                                                                               Function<? super T,​K> classifier,
                                                                                                                               Collector<? super T,​A,​D> downstream)
        Returns a Collector implementing a cascaded "group by" operation on input elements of type T, for classification function which maps input elements to the enum values. The downstream reduction for repeating keys is performed using the specified downstream Collector.

        Unlike the Collectors.groupingBy(Function, Collector) collector this collector produces an EnumMap which contains all possible keys including keys which were never returned by the classification function. These keys are mapped to the default collector value which is equivalent to collecting an empty stream with the same collector.

        This method returns a short-circuiting collector if the downstream collector is short-circuiting. The collection might stop when for every possible enum key the downstream collection is known to be finished.

        Type Parameters:
        T - the type of the input elements
        K - the type of the enum values returned by the classifier
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        enumClass - the class of enum values returned by the classifier
        classifier - a classifier function mapping input elements to enum values
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector implementing the cascaded group-by operation
        Throws:
        NullPointerException - if enumClass is null, classifier is null, or downstream is null.
        Since:
        0.3.7
        See Also:
        Collectors.groupingBy(Function, Collector), groupingBy(Function, Set, Supplier, Collector)
      • groupingBy

        public static <T,​K,​D,​A> Collector<T,​?,​Map<K,​D>> groupingBy​(Function<? super T,​? extends K> classifier,
                                                                                                       Set<K> domain,
                                                                                                       Collector<? super T,​A,​D> downstream)
        Returns a Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned.

        The main difference of this collector from Collectors.groupingBy(Function, Collector) is that it accepts additional domain parameter which is the Set of all possible map keys. If the mapper function produces the key out of domain, an IllegalStateException will occur. If the mapper function does not produce some of domain keys at all, they are also added to the result. These keys are mapped to the default collector value which is equivalent to collecting an empty stream with the same collector.

        This method returns a short-circuiting collector if the downstream collector is short-circuiting. The collection might stop when for every possible key from the domain the downstream collection is known to be finished.

        Type Parameters:
        T - the type of the input elements
        K - the type of the keys
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        classifier - a classifier function mapping input elements to keys
        domain - a domain of all possible key values
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector implementing the cascaded group-by operation with given domain
        Throws:
        NullPointerException - if classifier is null, domain is null, or downstream is null.
        Since:
        0.4.0
        See Also:
        groupingBy(Function, Set, Supplier, Collector), groupingByEnum(Class, Function, Collector)
      • groupingBy

        public static <T,​K,​D,​A,​M extends Map<K,​D>> Collector<T,​?,​M> groupingBy​(Function<? super T,​? extends K> classifier,
                                                                                                                         Set<K> domain,
                                                                                                                         Supplier<M> mapFactory,
                                                                                                                         Collector<? super T,​A,​D> downstream)
        Returns a Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector. The Map produced by the Collector is created with the supplied factory function.

        The main difference of this collector from Collectors.groupingBy(Function, Supplier, Collector) is that it accepts additional domain parameter which is the Set of all possible map keys. If the mapper function produces the key out of domain, an IllegalStateException will occur. If the mapper function does not produce some of domain keys at all, they are also added to the result. These keys are mapped to the default collector value which is equivalent to collecting an empty stream with the same collector.

        This method returns a short-circuiting collector if the downstream collector is short-circuiting. The collection might stop when for every possible key from the domain the downstream collection is known to be finished.

        Type Parameters:
        T - the type of the input elements
        K - the type of the keys
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        M - the type of the resulting Map
        Parameters:
        classifier - a classifier function mapping input elements to keys
        domain - a domain of all possible key values
        downstream - a Collector implementing the downstream reduction
        mapFactory - a function which, when called, produces a new empty Map of the desired type
        Returns:
        a Collector implementing the cascaded group-by operation with given domain
        Throws:
        NullPointerException - if classifier is null, domain is null, mapFactory is null, or downstream is null.
        Since:
        0.4.0
        See Also:
        groupingBy(Function, Set, Collector), groupingByEnum(Class, Function, Collector)
      • intersecting

        public static <T,​S extends Collection<T>> Collector<S,​?,​Set<T>> intersecting()
        Returns a Collector which collects the intersection of the input collections into the newly-created Set.

        The returned collector produces an empty set if the input is empty or intersection of the input collections is empty.

        There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned.

        This method returns a short-circuiting collector: it may not process all the elements if the resulting intersection is empty.

        Type Parameters:
        T - the type of the elements in the input collections
        S - the type of the input collections
        Returns:
        a Collector which finds all the minimal elements and collects them to the List.
        Since:
        0.4.0
      • collectingAndThen

        public static <T,​A,​R,​RR> Collector<T,​A,​RR> collectingAndThen​(Collector<T,​A,​R> downstream,
                                                                                                   Function<R,​RR> finisher)
        Adapts a Collector to perform an additional finishing transformation.

        Unlike Collectors.collectingAndThen(Collector, Function) this method returns a short-circuiting collector if the downstream collector is short-circuiting.

        Type Parameters:
        T - the type of the input elements
        A - intermediate accumulation type of the downstream collector
        R - result type of the downstream collector
        RR - result type of the resulting collector
        Parameters:
        downstream - a collector
        finisher - a function to be applied to the final result of the downstream collector
        Returns:
        a collector which performs the action of the downstream collector, followed by an additional finishing step
        Throws:
        NullPointerException - if downstream is null, or finisher is null.
        Since:
        0.4.0
        See Also:
        Collectors.collectingAndThen(Collector, Function)
      • partitioningBy

        public static <T,​D,​A> Collector<T,​?,​Map<Boolean,​D>> partitioningBy​(Predicate<? super T> predicate,
                                                                                                         Collector<? super T,​A,​D> downstream)
        Returns a Collector which partitions the input elements according to a Predicate, reduces the values in each partition according to another Collector, and organizes them into a Map<Boolean, D> whose values are the result of the downstream reduction.

        Unlike Collectors.partitioningBy(Predicate, Collector) this method returns a short-circuiting collector if the downstream collector is short-circuiting.

        Type Parameters:
        T - the type of the input elements
        A - the intermediate accumulation type of the downstream collector
        D - the result type of the downstream reduction
        Parameters:
        predicate - a predicate used for classifying input elements
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector implementing the cascaded partitioning operation
        Throws:
        NullPointerException - if predicate is null, or downstream is null.
        Since:
        0.4.0
        See Also:
        Collectors.partitioningBy(Predicate, Collector)
      • mapping

        public static <T,​U,​A,​R> Collector<T,​?,​R> mapping​(Function<? super T,​? extends U> mapper,
                                                                                       Collector<? super U,​A,​R> downstream)
        Adapts a Collector accepting elements of type U to one accepting elements of type T by applying a mapping function to each input element before accumulation.

        Unlike Collectors.mapping(Function, Collector) this method returns a short-circuiting collector if the downstream collector is short-circuiting.

        Type Parameters:
        T - the type of the input elements
        U - type of elements accepted by downstream collector
        A - intermediate accumulation type of the downstream collector
        R - result type of collector
        Parameters:
        mapper - a function to be applied to the input elements
        downstream - a collector which will accept mapped values
        Returns:
        a collector which applies the mapping function to the input elements and provides the mapped results to the downstream collector
        Throws:
        NullPointerException - if mapper is null, or downstream is null.
        Since:
        0.4.0
        See Also:
        Collectors.mapping(Function, Collector)
      • mapping

        public static <T,​U> Collector<T,​?,​List<U>> mapping​(Function<? super T,​? extends U> mapper)
        Returns a collector which collects input elements to the new List transforming them with the supplied function beforehand.

        This method behaves like Collectors.mapping(mapper, Collectors.toList()).

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        Type Parameters:
        T - the type of the input elements
        U - the resulting type of the mapper function
        Parameters:
        mapper - a function to be applied to the input elements
        Returns:
        a collector which applies the mapping function to the input elements and collects the mapped results to the List
        Throws:
        NullPointerException - if mapper is null.
        Since:
        0.6.0
        See Also:
        mapping(Function, Collector)
      • flatMapping

        public static <T,​U,​A,​R> Collector<T,​?,​R> flatMapping​(Function<? super T,​? extends Stream<? extends U>> mapper,
                                                                                           Collector<? super U,​A,​R> downstream)
        Adapts a Collector accepting elements of type U to one accepting elements of type T by applying a flat mapping function to each input element before accumulation. The flat mapping function maps an input element to a stream covering zero or more output elements that are then accumulated downstream. Each mapped stream is closed after its contents have been placed downstream. (If a mapped stream is null an empty stream is used, instead.)

        This method is similar to Collectors.flatMapping method which appears in JDK 9. However when downstream collector is short-circuiting , this method will also return a short-circuiting collector.

        Type Parameters:
        T - the type of the input elements
        U - type of elements accepted by downstream collector
        A - intermediate accumulation type of the downstream collector
        R - result type of collector
        Parameters:
        mapper - a function to be applied to the input elements, which returns a stream of results
        downstream - a collector which will receive the elements of the stream returned by mapper
        Returns:
        a collector which applies the mapping function to the input elements and provides the flat mapped results to the downstream collector
        Throws:
        NullPointerException - if mapper is null, or downstream is null.
        Since:
        0.4.1
      • flatMapping

        public static <T,​U> Collector<T,​?,​List<U>> flatMapping​(Function<? super T,​? extends Stream<? extends U>> mapper)
        Returns a collector which launches a flat mapping function for each input element and collects the elements of the resulting streams to the flat List. Each mapped stream is closed after its contents have been placed downstream. (If a mapped stream is null an empty stream is used, instead.)

        This method behaves like flatMapping(mapper, Collectors.toList()) .

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        Type Parameters:
        T - the type of the input elements
        U - type of the resulting elements
        Parameters:
        mapper - a function to be applied to the input elements, which returns a stream of results
        Returns:
        a collector which applies the mapping function to the input elements and collects the flat mapped results to the List
        Throws:
        NullPointerException - if mapper is null.
        Since:
        0.6.0
      • filtering

        public static <T,​A,​R> Collector<T,​?,​R> filtering​(Predicate<? super T> predicate,
                                                                                 Collector<T,​A,​R> downstream)
        Returns a Collector which passes only those elements to the specified downstream collector which match given predicate.

        This method returns a short-circuiting collector if downstream collector is short-circuiting.

        The operation performed by the returned collector is equivalent to stream.filter(predicate).collect(downstream). This collector is mostly useful as a downstream collector in cascaded operation involving pairing(Collector, Collector, BiFunction) collector.

        This method is similar to Collectors.filtering method which appears in JDK 9. However when downstream collector is short-circuiting , this method will also return a short-circuiting collector.

        Type Parameters:
        T - the type of the input elements
        A - intermediate accumulation type of the downstream collector
        R - result type of collector
        Parameters:
        predicate - a filter function to be applied to the input elements
        downstream - a collector which will accept filtered values
        Returns:
        a collector which applies the predicate to the input elements and provides the elements for which predicate returned true to the downstream collector
        Throws:
        NullPointerException - if predicate is null, or downstream is null.
        Since:
        0.4.0
        See Also:
        pairing(Collector, Collector, BiFunction)
      • filtering

        public static <T> Collector<T,​?,​List<T>> filtering​(Predicate<? super T> predicate)
        Returns a Collector which filters input elements by the supplied predicate, collecting them to the list.

        This method behaves like filtering(predicate, Collectors.toList()).

        There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        predicate - a filter function to be applied to the input elements
        Returns:
        a collector which applies the predicate to the input elements and collects the elements for which predicate returned true to the List
        Throws:
        NullPointerException - if predicate is null.
        Since:
        0.6.0
        See Also:
        filtering(Predicate, Collector)
      • andingInt

        public static <T> Collector<T,​?,​OptionalInt> andingInt​(ToIntFunction<T> mapper)
        Returns a Collector which performs the bitwise-and operation of a integer-valued function applied to the input elements. If no elements are present, the result is empty OptionalInt.

        This method returns a short-circuiting collector: it may not process all the elements if the result is zero.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        mapper - a function extracting the property to be processed
        Returns:
        a Collector that produces the bitwise-and operation of a derived property
        Throws:
        NullPointerException - if mapper is null.
        Since:
        0.4.0
      • andingLong

        public static <T> Collector<T,​?,​OptionalLong> andingLong​(ToLongFunction<T> mapper)
        Returns a Collector which performs the bitwise-and operation of a long-valued function applied to the input elements. If no elements are present, the result is empty OptionalLong.

        This method returns a short-circuiting collector: it may not process all the elements if the result is zero.

        Type Parameters:
        T - the type of the input elements
        Parameters:
        mapper - a function extracting the property to be processed
        Returns:
        a Collector that produces the bitwise-and operation of a derived property
        Throws:
        NullPointerException - if mapper is null.
        Since:
        0.4.0
      • commonPrefix

        public static Collector<CharSequence,​?,​String> commonPrefix()
        Returns a Collector which computes a common prefix of input CharSequence objects returning the result as String. For empty input the empty String is returned.

        The returned Collector handles specially Unicode surrogate pairs: the returned prefix may end with Unicode high-surrogate code unit only if it's not succeeded by Unicode low-surrogate code unit in any of the input sequences. Normally the ending high-surrogate code unit is removed from the prefix.

        This method returns a short-circuiting collector: it may not process all the elements if the common prefix is empty.

        Returns:
        a Collector which computes a common prefix.
        Since:
        0.5.0
      • commonSuffix

        public static Collector<CharSequence,​?,​String> commonSuffix()
        Returns a Collector which computes a common suffix of input CharSequence objects returning the result as String. For empty input the empty String is returned.

        The returned Collector handles specially Unicode surrogate pairs: the returned suffix may start with Unicode low-surrogate code unit only if it's not preceded by Unicode high-surrogate code unit in any of the input sequences. Normally the starting low-surrogate code unit is removed from the suffix.

        This method returns a short-circuiting collector: it may not process all the elements if the common suffix is empty.

        Returns:
        a Collector which computes a common suffix.
        Since:
        0.5.0
      • dominators

        public static <T> Collector<T,​?,​List<T>> dominators​(BiPredicate<? super T,​? super T> isDominator)
        Returns a collector which collects input elements into List removing the elements following their dominator element. The dominator elements are defined according to given isDominator BiPredicate. The isDominator relation must be transitive (if A dominates over B and B dominates over C, then A also dominates over C).

        This operation is similar to streamEx.collapse(isDominator).toList(). The important difference is that in this method BiPredicate accepts not the adjacent stream elements, but the leftmost element of the series (current dominator) and the current element.

        For example, consider the stream of numbers:

        
         StreamEx<Integer> stream = StreamEx.of(1, 5, 3, 4, 2, 7);
         

        Using stream.collapse((a, b) -> a >= b).toList() you will get the numbers which are bigger than their immediate predecessor ( [1, 5, 4, 7]), because (3, 4) pair is not collapsed. However using stream.collect(dominators((a, b) -> a >= b)) you will get the numbers which are bigger than any predecessor ([1, 5, 7]) as 5 is the dominator element for the subsequent 3, 4 and 2.

        Type Parameters:
        T - type of the input elements.
        Parameters:
        isDominator - a non-interfering, stateless, transitive BiPredicate which returns true if the first argument is the dominator for the second argument.
        Returns:
        a collector which collects input element into List leaving only dominator elements.
        Throws:
        NullPointerException - if isDominator is null.
        Since:
        0.5.1
        See Also:
        StreamEx.collapse(BiPredicate)
      • ifAllMatch

        public static <T,​A,​R> Collector<T,​?,​Optional<R>> ifAllMatch​(Predicate<T> predicate,
                                                                                            Collector<T,​A,​R> downstream)
        Returns a Collector which performs downstream reduction if all elements satisfy the Predicate. The result is described as an Optional<R>.

        The resulting collector returns an empty optional if at least one input element does not satisfy the predicate. Otherwise it returns an optional which contains the result of the downstream collector.

        This method returns a short-circuiting collector: it may not process all the elements if some of items don't satisfy the predicate or if downstream collector is a short-circuiting collector.

        It's guaranteed that the downstream collector is not called for elements which don't satisfy the predicate.

        Type Parameters:
        T - the type of input elements
        A - intermediate accumulation type of the downstream collector
        R - result type of the downstream collector
        Parameters:
        predicate - a non-interfering, stateless predicate to checks whether collector should proceed with element
        downstream - a Collector implementing the downstream reduction
        Returns:
        a Collector witch performs downstream reduction if all elements satisfy the predicate
        Throws:
        NullPointerException - if mapper is null.
        Since:
        0.6.3
        See Also:
        Stream.allMatch(Predicate), AbstractStreamEx.dropWhile(Predicate), AbstractStreamEx.takeWhile(Predicate)