Class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>>
- Type Parameters:
T
- the type of the stream elementsS
- the type of the stream extendingAbstractStreamEx
- All Implemented Interfaces:
AutoCloseable
,Iterable<T>
,BaseStream<T,
,Stream<T>> Stream<T>
- Direct Known Subclasses:
EntryStream
,StreamEx
StreamEx
and EntryStream
.-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
Field Summary
Fields inherited from class one.util.streamex.BaseStreamEx
CONSUMED_MESSAGE, context, spliterator
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractStreamEx
(Spliterator<? extends T> spliterator, StreamContext context) AbstractStreamEx
(Stream<? extends T> stream, StreamContext context) -
Method Summary
Modifier and TypeMethodDescription(package private) static <K,
V, M extends Map<K, V>>
voidaddToMap
(M map, K key, V val) boolean
boolean
Creates a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the other stream.(package private) S
appendSpliterator
(Stream<? extends T> other, Spliterator<? extends T> right) <U> U
Applies the supplied function to this stream and returns the result of the function.<R> R
collect
(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) <R,
A> R long
count()
long
Counts the number of elements in the stream that satisfy the predicate.distinct()
distinct
(long atLeast) Returns aStreamEx
consisting of the distinct elements (according toObject.equals(Object)
) which appear at least specified number of times in this stream.Returns a stream consisting of the distinct elements of this stream (according to object equality of the results of applying the given function).Returns a stream consisting of all elements from this stream starting from the first element which does not match the given predicate.findAny()
Returns anOptional
describing some element of the stream, which matches given predicate, or an emptyOptional
if there's no matching element.Returns anOptional
describing the first element of this stream, which matches given predicate, or an emptyOptional
if there's no matching element.<R> StreamEx<R>
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped array produced by applying the provided mapping function to each element.<R> StreamEx<R>
flatCollection
(Function<? super T, ? extends Collection<? extends R>> mapper) Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped collection produced by applying the provided mapping function to each element.<R> StreamEx<R>
flatMapToDouble
(Function<? super T, ? extends DoubleStream> mapper) flatMapToInt
(Function<? super T, ? extends IntStream> mapper) flatMapToLong
(Function<? super T, ? extends LongStream> mapper) foldLeft
(BinaryOperator<T> accumulator) Folds the elements of this stream using the provided accumulation function, going left to right.<U> U
foldLeft
(U seed, BiFunction<U, ? super T, U> accumulator) Folds the elements of this stream using the provided seed object and accumulation function, going left to right.foldRight
(BinaryOperator<T> accumulator) Folds the elements of this stream using the provided accumulation function, going right to left.<U> U
foldRight
(U seed, BiFunction<? super T, U, U> accumulator) Folds the elements of this stream using the provided seed object and accumulation function, going right to left.void
void
forEachOrdered
(Consumer<? super T> action) Returns a stream whose content is the same as this stream, except the case when this stream is empty.(package private) S
ifEmpty
(Stream<? extends T> other, Spliterator<? extends T> right) Returns anOptionalLong
describing the zero-based index of the first element of this stream, which matches given predicate, or an emptyOptionalLong
if there's no matching element.Returns anOptionalLong
describing the zero-based index of the first element of this stream, which equals to the given element, or an emptyOptionalLong
if there's no matching element.intersperse
(T delimiter) Returns a new stream containing all the elements of the original stream interspersed with given delimiter.iterator()
limit
(long maxSize) <R> StreamEx<R>
<R> StreamEx<R>
mapMulti
(BiConsumer<? super T, ? super Consumer<R>> mapper) Returns a stream where every element of this stream is replaced by elements produced by a mapper function.mapMultiToDouble
(BiConsumer<? super T, ? super DoubleConsumer> mapper) Returns aDoubleStreamEx
where every element of this stream is replaced by elements produced by a mapper function.mapMultiToInt
(BiConsumer<? super T, ? super IntConsumer> mapper) Returns anIntStreamEx
where every element of this stream is replaced by elements produced by a mapper function.mapMultiToLong
(BiConsumer<? super T, ? super LongConsumer> mapper) Returns aLongStreamEx
where every element of this stream is replaced by elements produced by a mapper function.<R> StreamEx<R>
mapPartial
(Function<? super T, ? extends Optional<? extends R>> mapper) Performs a mapping of the stream content to a partial function removing the elements to which the function is not applicable.mapToDouble
(ToDoubleFunction<? super T> mapper) mapToInt
(ToIntFunction<? super T> mapper) mapToLong
(ToLongFunction<? super T> mapper) max
(Comparator<? super T> comparator) <V extends Comparable<? super V>>
Optional<T>Returns the maximum element of this stream according to the natural order of the keys extracted by provided function.maxByDouble
(ToDoubleFunction<? super T> keyExtractor) Returns the maximum element of this stream according to the double values extracted by provided function.maxByInt
(ToIntFunction<? super T> keyExtractor) Returns the maximum element of this stream according to the int values extracted by provided function.maxByLong
(ToLongFunction<? super T> keyExtractor) Returns the maximum element of this stream according to the long values extracted by provided function.min
(Comparator<? super T> comparator) <V extends Comparable<? super V>>
Optional<T>Returns the minimum element of this stream according to the natural order of the keys extracted by provided function.minByDouble
(ToDoubleFunction<? super T> keyExtractor) Returns the minimum element of this stream according to the double values extracted by provided function.minByInt
(ToIntFunction<? super T> keyExtractor) Returns the minimum element of this stream according to the int values extracted by provided function.minByLong
(ToLongFunction<? super T> keyExtractor) Returns the minimum element of this stream according to the long values extracted by provided function.boolean
nonNull()
Returns a stream consisting of the elements of this stream that aren't null.<R> StreamEx<R>
pairMap
(BiFunction<? super T, ? super T, ? extends R> mapper) Returns a stream consisting of the results of applying the given function to the every adjacent pair of elements of this stream.parallel()
parallel
(ForkJoinPool fjp) Returns an equivalent stream that is parallel and bound to the suppliedForkJoinPool
.prefix
(BinaryOperator<T> op) Returns a stream containing cumulative results of applying the accumulation function going left to right.Creates a lazily concatenated stream whose elements are all the elements of the other stream followed by all the elements of this stream.(package private) S
prependSpliterator
(Stream<? extends T> other, Spliterator<? extends T> left) (package private) <R,
A> R rawCollect
(Collector<? super T, A, R> collector) reduce
(BinaryOperator<T> accumulator) reduce
(T identity, BinaryOperator<T> accumulator) <U> U
reduce
(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) reduceWithZero
(T zero, BinaryOperator<T> accumulator) Performs a possibly short-circuiting reduction of the stream elements using the providedBinaryOperator
.reduceWithZero
(T zero, T identity, BinaryOperator<T> accumulator) Performs a possibly short-circuiting reduction of the stream elements using the provided identity value and aBinaryOperator
.Returns a stream consisting of the elements of this stream that don't match the given predicate.reverseSorted
(Comparator<? super T> comparator) Returns a stream consisting of the elements of this stream, sorted in descending order according to the providedComparator
.scanLeft
(BinaryOperator<T> accumulator) Produces a list containing cumulative results of applying the accumulation function going left to right.<U> List<U>
scanLeft
(U seed, BiFunction<U, ? super T, U> accumulator) Produces a list containing cumulative results of applying the accumulation function going left to right using given seed value.scanRight
(BinaryOperator<T> accumulator) Produces a collection containing cumulative results of applying the accumulation function going right to left.<U> List<U>
scanRight
(U seed, BiFunction<? super T, U, U> accumulator) Produces a list containing cumulative results of applying the accumulation function going right to left using given seed value.skip
(long n) sorted()
sorted
(Comparator<? super T> comparator) <V extends Comparable<? super V>>
SReturns a stream consisting of the elements of this stream, sorted according to the natural order of the keys extracted by provided function.sortedByDouble
(ToDoubleFunction<? super T> keyExtractor) Returns a stream consisting of the elements of this stream, sorted according to the double values extracted by provided function.sortedByInt
(ToIntFunction<? super T> keyExtractor) Returns a stream consisting of the elements of this stream, sorted according to the int values extracted by provided function.sortedByLong
(ToLongFunction<? super T> keyExtractor) Returns a stream consisting of the elements of this stream, sorted according to the long values extracted by provided function.(package private) abstract S
supply
(Spliterator<T> spliterator) (package private) abstract S
Returns a stream consisting of all elements from this stream until the first element which does not match the given predicate is found.takeWhileInclusive
(Predicate<? super T> predicate) Returns a stream consisting of all elements from this stream until the first element which does not match the given predicate is found (including the first mismatching element).Object[]
toArray()
<A> A[]
toArray
(IntFunction<A[]> generator) <C extends Collection<T>>
CtoCollection
(Supplier<C> collectionFactory) Returns aCollection
containing the elements of this stream.<C extends Collection<T>,
R>
RtoCollectionAndThen
(Supplier<C> collectionFactory, Function<? super C, R> finisher) Creates a customCollection
containing the elements of this stream, then performs finishing transformation and returns its result.Returns an immutableList
containing the elements of this stream.Returns an immutableSet
containing the elements of this stream.toList()
Returns aList
containing the elements of this stream.<R> R
toListAndThen
(Function<? super List<T>, R> finisher) Creates aList
containing the elements of this stream, then performs finishing transformation and returns its result.(package private) final <K,
V, M extends Map<K, V>>
MtoMapThrowing
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valMapper, M map) Returns a mutableList
containing the elements of this stream.Returns a mutableSet
containing the elements of this stream.toSet()
Returns aSet
containing the elements of this stream.<R> R
toSetAndThen
(Function<? super Set<T>, R> finisher) Creates aSet
containing the elements of this stream, then performs finishing transformation and returns its result.Methods inherited from class one.util.streamex.BaseStreamEx
close, isParallel, spliterator, stream
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.stream.BaseStream
close, isParallel, spliterator
Methods inherited from interface java.lang.Iterable
spliterator
-
Constructor Details
-
AbstractStreamEx
AbstractStreamEx(Stream<? extends T> stream, StreamContext context) -
AbstractStreamEx
AbstractStreamEx(Spliterator<? extends T> spliterator, StreamContext context)
-
-
Method Details
-
createStream
- Specified by:
createStream
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>>
-
toMapThrowing
-
addToMap
-
rawCollect
-
appendSpliterator
-
prependSpliterator
-
ifEmpty
-
supply
-
supply
-
iterator
- Specified by:
iterator
in interfaceBaseStream<T,
S extends AbstractStreamEx<T, S>> - Specified by:
iterator
in interfaceIterable<T>
-
sequential
- Specified by:
sequential
in interfaceBaseStream<T,
S extends AbstractStreamEx<T, S>> - Overrides:
sequential
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>>
-
parallel
Description copied from class:BaseStreamEx
If this stream was created using
BaseStreamEx.parallel(ForkJoinPool)
, the new stream forgets about supplied customForkJoinPool
and its terminal operation will be executed in common pool.- Specified by:
parallel
in interfaceBaseStream<T,
S extends AbstractStreamEx<T, S>> - Overrides:
parallel
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>>
-
parallel
Description copied from class:BaseStreamEx
Returns an equivalent stream that is parallel and bound to the suppliedForkJoinPool
.This is an intermediate operation.
The terminal operation of this stream or any derived stream (except the streams created via
BaseStreamEx.parallel()
orBaseStreamEx.sequential()
methods) will be executed inside the suppliedForkJoinPool
. If current thread does not belong to that pool, it will wait till calculation finishes.- Overrides:
parallel
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>> - Parameters:
fjp
- aForkJoinPool
to submit the stream operation to.- Returns:
- a parallel stream bound to the supplied
ForkJoinPool
-
unordered
- Specified by:
unordered
in interfaceBaseStream<T,
S extends AbstractStreamEx<T, S>> - Overrides:
unordered
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>>
-
onClose
- Specified by:
onClose
in interfaceBaseStream<T,
S extends AbstractStreamEx<T, S>> - Overrides:
onClose
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>>
-
filter
-
flatMap
-
mapMulti
Returns a stream where every element of this stream is replaced by elements produced by a mapper function.This is an intermediate operation.
- Specified by:
mapMulti
in interfaceStream<T>
- Type Parameters:
R
- type of the resulting elements- Parameters:
mapper
- a non-interfering, stateless function that generates replacement elements. It accepts an element of this stream and a consumer that accepts new elements produced from the input element.- Returns:
- the new stream
- Since:
- 0.8.3
-
mapMultiToInt
Returns anIntStreamEx
where every element of this stream is replaced by elements produced by a mapper function.This is an intermediate operation.
- Specified by:
mapMultiToInt
in interfaceStream<T>
- Parameters:
mapper
- a non-interfering, stateless function that generates replacement elements. It accepts an element of this stream and a consumer that accepts new elements produced from the input element.- Returns:
- the new stream
- Since:
- 0.8.3
-
mapMultiToLong
Returns aLongStreamEx
where every element of this stream is replaced by elements produced by a mapper function.This is an intermediate operation.
- Specified by:
mapMultiToLong
in interfaceStream<T>
- Parameters:
mapper
- a non-interfering, stateless function that generates replacement elements. It accepts an element of this stream and a consumer that accepts new elements produced from the input element.- Returns:
- the new stream
- Since:
- 0.8.3
-
mapMultiToDouble
Returns aDoubleStreamEx
where every element of this stream is replaced by elements produced by a mapper function.This is an intermediate operation.
- Specified by:
mapMultiToDouble
in interfaceStream<T>
- Parameters:
mapper
- a non-interfering, stateless function that generates replacement elements. It accepts an element of this stream and a consumer that accepts new elements produced from the input element.- Returns:
- the new stream
- Since:
- 0.8.3
-
map
-
mapToInt
-
mapToLong
-
mapToDouble
- Specified by:
mapToDouble
in interfaceStream<T>
-
flatMapToInt
- Specified by:
flatMapToInt
in interfaceStream<T>
-
flatMapToLong
- Specified by:
flatMapToLong
in interfaceStream<T>
-
flatMapToDouble
- Specified by:
flatMapToDouble
in interfaceStream<T>
-
intersperse
Returns a new stream containing all the elements of the original stream interspersed with given delimiter.For example,
StreamEx.of("a", "b", "c").intersperse("x")
will yield a stream containing five elements: a, x, b, x, c.This is an intermediate operation.
- Parameters:
delimiter
- a delimiter to be inserted between each pair of elements- Returns:
- the new stream
- Since:
- 0.6.6
-
distinct
-
distinct
Returns a stream consisting of the distinct elements of this stream (according to object equality of the results of applying the given function).For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
- Parameters:
keyExtractor
- a non-interfering, stateless function which classifies input elements.- Returns:
- the new stream
- Since:
- 0.3.8
-
distinct
Returns aStreamEx
consisting of the distinct elements (according toObject.equals(Object)
) which appear at least specified number of times in this stream.This operation is not guaranteed to be stable: any of equal elements can be selected for the output. However, if this stream is ordered then order is preserved.
This is a stateful quasi-intermediate operation.
- Parameters:
atLeast
- minimal number of occurrences required to select the element. If atLeast is 1 or less, then this method is equivalent todistinct()
.- Returns:
- the new stream
- Since:
- 0.3.1
- See Also:
-
sorted
-
sorted
-
peek
-
limit
-
skip
-
forEach
-
forEachOrdered
- Specified by:
forEachOrdered
in interfaceStream<T>
-
toArray
-
toArray
-
reduce
-
reduce
-
reduce
-
reduceWithZero
Performs a possibly short-circuiting reduction of the stream elements using the providedBinaryOperator
. The result is described as anOptional<T>
.This is a short-circuiting terminal operation. It behaves like
reduce(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 operation takes the advantage of this and may short-circuit if zero is reached during the reduction.- Parameters:
zero
- zero elementaccumulator
- an associative , non-interfering , stateless function to combine two elements into one.- Returns:
- the result of reduction. Empty Optional is returned if the input stream is empty.
- Throws:
NullPointerException
- if accumulator is null or the result of reduction is null- Since:
- 0.7.3
- See Also:
-
reduceWithZero
Performs a possibly short-circuiting reduction of the stream elements using the provided identity value and aBinaryOperator
.This is a short-circuiting terminal operation. It behaves like
reduce(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 operation takes the advantage of this and may short-circuit if zero is reached during the reduction.- Parameters:
zero
- zero elementidentity
- an identity element. For allt
,accumulator.apply(t, identity)
is equal toaccumulator.apply(identity, t)
and is equal tot
.accumulator
- an associative , non-interfering , stateless function to combine two elements into one.- Returns:
- the result of reduction. Empty Optional is returned if the input stream is empty.
- Throws:
NullPointerException
- if accumulator is null or the result of reduction is null- Since:
- 0.7.3
- See Also:
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) -
collect
If special short-circuiting collector is passed, this operation becomes short-circuiting as well.
-
min
-
max
-
count
public long count() -
count
Counts the number of elements in the stream that satisfy the predicate.This is a terminal operation.
- Parameters:
predicate
- a non-interfering , stateless predicate to apply to stream elements. Only elements passing the predicate will be counted.- Returns:
- the count of elements in this stream satisfying the predicate.
-
anyMatch
-
allMatch
-
noneMatch
-
findFirst
-
findAny
-
indexOf
Returns anOptionalLong
describing the zero-based index of the first element of this stream, which equals to the given element, or an emptyOptionalLong
if there's no matching element.This is a short-circuiting terminal operation.
- Parameters:
element
- an element to look for- Returns:
- an
OptionalLong
describing the index of the first matching element of this stream, or an emptyOptionalLong
if there's no matching element. - Since:
- 0.4.0
- See Also:
-
indexOf
Returns anOptionalLong
describing the zero-based index of the first element of this stream, which matches given predicate, or an emptyOptionalLong
if there's no matching element.This is a short-circuiting terminal operation.
- Parameters:
predicate
- a non-interfering , stateless predicate which returned value should match- Returns:
- an
OptionalLong
describing the index of the first matching element of this stream, or an emptyOptionalLong
if there's no matching element. - Since:
- 0.4.0
- See Also:
-
flatCollection
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped collection produced by applying the provided mapping function to each element. (If a mapped collection isnull
nothing is added for given element to the resulting stream.)This is an intermediate operation.
The
flatCollection()
operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.- Type Parameters:
R
- The element type of the new stream- Parameters:
mapper
- a non-interfering , stateless function to apply to each element which produces aCollection
of new values- Returns:
- the new stream
-
flatArray
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped array produced by applying the provided mapping function to each element. (If a mapped array isnull
nothing is added for given element to the resulting stream.)This is an intermediate operation.
The
flatArray()
operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.- Type Parameters:
R
- The element type of the new stream- Parameters:
mapper
- a non-interfering , stateless function to apply to each element which produces an array of new values- Returns:
- the new stream
- Since:
- 0.6.5
-
mapPartial
Performs a mapping of the stream content to a partial function removing the elements to which the function is not applicable.If the mapping function returns
Optional.empty()
, the original value will be removed from the resulting stream. The mapping function may not return null.This is an intermediate operation.
The
mapPartial()
operation has the effect of applying a one-to-zero-or-one transformation to the elements of the stream, and then flattening the resulting elements into a new stream.- Type Parameters:
R
- The element type of the new stream- Parameters:
mapper
- a non-interfering , stateless partial function to apply to each element which returns a present optional if it's applicable, or an empty optional otherwise- Returns:
- the new stream
- Since:
- 0.6.8
-
pairMap
Returns a stream consisting of the results of applying the given function to the every adjacent pair of elements of this stream.This is a quasi-intermediate operation.
The output stream will contain one element less than this stream. If this stream contains zero or one element the output stream will be empty.
- Type Parameters:
R
- The element type of the new stream- Parameters:
mapper
- a non-interfering, stateless function to apply to each adjacent pair of this stream elements.- Returns:
- the new stream
- Since:
- 0.2.1
-
remove
Returns a stream consisting of the elements of this stream that don't match the given predicate.This is an intermediate operation.
- Parameters:
predicate
- a non-interfering , stateless predicate to apply to each element to determine if it should be excluded- Returns:
- the new stream
- See Also:
-
nonNull
Returns a stream consisting of the elements of this stream that aren't null.This is an intermediate operation.
- Returns:
- the new stream
- See Also:
-
findAny
Returns anOptional
describing some element of the stream, which matches given predicate, or an emptyOptional
if there's no matching element.This is a short-circuiting terminal operation.
The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations on the same source may not return the same result. (If a stable result is desired, use
findFirst(Predicate)
instead.)- Parameters:
predicate
- a non-interfering , stateless predicate which returned value should match- Returns:
- an
Optional
describing some matching element of this stream, or an emptyOptional
if there's no matching element - Throws:
NullPointerException
- if the element selected is null- See Also:
-
findFirst
Returns anOptional
describing the first element of this stream, which matches given predicate, or an emptyOptional
if there's no matching element.This is a short-circuiting terminal operation.
- Parameters:
predicate
- a non-interfering , stateless predicate which returned value should match- Returns:
- an
Optional
describing the first matching element of this stream, or an emptyOptional
if there's no matching element - Throws:
NullPointerException
- if the element selected is null- See Also:
-
reverseSorted
Returns a stream consisting of the elements of this stream, sorted in descending order according to the providedComparator
.For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
- Parameters:
comparator
- a non-interfering , statelessComparator
to be used to compare stream elements- Returns:
- the new stream
-
sortedBy
Returns a stream consisting of the elements of this stream, sorted according to the natural order of the keys extracted by provided function.For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
- Type Parameters:
V
- the type of theComparable
sort key- Parameters:
keyExtractor
- a non-interfering , stateless function to be used to extract sorting keys- Returns:
- the new stream
-
sortedByInt
Returns a stream consisting of the elements of this stream, sorted according to the int values extracted by provided function.For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
- Parameters:
keyExtractor
- a non-interfering , stateless function to be used to extract sorting keys- Returns:
- the new stream
-
sortedByLong
Returns a stream consisting of the elements of this stream, sorted according to the long values extracted by provided function.For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
- Parameters:
keyExtractor
- a non-interfering , stateless function to be used to extract sorting keys- Returns:
- the new stream
-
sortedByDouble
Returns a stream consisting of the elements of this stream, sorted according to the double values extracted by provided function.For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
- Parameters:
keyExtractor
- a non-interfering , stateless function to be used to extract sorting keys- Returns:
- the new stream
-
minBy
public <V extends Comparable<? super V>> Optional<T> minBy(Function<? super T, ? extends V> keyExtractor) Returns the minimum element of this stream according to the natural order of the keys extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
min(Comparator.comparing(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Type Parameters:
V
- the type of the comparable keys- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the comparable keys from this stream elements- Returns:
- an
Optional
describing the minimum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the minimum element is null
-
minByInt
Returns the minimum element of this stream according to the int values extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
min(Comparator.comparingInt(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the int keys from this stream elements- Returns:
- an
Optional
describing the minimum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the minimum element is null
-
minByLong
Returns the minimum element of this stream according to the long values extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
min(Comparator.comparingLong(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the long keys from this stream elements- Returns:
- an
Optional
describing the minimum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the minimum element is null
-
minByDouble
Returns the minimum element of this stream according to the double values extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
min(Comparator.comparingDouble(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the double keys from this stream elements- Returns:
- an
Optional
describing the minimum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the minimum element is null
-
maxBy
public <V extends Comparable<? super V>> Optional<T> maxBy(Function<? super T, ? extends V> keyExtractor) Returns the maximum element of this stream according to the natural order of the keys extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
max(Comparator.comparing(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Type Parameters:
V
- the type of the comparable keys- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the comparable keys from this stream elements- Returns:
- an
Optional
describing the maximum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the maximum element is null
-
maxByInt
Returns the maximum element of this stream according to the int values extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
max(Comparator.comparingInt(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the int keys from this stream elements- Returns:
- an
Optional
describing the maximum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the maximum element is null
-
maxByLong
Returns the maximum element of this stream according to the long values extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
max(Comparator.comparingLong(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the long keys from this stream elements- Returns:
- an
Optional
describing the maximum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the maximum element is null
-
maxByDouble
Returns the maximum element of this stream according to the double values extracted by provided function. This is a special case of a reduction.This is a terminal operation.
This method is equivalent to
max(Comparator.comparingDouble(keyExtractor))
, but may work faster as keyExtractor function is applied only once per each input element.- Parameters:
keyExtractor
- a non-interfering , stateless function to extract the double keys from this stream elements- Returns:
- an
Optional
describing the maximum element of this stream, or an emptyOptional
if the stream is empty - Throws:
NullPointerException
- if the maximum element is null
-
append
Creates a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the other stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers for both input streams are invoked.This is a quasi-intermediate operation with tail-stream optimization.
May return this if the supplied stream is known to be empty.
- Parameters:
other
- the other stream- Returns:
- this stream appended by the other stream
- See Also:
-
prepend
Creates a lazily concatenated stream whose elements are all the elements of the other stream followed by all the elements of this stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers for both input streams are invoked.This is a quasi-intermediate operation with tail-stream optimization.
May return this if the supplied stream is known to be empty.
- Parameters:
other
- the other stream- Returns:
- this stream prepended by the other stream
- See Also:
-
ifEmpty
Returns a stream whose content is the same as this stream, except the case when this stream is empty. In this case, its contents is replaced with other stream contents.The other stream will not be traversed if this stream is not empty.
If this stream is parallel and empty, the other stream is not guaranteed to be parallelized.
This is a quasi-intermediate operation.
- Parameters:
other
- other stream to replace the contents of this stream if this stream is empty.- Returns:
- the stream whose content is replaced by other stream contents only if this stream is empty.
- Since:
- 0.6.6
-
toList
Returns aList
containing the elements of this stream. There are no guarantees on the type, mutability, serializability, or thread-safety of the returnedList
; if more control over the returnedList
is required, usetoCollection(Supplier)
.This is a terminal operation.
-
toMutableList
Returns a mutableList
containing the elements of this stream. There are no guarantees on the type, serializability, or thread-safety of the returnedList
; if more control over the returnedList
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
List
containing the elements of this stream - Since:
- 0.8.0
- See Also:
-
toImmutableList
Returns an immutableList
containing the elements of this stream. There's no guarantees on exact type of the returnedList
. The returnedList
is guaranteed to be serializable if all its elements are serializable.This is a terminal operation.
- Returns:
- a
List
containing the elements of this stream - Since:
- 0.6.3
- See Also:
-
toListAndThen
Creates aList
containing the elements of this stream, then performs finishing transformation and returns its result. There are no guarantees on the type, serializability or thread-safety of theList
created.This is a terminal operation.
- Type Parameters:
R
- the type of the result- Parameters:
finisher
- a function to be applied to the intermediate list- Returns:
- result of applying the finisher transformation to the list of the stream elements.
- Since:
- 0.2.3
- See Also:
-
toSet
Returns aSet
containing the elements of this stream. There are no guarantees on the type, mutability, serializability, or thread-safety of the returnedSet
; if more control over the returnedSet
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
Set
containing the elements of this stream - See Also:
-
toMutableSet
Returns a mutableSet
containing the elements of this stream. There are no guarantees on the type, serializability, or thread-safety of the returnedSet
; if more control over the returnedSet
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
Set
containing the elements of this stream - Since:
- 0.8.0
- See Also:
-
toImmutableSet
Returns an immutableSet
containing the elements of this stream. There's no guarantees on exact type of the returnedSet
. In particular, no specific element order in the resulting set is guaranteed. The returnedSet
is guaranteed to be serializable if all its elements are serializable.This is a terminal operation.
- Returns:
- a
Set
containing the elements of this stream - Since:
- 0.6.3
- See Also:
-
toSetAndThen
Creates aSet
containing the elements of this stream, then performs finishing transformation and returns its result. There are no guarantees on the type, serializability or thread-safety of theSet
created.This is a terminal operation.
- Type Parameters:
R
- the result type- Parameters:
finisher
- a function to be applied to the intermediateSet
- Returns:
- result of applying the finisher transformation to the
Set
of the stream elements. - Since:
- 0.2.3
- See Also:
-
toCollectionAndThen
public <C extends Collection<T>,R> R toCollectionAndThen(Supplier<C> collectionFactory, Function<? super C, R> finisher) Creates a customCollection
containing the elements of this stream, then performs finishing transformation and returns its result. TheCollection
is created by the provided factory.This is a terminal operation.
- Type Parameters:
C
- the type of the resultingCollection
R
- the result type- Parameters:
collectionFactory
- aSupplier
which returns a new, emptyCollection
of the appropriate typefinisher
- a function to be applied to the intermediateCollection
- Returns:
- result of applying the finisher transformation to the
Collection
of the stream elements. - Since:
- 0.7.3
- See Also:
-
toCollection
Returns aCollection
containing the elements of this stream. TheCollection
is created by the provided factory.This is a terminal operation.
- Type Parameters:
C
- the type of the resultingCollection
- Parameters:
collectionFactory
- aSupplier
which returns a new, emptyCollection
of the appropriate type- Returns:
- a
Collection
containing the elements of this stream - See Also:
-
foldLeft
Folds the elements of this stream using the provided seed object and accumulation function, going left to right. This is equivalent to:U result = seed; for (T element : this stream) result = accumulator.apply(result, element) return result;
This is a terminal operation.
This method cannot take all the advantages of parallel streams as it must process elements strictly left to right. If your accumulator function is associative, and you can provide a combiner function, consider using
reduce(Object, BiFunction, BinaryOperator)
method.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
- Type Parameters:
U
- The type of the result- Parameters:
seed
- the starting valueaccumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the result of the folding
- Since:
- 0.2.0
- See Also:
-
foldLeft
Folds the elements of this stream using the provided accumulation function, going left to right. This is equivalent to:boolean foundAny = false; T result = null; for (T element : this stream) { if (!foundAny) { foundAny = true; result = element; } else result = accumulator.apply(result, element); } return foundAny ? Optional.of(result) : Optional.empty();
This is a terminal operation.
This method cannot take all the advantages of parallel streams as it must process elements strictly left to right. If your accumulator function is associative, consider using
reduce(BinaryOperator)
method.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
- Parameters:
accumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the result of the folding
- Since:
- 0.4.0
- See Also:
-
foldRight
Folds the elements of this stream using the provided seed object and accumulation function, going right to left.This is a terminal operation.
As this method must process elements strictly right to left, it cannot start processing till all the previous stream stages complete. Also, it requires intermediate memory to store the whole content of the stream as the stream natural order is left to right. If your accumulator function is associative, and you can provide a combiner function, consider using
reduce(Object, BiFunction, BinaryOperator)
method.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
- Type Parameters:
U
- The type of the result- Parameters:
seed
- the starting valueaccumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the result of the folding
- Since:
- 0.2.2
- See Also:
-
foldRight
Folds the elements of this stream using the provided accumulation function, going right to left.This is a terminal operation.
As this method must process elements strictly right to left, it cannot start processing till all the previous stream stages complete. Also, it requires intermediate memory to store the whole content of the stream as the stream natural order is left to right. If your accumulator function is associative, consider using
reduce(BinaryOperator)
method.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
- Parameters:
accumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the result of the folding
- Since:
- 0.4.0
- See Also:
-
scanLeft
Produces a list containing cumulative results of applying the accumulation function going left to right using given seed value.This is a terminal operation.
The resulting
List
is guaranteed to be mutable.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
This method cannot take all the advantages of parallel streams as it must process elements strictly left to right.
- Type Parameters:
U
- The type of the result- Parameters:
seed
- the starting valueaccumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the
List
where the first element is the seed and every successor element is the result of applying accumulator function to the previous list element and the corresponding stream element. The resulting list is one element longer than this stream. - Since:
- 0.2.1
- See Also:
-
scanLeft
Produces a list containing cumulative results of applying the accumulation function going left to right.This is a terminal operation.
The resulting
List
is guaranteed to be mutable.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
This method cannot take all the advantages of parallel streams as it must process elements strictly left to right.
- Parameters:
accumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the
List
where the first element is the first element of this stream and every successor element is the result of applying accumulator function to the previous list element and the corresponding stream element. The resulting list has the same size as this stream. - Since:
- 0.4.0
- See Also:
-
scanRight
Produces a list containing cumulative results of applying the accumulation function going right to left using given seed value.This is a terminal operation.
The resulting
List
is guaranteed to be mutable.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
This method cannot take all the advantages of parallel streams as it must process elements strictly right to left.
- Type Parameters:
U
- The type of the result- Parameters:
seed
- the starting valueaccumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the
List
where the last element is the seed and every predecessor element is the result of applying accumulator function to the corresponding stream element and the next list element. The resulting list is one element longer than this stream. - Since:
- 0.2.2
- See Also:
-
scanRight
Produces a collection containing cumulative results of applying the accumulation function going right to left.This is a terminal operation.
The result
List
is guaranteed to be mutable.For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.
This method cannot take all the advantages of parallel streams as it must process elements strictly right to left.
- Parameters:
accumulator
- a non-interfering , stateless function for incorporating an additional element into a result- Returns:
- the
List
where the last element is the last element of this stream and every predecessor element is the result of applying accumulator function to the corresponding stream element and the next list element. The resulting list is one element longer than this stream. - Since:
- 0.4.0
- See Also:
-
takeWhile
Returns a stream consisting of all elements from this stream until the first element which does not match the given predicate is found.This is a short-circuiting stateful operation. It can be either intermediate or quasi-intermediate. When using with JDK 1.9 or higher it calls the corresponding JDK 1.9 implementation. When using with JDK 1.8 it uses own implementation.
While this operation is quite cheap for sequential stream, it can be quite expensive on parallel pipelines. Using unordered source or making it explicitly unordered with
unordered()
call may improve the parallel processing performance if semantics permit. -
takeWhileInclusive
Returns a stream consisting of all elements from this stream until the first element which does not match the given predicate is found (including the first mismatching element).This is a quasi-intermediate operation.
While this operation is quite cheap for sequential stream, it can be quite expensive on parallel pipelines. Using unordered source or making it explicitly unordered with
unordered()
call may improve the parallel processing performance if semantics permit.- Parameters:
predicate
- a non-interfering, stateless predicate to apply to elements.- Returns:
- the new stream.
- Since:
- 0.5.5
- See Also:
-
dropWhile
Returns a stream consisting of all elements from this stream starting from the first element which does not match the given predicate. If the predicate is true for all stream elements, an empty stream is returned.This is a stateful operation. It can be either intermediate or quasi-intermediate. When using with JDK 1.9 or higher it calls the corresponding JDK 1.9 implementation. When using with JDK 1.8 it uses own implementation.
While this operation is quite cheap for sequential stream, it can be quite expensive on parallel pipelines. Using unordered source or making it explicitly unordered with
unordered()
call may improve the parallel processing performance if semantics permit. -
prefix
Returns a stream containing cumulative results of applying the accumulation function going left to right.This is a stateful quasi-intermediate operation.
This operation resembles
scanLeft(BinaryOperator)
, but unlikescanLeft
this operation is intermediate and accumulation function must be associative.This method cannot take all the advantages of parallel streams as it must process elements strictly left to right. Using an unordered source or removing the ordering constraint with
unordered()
may improve the parallel processing speed.- Parameters:
op
- an associative, non-interfering , stateless function for computing the next element based on the previous one- Returns:
- the new stream.
- Since:
- 0.6.1
- See Also:
-
chain
Description copied from class:BaseStreamEx
Applies the supplied function to this stream and returns the result of the function.This method can be used to add more functionality in the fluent style. For example, consider user-defined static method
batches(stream, n)
which breaks the stream into batches of given length. Normally you would writebatches(StreamEx.of(input).map(...), 10).filter(...)
. Using thechain()
method you can write in more fluent manner:StreamEx.of(input).map(...).chain(s -> batches(s, 10)).filter(...)
.You could even go further and define a method which returns a function like
<T> UnaryOperator<StreamEx<T>> batches(int n)
and use it like this:StreamEx.of(input).map(...).chain(batches(10)).filter(...)
.- Specified by:
chain
in classBaseStreamEx<T,
Stream<T>, Spliterator<T>, S extends AbstractStreamEx<T, S>> - Type Parameters:
U
- the type of the function result.- Parameters:
mapper
- function to invoke.- Returns:
- the result of the function invocation.
-