Class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>>
- Object
-
- AbstractStreamEx<T,S>
-
- Type Parameters:
T
- the type of the stream elementsS
- the type of of the stream extendingAbstractStreamEx
- All Implemented Interfaces:
AutoCloseable
,Iterable<T>
,BaseStream<T,Stream<T>>
,Stream<T>
- Direct Known Subclasses:
EntryStream
,StreamEx
public abstract class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>> extends Object implements Stream<T>, Iterable<T>
Base class providing common functionality forStreamEx
andEntryStream
.- Author:
- Tagir Valeev
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface Stream
Stream.Builder<T extends Object>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allMatch(Predicate<? super T> predicate)
boolean
anyMatch(Predicate<? super T> predicate)
S
append(Stream<? extends T> other)
Creates a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the other stream.<U> U
chain(Function<? super S,U> mapper)
Applies the supplied function to this stream and returns the result of the function.void
close()
<R> R
collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
<R,A>
Rcollect(Collector<? super T,A,R> collector)
long
count()
S
distinct()
S
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.S
distinct(Function<? super T,?> keyExtractor)
Returns a stream consisting of the distinct elements of this stream (according to object equality of the results of applying the given function).S
dropWhile(Predicate<? super T> predicate)
Returns a stream consisting of all elements from this stream starting from the first element which does not match the given predicate.S
filter(Predicate<? super T> predicate)
Optional<T>
findAny()
Optional<T>
findAny(Predicate<? super T> predicate)
Returns anOptional
describing some element of the stream, which matches given predicate, or an emptyOptional
if there's no matching element.Optional<T>
findFirst()
Optional<T>
findFirst(Predicate<? super T> predicate)
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>
flatArray(Function<? super T,? extends R[]> mapper)
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>
flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
DoubleStreamEx
flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
IntStreamEx
flatMapToInt(Function<? super T,? extends IntStream> mapper)
LongStreamEx
flatMapToLong(Function<? super T,? extends LongStream> mapper)
Optional<T>
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.Optional<T>
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
forEach(Consumer<? super T> action)
void
forEachOrdered(Consumer<? super T> action)
S
ifEmpty(Stream<? extends T> other)
Returns a stream which contents is the same as this stream, except the case when this stream is empty.OptionalLong
indexOf(Predicate<? super T> predicate)
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.OptionalLong
indexOf(T 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.S
intersperse(T delimiter)
Returns a new stream containing all the elements of the original stream interspersed with given delimiter.boolean
isParallel()
Iterator<T>
iterator()
S
limit(long maxSize)
<R> StreamEx<R>
map(Function<? super T,? extends R> mapper)
<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.DoubleStreamEx
mapToDouble(ToDoubleFunction<? super T> mapper)
IntStreamEx
mapToInt(ToIntFunction<? super T> mapper)
LongStreamEx
mapToLong(ToLongFunction<? super T> mapper)
Optional<T>
max(Comparator<? super T> comparator)
<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.Optional<T>
maxByDouble(ToDoubleFunction<? super T> keyExtractor)
Returns the maximum element of this stream according to the double values extracted by provided function.Optional<T>
maxByInt(ToIntFunction<? super T> keyExtractor)
Returns the maximum element of this stream according to the int values extracted by provided function.Optional<T>
maxByLong(ToLongFunction<? super T> keyExtractor)
Returns the maximum element of this stream according to the long values extracted by provided function.Optional<T>
min(Comparator<? super T> comparator)
<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.Optional<T>
minByDouble(ToDoubleFunction<? super T> keyExtractor)
Returns the minimum element of this stream according to the double values extracted by provided function.Optional<T>
minByInt(ToIntFunction<? super T> keyExtractor)
Returns the minimum element of this stream according to the int values extracted by provided function.Optional<T>
minByLong(ToLongFunction<? super T> keyExtractor)
Returns the minimum element of this stream according to the long values extracted by provided function.boolean
noneMatch(Predicate<? super T> predicate)
S
nonNull()
Returns a stream consisting of the elements of this stream that aren't null.S
onClose(Runnable closeHandler)
<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.S
parallel()
S
parallel(ForkJoinPool fjp)
Returns an equivalent stream that is parallel and bound to the suppliedForkJoinPool
.S
peek(Consumer<? super T> action)
S
prefix(BinaryOperator<T> op)
Returns a stream containing cumulative results of applying the accumulation function going left to right.S
prepend(Stream<? extends T> other)
Creates a lazily concatenated stream whose elements are all the elements of the other stream followed by all the elements of this stream.Optional<T>
reduce(BinaryOperator<T> accumulator)
T
reduce(T identity, BinaryOperator<T> accumulator)
<U> U
reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
Optional<T>
reduceWithZero(T zero, BinaryOperator<T> accumulator)
Performs a possibly short-circuiting reduction of the stream elements using the providedBinaryOperator
.T
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
.S
remove(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that don't match the given predicate.S
reverseSorted(Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted in descending order according to the providedComparator
.List<T>
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.List<T>
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.S
sequential()
S
skip(long n)
S
sorted()
S
sorted(Comparator<? super T> comparator)
<V extends Comparable<? super V>>
SsortedBy(Function<? super T,? extends V> keyExtractor)
Returns a stream consisting of the elements of this stream, sorted according to the natural order of the keys extracted by provided function.S
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.S
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.S
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.SPLTR
spliterator()
S
takeWhile(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.S
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.List<T>
toImmutableList()
Returns an immutableList
containing the elements of this stream.Set<T>
toImmutableSet()
Returns an immutableSet
containing the elements of this stream.List<T>
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.Set<T>
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.S
unordered()
-
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface BaseStream
close, isParallel, spliterator
-
Methods inherited from interface Iterable
spliterator
-
-
-
-
Method Detail
-
iterator
public Iterator<T> iterator()
- Specified by:
iterator
in interfaceBaseStream<T,S extends AbstractStreamEx<T,S>>
- Specified by:
iterator
in interfaceIterable<T>
-
sequential
public S sequential()
- Specified by:
sequential
in interfaceBaseStream<T,S extends AbstractStreamEx<T,S>>
-
parallel
public S parallel()
If this stream was created using
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>>
-
parallel
public S parallel(ForkJoinPool fjp)
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
BaseStream.parallel()
orBaseStream.sequential()
methods) will be executed inside the suppliedForkJoinPool
. If current thread does not belong to that pool, it will wait till calculation finishes.- Parameters:
fjp
- aForkJoinPool
to submit the stream operation to.- Returns:
- a parallel stream bound to the supplied
ForkJoinPool
-
unordered
public S unordered()
- Specified by:
unordered
in interfaceBaseStream<T,S extends AbstractStreamEx<T,S>>
-
onClose
public S onClose(Runnable closeHandler)
- Specified by:
onClose
in interfaceBaseStream<T,S extends AbstractStreamEx<T,S>>
-
filter
public S filter(Predicate<? super T> predicate)
- Specified by:
filter
in interfaceStream<T>
- See Also:
nonNull()
,remove(Predicate)
,StreamEx.select(Class)
-
mapToInt
public IntStreamEx mapToInt(ToIntFunction<? super T> mapper)
-
mapToLong
public LongStreamEx mapToLong(ToLongFunction<? super T> mapper)
-
mapToDouble
public DoubleStreamEx mapToDouble(ToDoubleFunction<? super T> mapper)
- Specified by:
mapToDouble
in interfaceStream<T>
-
flatMapToInt
public IntStreamEx flatMapToInt(Function<? super T,? extends IntStream> mapper)
- Specified by:
flatMapToInt
in interfaceStream<T>
-
flatMapToLong
public LongStreamEx flatMapToLong(Function<? super T,? extends LongStream> mapper)
- Specified by:
flatMapToLong
in interfaceStream<T>
-
flatMapToDouble
public DoubleStreamEx flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
- Specified by:
flatMapToDouble
in interfaceStream<T>
-
intersperse
public S intersperse(T delimiter)
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
public S distinct(Function<? super T,?> keyExtractor)
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
public S 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.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:
distinct()
-
sorted
public S sorted(Comparator<? super T> comparator)
-
forEachOrdered
public void forEachOrdered(Consumer<? super T> action)
- Specified by:
forEachOrdered
in interfaceStream<T>
-
toArray
public <A> A[] toArray(IntFunction<A[]> generator)
-
reduce
public T reduce(T identity, BinaryOperator<T> accumulator)
-
reduce
public Optional<T> reduce(BinaryOperator<T> accumulator)
-
reduce
public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
-
reduceWithZero
public Optional<T> reduceWithZero(T zero, BinaryOperator<T> accumulator)
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:
MoreCollectors.reducingWithZero(Object, BinaryOperator)
,reduceWithZero(Object, Object, BinaryOperator)
,reduce(BinaryOperator)
-
reduceWithZero
public T 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
.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:
MoreCollectors.reducingWithZero(Object, Object, BinaryOperator)
,reduceWithZero(Object, BinaryOperator)
,reduce(Object, BinaryOperator)
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
-
collect
public <R,A> R collect(Collector<? super T,A,R> collector)
If special short-circuiting collector is passed, this operation becomes short-circuiting as well.
-
min
public Optional<T> min(Comparator<? super T> comparator)
-
max
public Optional<T> max(Comparator<? super T> comparator)
-
indexOf
public OptionalLong indexOf(T 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.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(Predicate)
-
indexOf
public OptionalLong indexOf(Predicate<? super T> predicate)
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:
findFirst(Predicate)
,indexOf(Object)
-
flatCollection
public <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. (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
public <R> StreamEx<R> flatArray(Function<? super T,? extends R[]> mapper)
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
public <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.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
public <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.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
public S remove(Predicate<? super T> predicate)
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:
filter(Predicate)
,nonNull()
,StreamEx.select(Class)
-
nonNull
public S 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:
filter(Predicate)
,remove(Predicate)
,StreamEx.select(Class)
-
findAny
public Optional<T> findAny(Predicate<? super T> predicate)
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:
findAny()
,findFirst(Predicate)
-
findFirst
public Optional<T> findFirst(Predicate<? super T> predicate)
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:
findFirst()
-
reverseSorted
public S reverseSorted(Comparator<? super T> comparator)
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
public <V extends Comparable<? super V>> S sortedBy(Function<? super T,? extends V> keyExtractor)
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
public S 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.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
public S 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.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
public S 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.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
public Optional<T> minByInt(ToIntFunction<? super T> keyExtractor)
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
public Optional<T> minByLong(ToLongFunction<? super T> keyExtractor)
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
public Optional<T> minByDouble(ToDoubleFunction<? super T> keyExtractor)
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
public Optional<T> maxByInt(ToIntFunction<? super T> keyExtractor)
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
public Optional<T> maxByLong(ToLongFunction<? super T> keyExtractor)
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
public Optional<T> maxByDouble(ToDoubleFunction<? super T> keyExtractor)
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
public S append(Stream<? extends T> other)
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:
Stream.concat(Stream, Stream)
-
prepend
public S prepend(Stream<? extends T> other)
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:
Stream.concat(Stream, Stream)
-
ifEmpty
public S ifEmpty(Stream<? extends T> other)
Returns a stream which contents 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 which contents is replaced by other stream contents only if this stream is empty.
- Since:
- 0.6.6
-
toList
public List<T> toList()
Returns aList
containing the elements of this stream. The returnedList
is guaranteed to be mutable, but there are no guarantees on the type, serializability, or thread-safety; if more control over the returnedList
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
List
containing the elements of this stream - See Also:
Collectors.toList()
,toImmutableList()
-
toImmutableList
public List<T> 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:
toList()
-
toListAndThen
public <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. 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:
toList()
-
toSet
public Set<T> toSet()
Returns aSet
containing the elements of this stream. The returnedSet
is guaranteed to be mutable, but there are no guarantees on the type, serializability, or thread-safety; 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:
Collectors.toSet()
-
toImmutableSet
public Set<T> 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:
toSet()
-
toSetAndThen
public <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. 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:
toSet()
-
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(Supplier)
-
toCollection
public <C extends Collection<T>> C toCollection(Supplier<C> collectionFactory)
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:
Collectors.toCollection(Supplier)
,toCollectionAndThen(Supplier, Function)
-
foldLeft
public <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. 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:
foldRight(Object, BiFunction)
,reduce(Object, BinaryOperator)
,reduce(Object, BiFunction, BinaryOperator)
-
foldLeft
public Optional<T> foldLeft(BinaryOperator<T> accumulator)
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:
foldLeft(Object, BiFunction)
,foldRight(BinaryOperator)
,reduce(BinaryOperator)
-
foldRight
public <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.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:
foldLeft(Object, BiFunction)
,reduce(Object, BinaryOperator)
,reduce(Object, BiFunction, BinaryOperator)
-
foldRight
public Optional<T> foldRight(BinaryOperator<T> accumulator)
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:
foldRight(Object, BiFunction)
,foldLeft(BinaryOperator)
,reduce(BinaryOperator)
-
scanLeft
public <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.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:
foldLeft(Object, BiFunction)
,scanRight(Object, BiFunction)
-
scanLeft
public List<T> scanLeft(BinaryOperator<T> accumulator)
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:
foldLeft(BinaryOperator)
,scanRight(BinaryOperator)
,prefix(BinaryOperator)
-
scanRight
public <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.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:
scanLeft(Object, BiFunction)
,foldRight(Object, BiFunction)
-
scanRight
public List<T> scanRight(BinaryOperator<T> accumulator)
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:
scanLeft(BinaryOperator)
,foldRight(BinaryOperator)
-
takeWhile
public S takeWhile(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.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.- Specified by:
takeWhile
in interfaceStream<T>
- Parameters:
predicate
- a non-interfering, stateless predicate to apply to elements.- Returns:
- the new stream.
- Since:
- 0.3.6
- See Also:
takeWhileInclusive(Predicate)
,dropWhile(Predicate)
-
takeWhileInclusive
public S 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).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:
takeWhile(Predicate)
-
dropWhile
public S dropWhile(Predicate<? super T> predicate)
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
public S prefix(BinaryOperator<T> op)
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:
scanLeft(BinaryOperator)
-
chain
public <U> U chain(Function<? super S,U> mapper)
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(...)
.- Type Parameters:
U
- the type of the function result.- Parameters:
mapper
- function to invoke.- Returns:
- the result of the function invocation.
-
spliterator
public SPLTR spliterator()
- Specified by:
spliterator
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
isParallel
public boolean isParallel()
- Specified by:
isParallel
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
-