Class MoreCollectors
- Object
-
- 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 aCollector
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 aCollector
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 aCollector
to perform an additional finishing transformation.static Collector<CharSequence,?,String>
commonPrefix()
Returns aCollector
which computes a common prefix of inputCharSequence
objects returning the result asString
.static Collector<CharSequence,?,String>
commonSuffix()
Returns aCollector
which computes a common suffix of inputCharSequence
objects returning the result asString
.static <T> Collector<T,?,Integer>
countingInt()
Returns aCollector
accepting elements of typeT
that counts the number of input elements and returns result asInteger
.static <T> Collector<T,?,List<T>>
distinctBy(Function<? super T,?> mapper)
Returns aCollector
which collects into theList
the input elements for which given mapper function returns distinct results.static <T> Collector<T,?,Integer>
distinctCount(Function<? super T,?> mapper)
Returns aCollector
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 intoList
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 aCollector
that accumulates elements into a resultMap
defined bymapSupplier
function whose keys and values are taken fromMap.Entry
and combining them using the providedcombiner
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 aCollector
that accumulates elements into a resultMap
defined bymapSupplier
function whose keys and values are taken fromMap.Entry
.static <K,V>
Collector<Map.Entry<? extends K,? extends V>,?,Map<K,V>>entriesToMap()
Returns aCollector
that accumulates elements into aMap
whose keys and values are taken fromMap.Entry
.static <K,V>
Collector<Map.Entry<? extends K,? extends V>,?,Map<K,V>>entriesToMap(BinaryOperator<V> combiner)
Returns aCollector
that accumulates elements into aMap
whose keys and values are taken fromMap.Entry
and combining them using the providedcombiner
function to the input elements.static <T> Collector<T,?,List<T>>
filtering(Predicate<? super T> predicate)
Returns aCollector
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 aCollector
which passes only those elements to the specified downstream collector which match given predicate.static <T> Collector<T,?,Optional<T>>
first()
Returns aCollector
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 flatList
.static <T,U,A,R>
Collector<T,?,R>flatMapping(Function<? super T,? extends Stream<? extends U>> mapper, Collector<? super U,A,R> downstream)
Adapts aCollector
accepting elements of typeU
to one accepting elements of typeT
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 aCollector
which collects at most specified number of the greatest stream elements according to the natural order into theList
.static <T> Collector<T,?,List<T>>
greatest(Comparator<? super T> comparator, int n)
Returns aCollector
which collects at most specified number of the greatest stream elements according to the specifiedComparator
into theList
.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 aCollector
implementing a cascaded "group by" operation on input elements of typeT
, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector
.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 aCollector
implementing a cascaded "group by" operation on input elements of typeT
, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector
.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 aCollector
implementing a cascaded "group by" operation on input elements of typeT
, for classification function which maps input elements to the enum values.static <T> Collector<T,?,List<T>>
head(int n)
Returns aCollector
which collects at most specified number of the first stream elements into theList
.static <T,A,R>
Collector<T,?,Optional<R>>ifAllMatch(Predicate<T> predicate, Collector<T,A,R> downstream)
Returns aCollector
which performs downstream reduction if all elements satisfy thePredicate
.static <T,S extends Collection<T>>
Collector<S,?,Set<T>>intersecting()
Returns aCollector
which collects the intersection of the input collections into the newly-createdSet
.static <T> Collector<T,?,Optional<T>>
last()
Returns aCollector
which collects only the last stream element if any.static <T extends Comparable<? super T>>
Collector<T,?,List<T>>least(int n)
Returns aCollector
which collects at most specified number of the least stream elements according to the natural order into theList
.static <T> Collector<T,?,List<T>>
least(Comparator<? super T> comparator, int n)
Returns aCollector
which collects at most specified number of the least stream elements according to the specifiedComparator
into theList
.static <T,U>
Collector<T,?,List<U>>mapping(Function<? super T,? extends U> mapper)
Returns a collector which collects input elements to the newList
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 aCollector
accepting elements of typeU
to one accepting elements of typeT
by applying a mapping function to each input element before accumulation.static <T extends Comparable<? super T>>
Collector<T,?,List<T>>maxAll()
Returns aCollector
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 aCollector
which finds all the elements which are equal to each other and bigger than any other element according to the specifiedComparator
.static <T,A,D>
Collector<T,?,D>maxAll(Comparator<? super T> comparator, Collector<? super T,A,D> downstream)
Returns aCollector
which finds all the elements which are equal to each other and bigger than any other element according to the specifiedComparator
.static <T extends Comparable<? super T>,A,D>
Collector<T,?,D>maxAll(Collector<T,A,D> downstream)
Returns aCollector
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 aCollector
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 aCollector
which finds the index of the maximal stream element according to the specifiedComparator
.static <T extends Comparable<? super T>>
Collector<T,?,List<T>>minAll()
Returns aCollector
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 aCollector
which finds all the elements which are equal to each other and smaller than any other element according to the specifiedComparator
.static <T,A,D>
Collector<T,?,D>minAll(Comparator<? super T> comparator, Collector<T,A,D> downstream)
Returns aCollector
which finds all the elements which are equal to each other and smaller than any other element according to the specifiedComparator
.static <T extends Comparable<? super T>,A,D>
Collector<T,?,D>minAll(Collector<T,A,D> downstream)
Returns aCollector
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 aCollector
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 aCollector
which finds the index of the minimal stream element according to the specifiedComparator
.static <T,R>
Collector<T,?,Optional<R>>minMax(Comparator<? super T> comparator, BiFunction<? super T,? super T,? extends R> finisher)
Returns aCollector
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 aCollector
which collects the stream element if stream contains exactly one element.static <T> Collector<T,?,Optional<T>>
onlyOne(Predicate<? super T> predicate)
Returns aCollector
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 aCollector
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 aCollector
which partitions the input elements according to aPredicate
, reduces the values in each partition according to anotherCollector
, and organizes them into aMap<Boolean, D>
whose values are the result of the downstream reduction.static <T> Collector<T,?,Optional<T>>
reducingWithZero(T zero, BinaryOperator<T> op)
Returns aCollector
which performs a possibly short-circuiting reduction of its input elements under a specifiedBinaryOperator
.static <T> Collector<T,?,T>
reducingWithZero(T zero, T identity, BinaryOperator<T> op)
Returns aCollector
which performs a possibly short-circuiting reduction of its input elements using the provided identity value and aBinaryOperator
.static <T> Collector<T,?,List<T>>
tail(int n)
Returns aCollector
which collects at most specified number of the last stream elements into theList
.static <T> Collector<T,?,T[]>
toArray(IntFunction<T[]> generator)
Returns aCollector
that accumulates the input elements into a new array.static <T> Collector<T,?,boolean[]>
toBooleanArray(Predicate<T> predicate)
Returns aCollector
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 aCollector
that accumulates the input enum values into a newEnumSet
.
-
-
-
Method Detail
-
toArray
public static <T> Collector<T,?,T[]> toArray(IntFunction<T[]> generator)
Returns aCollector
that accumulates the input elements into a new array. The operation performed by the returned collector is equivalent tostream.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 aCollector
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 aCollector
that accumulates the input enum values into a newEnumSet
.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 aEnumSet
- Throws:
NullPointerException
- if enumClass is null.
-
entriesToMap
public static <K,V> Collector<Map.Entry<? extends K,? extends V>,?,Map<K,V>> entriesToMap()
Returns aCollector
that accumulates elements into aMap
whose keys and values are taken fromMap.Entry
.There are no guarantees on the type or serializability of the
Map
returned; if more control over the returnedMap
is required, useentriesToCustomMap(Supplier)
Returned
Map
is guaranteed to be modifiable. SeeEntryStream.toMap()
.- Type Parameters:
K
- the type of the map keysV
- the type of the map values- Returns:
Collector
which collects elements into aMap
whose keys and values are taken fromMap.Entry
- Throws:
IllegalStateException
- if this stream contains duplicate keys (according toObject.equals(Object)
).- Since:
- 0.7.3
- See Also:
entriesToMap(BinaryOperator)
,Collectors.toMap(Function, Function)
-
entriesToMap
public static <K,V> Collector<Map.Entry<? extends K,? extends V>,?,Map<K,V>> entriesToMap(BinaryOperator<V> combiner)
Returns aCollector
that accumulates elements into aMap
whose keys and values are taken fromMap.Entry
and combining them using the providedcombiner
function to the input elements.There are no guarantees on the type or serializability of the
Map
returned; if more control over the returnedMap
is required, useentriesToCustomMap(BinaryOperator, Supplier)
Returned
Map
is guaranteed to be modifiable. SeeEntryStream.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 providedcombiner
function.- Type Parameters:
K
- the type of the map keysV
- the type of the map values- Parameters:
combiner
- a merge function, used to resolve collisions between values associated with the same key, as supplied toMap.merge(Object, Object, BiFunction)
- Returns:
Collector
which collects elements into aMap
whose keys and values are taken fromMap.Entry
and combining them using thecombiner
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(Supplier<M> mapSupplier)
Returns aCollector
that accumulates elements into a resultMap
defined bymapSupplier
function whose keys and values are taken fromMap.Entry
.- Type Parameters:
K
- the type of the map keysV
- the type of the map valuesM
- the type of the resultingMap
- Parameters:
mapSupplier
- a function which returns a new, emptyMap
into which the results will be inserted- Returns:
Collector
which collects elements into aMap
defined bymapSupplier
function whose keys and values are taken fromMap.Entry
- Throws:
IllegalStateException
- if this stream contains duplicate keys (according toObject.equals(Object)
).NullPointerException
- if mapSupplier is null.NullPointerException
- if entry value is null.- Since:
- 0.7.3
- See Also:
entriesToCustomMap(BinaryOperator, Supplier)
,Collector.of(Supplier, BiConsumer, BinaryOperator, Collector.Characteristics...)
-
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 aCollector
that accumulates elements into a resultMap
defined bymapSupplier
function whose keys and values are taken fromMap.Entry
and combining them using the providedcombiner
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 providedcombiner
function.- Type Parameters:
K
- the type of the map keysV
- the type of the map valuesM
- the type of the resultingMap
- Parameters:
combiner
- a merge function, used to resolve collisions between values associated with the same key, as supplied toMap.merge(Object, Object, BiFunction)
mapSupplier
- a function which returns a new, emptyMap
into which the results will be inserted- Returns:
Collector
which collects elements into aMap
whose keys and values are taken fromMap.Entry
and combining them using thecombiner
function- Throws:
NullPointerException
- ifcombiner
is null.NullPointerException
- ifmapSupplier
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 aCollector
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 aCollector
which collects into theList
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 aCollector
accepting elements of typeT
that counts the number of input elements and returns result asInteger
. 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 aCollector
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 elementsA1
- the intermediate accumulation type of the first collectorA2
- the intermediate accumulation type of the second collectorR1
- the result type of the first collectorR2
- the result type of the second collectorR
- the final result type- Parameters:
c1
- the first collectorc2
- the second collectorfinisher
- 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 aCollector
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 intoOptional
.- Type Parameters:
T
- the type of the input elementsR
- the type of the result wrapped intoOptional
- Parameters:
comparator
- comparator which is used to find minimal and maximal elementfinisher
- aBiFunction
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 aCollector
which finds all the elements which are equal to each other and bigger than any other element according to the specifiedComparator
. The found elements are reduced using the specified downstreamCollector
.- Type Parameters:
T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
comparator
- aComparator
to compare the elementsdownstream
- aCollector
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 aCollector
which finds all the elements which are equal to each other and bigger than any other element according to the specifiedComparator
. The found elements are collected toList
.- Type Parameters:
T
- the type of the input elements- Parameters:
comparator
- aComparator
to compare the elements- Returns:
- a
Collector
which finds all the maximal elements and collects them to theList
. - 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 aCollector
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 downstreamCollector
.- Type Parameters:
T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
downstream
- aCollector
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 aCollector
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 toList
.- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
which finds all the maximal elements and collects them to theList
. - 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 aCollector
which finds all the elements which are equal to each other and smaller than any other element according to the specifiedComparator
. The found elements are reduced using the specified downstreamCollector
.- Type Parameters:
T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
comparator
- aComparator
to compare the elementsdownstream
- aCollector
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 aCollector
which finds all the elements which are equal to each other and smaller than any other element according to the specifiedComparator
. The found elements are collected toList
.- Type Parameters:
T
- the type of the input elements- Parameters:
comparator
- aComparator
to compare the elements- Returns:
- a
Collector
which finds all the minimal elements and collects them to theList
. - 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 aCollector
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 downstreamCollector
.- Type Parameters:
T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
downstream
- aCollector
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 aCollector
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 toList
.- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
which finds all the minimal elements and collects them to theList
. - See Also:
minAll(Comparator)
,minAll(Collector)
-
onlyOne
public static <T> Collector<T,?,Optional<T>> onlyOne()
Returns aCollector
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 emptyOptional
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 aCollector
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 emptyOptional
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 aCollector
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 emptyOptional
is returned. - Throws:
NullPointerException
- if the first stream element is null.
-
last
public static <T> Collector<T,?,Optional<T>> last()
Returns aCollector
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 emptyOptional
is returned. - Throws:
NullPointerException
- if the last stream element is null.
-
head
public static <T> Collector<T,?,List<T>> head(int n)
Returns aCollector
which collects at most specified number of the first stream elements into theList
.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 aCollector
which collects at most specified number of the last stream elements into theList
.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 aCollector
which collects at most specified number of the greatest stream elements according to the specifiedComparator
into theList
. The resultingList
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 ifn
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 byn
- 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 aCollector
which collects at most specified number of the greatest stream elements according to the natural order into theList
. The resultingList
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 ifn
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 aCollector
which collects at most specified number of the least stream elements according to the specifiedComparator
into theList
. The resultingList
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 ifn
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 byn
- 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 aCollector
which collects at most specified number of the least stream elements according to the natural order into theList
. The resultingList
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 ifn
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 aCollector
which finds the index of the minimal stream element according to the specifiedComparator
. 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
- aComparator
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 aCollector
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 aCollector
which finds the index of the maximal stream element according to the specifiedComparator
. 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
- aComparator
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 aCollector
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 aCollector
implementing a cascaded "group by" operation on input elements of typeT
, for classification function which maps input elements to the enum values. The downstream reduction for repeating keys is performed using the specified downstreamCollector
.Unlike the
Collectors.groupingBy(Function, Collector)
collector this collector produces anEnumMap
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 elementsK
- the type of the enum values returned by the classifierA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
enumClass
- the class of enum values returned by the classifierclassifier
- a classifier function mapping input elements to enum valuesdownstream
- aCollector
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 aCollector
implementing a cascaded "group by" operation on input elements of typeT
, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector
.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 theSet
of all possible map keys. If the mapper function produces the key out of domain, anIllegalStateException
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 elementsK
- the type of the keysA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
classifier
- a classifier function mapping input elements to keysdomain
- a domain of all possible key valuesdownstream
- aCollector
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 aCollector
implementing a cascaded "group by" operation on input elements of typeT
, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector
. TheMap
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 theSet
of all possible map keys. If the mapper function produces the key out of domain, anIllegalStateException
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 elementsK
- the type of the keysA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductionM
- the type of the resultingMap
- Parameters:
classifier
- a classifier function mapping input elements to keysdomain
- a domain of all possible key valuesdownstream
- aCollector
implementing the downstream reductionmapFactory
- a function which, when called, produces a new emptyMap
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 aCollector
which collects the intersection of the input collections into the newly-createdSet
.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 collectionsS
- the type of the input collections- Returns:
- a
Collector
which finds all the minimal elements and collects them to theList
. - 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 aCollector
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 elementsA
- intermediate accumulation type of the downstream collectorR
- result type of the downstream collectorRR
- result type of the resulting collector- Parameters:
downstream
- a collectorfinisher
- 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 aCollector
which partitions the input elements according to aPredicate
, reduces the values in each partition according to anotherCollector
, and organizes them into aMap<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 elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reduction- Parameters:
predicate
- a predicate used for classifying input elementsdownstream
- aCollector
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 aCollector
accepting elements of typeU
to one accepting elements of typeT
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 elementsU
- type of elements accepted by downstream collectorA
- intermediate accumulation type of the downstream collectorR
- result type of collector- Parameters:
mapper
- a function to be applied to the input elementsdownstream
- 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 newList
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 elementsU
- 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 aCollector
accepting elements of typeU
to one accepting elements of typeT
by applying a flat mapping function to each input element before accumulation. The flat mapping function maps an input element to astream
covering zero or more output elements that are then accumulated downstream. Each mapped stream isclosed
after its contents have been placed downstream. (If a mapped stream isnull
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 elementsU
- type of elements accepted by downstream collectorA
- intermediate accumulation type of the downstream collectorR
- result type of collector- Parameters:
mapper
- a function to be applied to the input elements, which returns a stream of resultsdownstream
- 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 flatList
. Each mapped stream isclosed
after its contents have been placed downstream. (If a mapped stream isnull
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 elementsU
- 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 aCollector
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 involvingpairing(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 elementsA
- intermediate accumulation type of the downstream collectorR
- result type of collector- Parameters:
predicate
- a filter function to be applied to the input elementsdownstream
- 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 aCollector
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 aCollector
which performs the bitwise-and operation of a integer-valued function applied to the input elements. If no elements are present, the result is emptyOptionalInt
.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 aCollector
which performs the bitwise-and operation of a long-valued function applied to the input elements. If no elements are present, the result is emptyOptionalLong
.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 aCollector
which computes a common prefix of inputCharSequence
objects returning the result asString
. For empty input the emptyString
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 aCollector
which computes a common suffix of inputCharSequence
objects returning the result asString
. For empty input the emptyString
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 intoList
removing the elements following their dominator element. The dominator elements are defined according to given isDominatorBiPredicate
. 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 methodBiPredicate
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 usingstream.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, transitiveBiPredicate
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 aCollector
which performs downstream reduction if all elements satisfy thePredicate
. The result is described as anOptional<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 elementsA
- intermediate accumulation type of the downstream collectorR
- result type of the downstream collector- Parameters:
predicate
- a non-interfering, stateless predicate to checks whether collector should proceed with elementdownstream
- aCollector
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)
-
reducingWithZero
public static <T> Collector<T,?,Optional<T>> reducingWithZero(T zero, BinaryOperator<T> op)
Returns aCollector
which performs a possibly short-circuiting reduction of its input elements under a specifiedBinaryOperator
. The result is described as anOptional<T>
.This collector behaves like
Collectors.reducing(BinaryOperator)
. However, it additionally accepts a zero element (also known as absorbing element). When zero element is passed to the accumulator then the result must be zero as well. So the collector takes the advantage of this and may short-circuit if zero is reached during the collection.This method returns a short-circuiting collector: it may not process all the elements if the result of reduction is equal to zero.
This collector is mostly useful as a downstream collector. To perform simple short-circuiting reduction, use
AbstractStreamEx.reduceWithZero(Object, BinaryOperator)
instead.- Type Parameters:
T
- the type of input elements- Parameters:
zero
- zero elementop
- an associative , non-interfering , stateless function to combine two elements into one.- Returns:
- a collector which returns an
Optional
describing the reduction result. For empty stream an emptyOptional
is returned. - Throws:
NullPointerException
- if op is null or the result of reduction is null- Since:
- 0.7.3
- See Also:
reducingWithZero(Object, Object, BinaryOperator)
,AbstractStreamEx.reduceWithZero(Object, BinaryOperator)
,Collectors.reducing(BinaryOperator)
-
reducingWithZero
public static <T> Collector<T,?,T> reducingWithZero(T zero, T identity, BinaryOperator<T> op)
Returns aCollector
which performs a possibly short-circuiting reduction of its input elements using the provided identity value and aBinaryOperator
.This collector behaves like
Collectors.reducing(Object, BinaryOperator)
. However, it additionally accepts a zero element (also known as absorbing element). When zero element is passed to the accumulator then the result must be zero as well. So the collector takes the advantage of this and may short-circuit if zero is reached during the collection.This method returns a short-circuiting collector: it may not process all the elements if the result of reduction is equal to zero.
This collector is mostly useful as a downstream collector. To perform simple short-circuiting reduction, use
AbstractStreamEx.reduceWithZero(Object, BinaryOperator)
instead.- Type Parameters:
T
- the type of input elements- Parameters:
zero
- zero elementidentity
- an identity element. For allt
,op.apply(t, identity)
is equal toop.apply(identity, t)
and is equal tot
.op
- an associative , non-interfering , stateless function to combine two elements into one.- Returns:
- a collector which returns the reduction result.
- Throws:
NullPointerException
- if op is null- Since:
- 0.7.3
- See Also:
reducingWithZero(Object, BinaryOperator)
,AbstractStreamEx.reduceWithZero(Object, Object, BinaryOperator)
,Collectors.reducing(Object, BinaryOperator)
-
-