T - the type of the stream elementsS - the type of of the stream extending AbstractStreamExpublic abstract class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>> extends Object implements Stream<T>, Iterable<T>
StreamEx and EntryStream.Stream.Builder<T>| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(Predicate<? super T> predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(Predicate<? super T> predicate)
Returns whether any elements of this stream match the provided
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()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<R,A> R |
collect(Collector<? super T,A,R> collector)
Performs a mutable
reduction operation on the elements of this stream using a
Collector. |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super T> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable
reduction operation on the elements of this stream.
|
long |
count()
Returns the count of elements in this stream.
|
S |
distinct()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object)) of 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)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
Optional<T> |
findAny()
Returns an
Optional describing some element of the stream, or an
empty Optional if the stream is empty. |
Optional<T> |
findAny(Predicate<? super T> predicate)
Returns an
Optional describing some element of the stream, which
matches given predicate, or an empty Optional if there's no
matching element. |
Optional<T> |
findFirst()
Returns an
Optional describing the first element of this stream,
or an empty Optional if the stream is empty. |
Optional<T> |
findFirst(Predicate<? super T> predicate)
Returns an
Optional describing the first element of this stream,
which matches given predicate, or an empty Optional 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)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
DoubleStreamEx |
flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
Returns an
DoubleStream consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced
by applying the provided mapping function to each element. |
IntStreamEx |
flatMapToInt(Function<? super T,? extends IntStream> mapper)
Returns an
IntStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. |
LongStreamEx |
flatMapToLong(Function<? super T,? extends LongStream> mapper)
Returns an
LongStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. |
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)
Performs an action for each element of this stream.
|
void |
forEachOrdered(Consumer<? super T> action)
Performs an action for each element of this stream, in the encounter
order of the stream if the stream has a defined encounter order.
|
OptionalLong |
indexOf(Predicate<? super T> predicate)
Returns an
OptionalLong describing the zero-based index of the
first element of this stream, which matches given predicate, or an empty
OptionalLong if there's no matching element. |
OptionalLong |
indexOf(T element)
Returns an
OptionalLong describing the zero-based index of the
first element of this stream, which equals to the given element, or an
empty OptionalLong if there's no matching element. |
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
Iterator<T> |
iterator()
Returns an iterator for the elements of this stream.
|
S |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
<R> StreamEx<R> |
map(Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
DoubleStreamEx |
mapToDouble(ToDoubleFunction<? super T> mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
IntStreamEx |
mapToInt(ToIntFunction<? super T> mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
LongStreamEx |
mapToLong(ToLongFunction<? super T> mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
Optional<T> |
max(Comparator<? super T> comparator)
Returns the maximum element of this stream according to the provided
Comparator. |
<V extends Comparable<? super V>> |
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)
Returns the minimum element of this stream according to the provided
Comparator. |
<V extends Comparable<? super V>> |
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)
Returns whether no elements of this stream match the provided predicate.
|
S |
nonNull()
Returns a stream consisting of the elements of this stream that aren't
null.
|
S |
onClose(Runnable closeHandler)
Returns an equivalent stream with an additional close handler.
|
S |
parallel()
Returns an equivalent stream that is parallel.
|
S |
parallel(ForkJoinPool fjp)
Returns an equivalent stream that is parallel and bound to the supplied
ForkJoinPool. |
S |
peek(Consumer<? super T> action)
Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.
|
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)
Performs a reduction on the
elements of this stream, using an
associative accumulation
function, and returns an
Optional describing the reduced value,
if any. |
T |
reduce(T identity,
BinaryOperator<T> accumulator)
Performs a reduction on the
elements of this stream, using the provided identity value and an
associative
accumulation function, and returns the reduced value.
|
<U> U |
reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
Performs a reduction on the
elements of this stream, using the provided identity, accumulation and
combining functions.
|
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 provided
Comparator. |
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()
Returns an equivalent stream that is sequential.
|
S |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
S |
skipOrdered(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream even if the
stream is unordered. |
S |
sorted()
Returns a stream consisting of the elements of this stream, sorted
according to natural order.
|
S |
sorted(Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted
according to the provided
Comparator. |
<V extends Comparable<? super V>> |
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.
|
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()
Returns a spliterator for the elements of this stream.
|
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()
Returns an array containing the elements of this stream.
|
<A> A[] |
toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the
provided
generator function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing. |
<C extends Collection<T>> |
toCollection(Supplier<C> collectionFactory)
Returns a
Collection containing the elements of this stream. |
List<T> |
toImmutableList()
Returns an immutable
List containing the elements of this stream. |
Set<T> |
toImmutableSet()
Returns an immutable
Set containing the elements of this stream. |
List<T> |
toList()
Returns a
List containing the elements of this stream. |
<R> R |
toListAndThen(Function<? super List<T>,R> finisher)
Creates a
List containing the elements of this stream, then
performs finishing transformation and returns its result. |
Set<T> |
toSet()
Returns a
Set containing the elements of this stream. |
<R> R |
toSetAndThen(Function<? super Set<T>,R> finisher)
Creates a
Set containing the elements of this stream, then
performs finishing transformation and returns its result. |
S |
unordered()
Returns an equivalent stream that is
unordered.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitclose, isParallel, spliteratorspliteratorpublic Iterator<T> iterator()
java.util.stream.BaseStreamThis is a terminal operation.
public S sequential()
java.util.stream.BaseStreamThis is an intermediate operation.
sequential in interface BaseStream<T,Stream<T>>public S parallel()
This is an intermediate operation.
If this stream was created using parallel(ForkJoinPool), the new
stream forgets about supplied custom ForkJoinPool and its
terminal operation will be executed in common pool.
parallel in interface BaseStream<T,Stream<T>>public S parallel(ForkJoinPool fjp)
ForkJoinPool.
This is an intermediate operation.
The terminal operation of this stream or any derived stream (except the
streams created via parallel() or sequential() methods)
will be executed inside the supplied ForkJoinPool. If current
thread does not belong to that pool, it will wait till calculation
finishes.
fjp - a ForkJoinPool to submit the stream operation to.ForkJoinPoolpublic S unordered()
java.util.stream.BaseStreamThis is an intermediate operation.
unordered in interface BaseStream<T,Stream<T>>public S onClose(Runnable closeHandler)
java.util.stream.BaseStreamBaseStream.close() method
is called on the stream, and are executed in the order they were
added. All close handlers are run, even if earlier close handlers throw
exceptions. If any close handler throws an exception, the first
exception thrown will be relayed to the caller of close(), with
any remaining exceptions added to that exception as suppressed exceptions
(unless one of the remaining exceptions is the same exception as the
first exception, since an exception cannot suppress itself.) May
return itself.
This is an intermediate operation.
onClose in interface BaseStream<T,Stream<T>>closeHandler - A task to execute when the stream is closedpublic S filter(Predicate<? super T> predicate)
java.util.stream.StreamThis is an intermediate operation.
filter in interface Stream<T>predicate - a non-interfering,
stateless
predicate to apply to each element to determine if it
should be includedpublic <R> StreamEx<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
java.util.stream.Streamclosed after its contents
have been placed into this stream. (If a mapped stream is null
an empty stream is used, instead.)
This is an intermediate operation.
flatMap in interface Stream<T>R - The element type of the new streammapper - a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuespublic <R> StreamEx<R> map(Function<? super T,? extends R> mapper)
java.util.stream.StreamThis is an intermediate operation.
map in interface Stream<T>R - The element type of the new streammapper - a non-interfering,
stateless
function to apply to each elementpublic IntStreamEx mapToInt(ToIntFunction<? super T> mapper)
java.util.stream.StreamIntStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToInt in interface Stream<T>mapper - a non-interfering,
stateless
function to apply to each elementpublic LongStreamEx mapToLong(ToLongFunction<? super T> mapper)
java.util.stream.StreamLongStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToLong in interface Stream<T>mapper - a non-interfering,
stateless
function to apply to each elementpublic DoubleStreamEx mapToDouble(ToDoubleFunction<? super T> mapper)
java.util.stream.StreamDoubleStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToDouble in interface Stream<T>mapper - a non-interfering,
stateless
function to apply to each elementpublic IntStreamEx flatMapToInt(Function<? super T,? extends IntStream> mapper)
java.util.stream.StreamIntStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. Each mapped
stream is closed after its
contents have been placed into this stream. (If a mapped stream is
null an empty stream is used, instead.)
This is an intermediate operation.
flatMapToInt in interface Stream<T>mapper - a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)public LongStreamEx flatMapToLong(Function<? super T,? extends LongStream> mapper)
java.util.stream.StreamLongStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. Each mapped
stream is closed after its
contents have been placed into this stream. (If a mapped stream is
null an empty stream is used, instead.)
This is an intermediate operation.
flatMapToLong in interface Stream<T>mapper - a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)public DoubleStreamEx flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
java.util.stream.StreamDoubleStream consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced
by applying the provided mapping function to each element. Each mapped
stream is closed after its
contents have placed been into this stream. (If a mapped stream is
null an empty stream is used, instead.)
This is an intermediate operation.
flatMapToDouble in interface Stream<T>mapper - a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)public S distinct()
java.util.stream.StreamObject.equals(Object)) of this stream.
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.
public S distinct(Function<? super T,?> keyExtractor)
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.
keyExtractor - a non-interfering, stateless function which
classifies input elements.public S sorted()
java.util.stream.StreamComparable, a java.lang.ClassCastException may be thrown
when the terminal operation is executed.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public S sorted(Comparator<? super T> comparator)
java.util.stream.StreamComparator.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
sorted in interface Stream<T>comparator - a non-interfering,
stateless
Comparator to be used to compare stream elementspublic S peek(Consumer<? super T> action)
java.util.stream.StreamThis is an intermediate operation.
For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.
peek in interface Stream<T>action - a
non-interfering action to perform on the elements as
they are consumed from the streampublic S limit(long maxSize)
java.util.stream.StreammaxSize in length.
public S skip(long n)
java.util.stream.Streamn elements of the stream.
If this stream contains fewer than n elements then an
empty stream will be returned.
This is a stateful intermediate operation.
public void forEach(Consumer<? super T> action)
java.util.stream.StreamThis is a terminal operation.
The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
public void forEachOrdered(Consumer<? super T> action)
java.util.stream.StreamThis is a terminal operation.
This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.
forEachOrdered in interface Stream<T>action - a
non-interfering action to perform on the elementsStream.forEach(Consumer)public Object[] toArray()
java.util.stream.StreamThis is a terminal operation.
public <A> A[] toArray(IntFunction<A[]> generator)
java.util.stream.Streamgenerator function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing.
This is a terminal operation.
public T reduce(T identity, BinaryOperator<T> accumulator)
java.util.stream.Stream
T result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity value must be an identity for the accumulator
function. This means that for all t,
accumulator.apply(identity, t) is equal to t.
The accumulator function must be an
associative function.
This is a terminal operation.
reduce in interface Stream<T>identity - the identity value for the accumulating functionaccumulator - an associative,
non-interfering,
stateless
function for combining two valuespublic Optional<T> reduce(BinaryOperator<T> accumulator)
java.util.stream.StreamOptional describing the reduced value,
if any. 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();
but is not constrained to execute sequentially.
The accumulator function must be an
associative function.
This is a terminal operation.
reduce in interface Stream<T>accumulator - an associative,
non-interfering,
stateless
function for combining two valuesOptional describing the result of the reductionStream.reduce(Object, BinaryOperator),
Stream.min(Comparator),
Stream.max(Comparator)public <U> U reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
java.util.stream.Stream
U result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity value must be an identity for the combiner
function. This means that for all u, combiner(identity, u)
is equal to u. Additionally, the combiner function
must be compatible with the accumulator function; for all
u and t, the following must hold:
combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
This is a terminal operation.
reduce in interface Stream<T>U - The type of the resultidentity - the identity value for the combiner functionaccumulator - an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner - an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionStream.reduce(BinaryOperator),
Stream.reduce(Object, BinaryOperator)public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
java.util.stream.StreamArrayList, and elements are incorporated by updating
the state of the result rather than by replacing the result. This
produces a result equivalent to:
R result = supplier.get();
for (T element : this stream)
accumulator.accept(result, element);
return result;
Like Stream.reduce(Object, BinaryOperator), collect operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
collect in interface Stream<T>R - type of the resultsupplier - a function that creates a new result container. For a
parallel execution, this function may be called
multiple times and must return a fresh value each time.accumulator - an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner - an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionpublic <R,A> R collect(Collector<? super T,A,R> collector)
Collector. A Collector
encapsulates the functions used as arguments to
Stream.collect(Supplier, BiConsumer, BiConsumer), allowing for reuse of
collection strategies and composition of collect operations such as
multiple-level grouping or partitioning.
If the stream is parallel, and the Collector
is concurrent, and
either the stream is unordered or the collector is
unordered,
then a concurrent reduction will be performed (see Collector for
details on concurrent reduction.)
This is a terminal operation.
When executed in parallel, multiple intermediate results may be
instantiated, populated, and merged so as to maintain isolation of
mutable data structures. Therefore, even when executed in parallel
with non-thread-safe data structures (such as ArrayList), no
additional synchronization is needed for a parallel reduction.
If special short-circuiting collector is passed, this operation becomes short-circuiting as well.
collect in interface Stream<T>R - the type of the resultA - the intermediate accumulation type of the Collectorcollector - the Collector describing the reductionStream.collect(Supplier, BiConsumer, BiConsumer),
Collectorspublic Optional<T> min(Comparator<? super T> comparator)
java.util.stream.StreamComparator. This is a special case of a
reduction.
This is a terminal operation.
min in interface Stream<T>comparator - a non-interfering,
stateless
Comparator to compare elements of this streamOptional describing the minimum element of this stream,
or an empty Optional if the stream is emptypublic Optional<T> max(Comparator<? super T> comparator)
java.util.stream.StreamComparator. This is a special case of a
reduction.
This is a terminal operation.
max in interface Stream<T>comparator - a non-interfering,
stateless
Comparator to compare elements of this streamOptional describing the maximum element of this stream,
or an empty Optional if the stream is emptypublic long count()
java.util.stream.Stream
return mapToLong(e -> 1L).sum();
This is a terminal operation.
public boolean anyMatch(Predicate<? super T> predicate)
java.util.stream.Streamfalse is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
anyMatch in interface Stream<T>predicate - a non-interfering,
stateless
predicate to apply to elements of this streamtrue if any elements of the stream match the provided
predicate, otherwise falsepublic boolean allMatch(Predicate<? super T> predicate)
java.util.stream.Streamtrue is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
allMatch in interface Stream<T>predicate - a non-interfering,
stateless
predicate to apply to elements of this streamtrue if either all elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic boolean noneMatch(Predicate<? super T> predicate)
java.util.stream.Streamtrue is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
noneMatch in interface Stream<T>predicate - a non-interfering,
stateless
predicate to apply to elements of this streamtrue if either no elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic Optional<T> findFirst()
java.util.stream.StreamOptional describing the first element of this stream,
or an empty Optional if the stream is empty. If the stream has
no encounter order, then any element may be returned.
This is a short-circuiting terminal operation.
public Optional<T> findAny()
java.util.stream.StreamOptional describing some element of the stream, or an
empty Optional if the stream is empty.
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 Stream.findFirst() instead.)
findAny in interface Stream<T>Optional describing some element of this stream, or an
empty Optional if the stream is emptyStream.findFirst()public OptionalLong indexOf(T element)
OptionalLong describing the zero-based index of the
first element of this stream, which equals to the given element, or an
empty OptionalLong if there's no matching element.
This is a short-circuiting terminal operation.
element - an element to look forOptionalLong describing the index of the first
matching element of this stream, or an empty OptionalLong
if there's no matching element.indexOf(Predicate)public OptionalLong indexOf(Predicate<? super T> predicate)
OptionalLong describing the zero-based index of the
first element of this stream, which matches given predicate, or an empty
OptionalLong if there's no matching element.
This is a short-circuiting terminal operation.
predicate - a non-interfering ,
stateless
predicate which returned value should matchOptionalLong describing the index of the first
matching element of this stream, or an empty OptionalLong
if there's no matching element.findFirst(Predicate),
indexOf(Object)public <R> StreamEx<R> flatCollection(Function<? super T,? extends Collection<? extends R>> mapper)
null 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.
R - The element type of the new streammapper - a non-interfering ,
stateless
function to apply to each element which produces a
Collection of new valuespublic <R> StreamEx<R> flatArray(Function<? super T,? extends R[]> mapper)
null 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.
R - The element type of the new streammapper - a non-interfering ,
stateless
function to apply to each element which produces an
array of new valuespublic S remove(Predicate<? super T> predicate)
This is an intermediate operation.
predicate - a non-interfering ,
stateless
predicate to apply to each element to determine if it should be
excludedpublic S nonNull()
This is an intermediate operation.
public Optional<T> findAny(Predicate<? super T> predicate)
Optional describing some element of the stream, which
matches given predicate, or an empty Optional 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.)
predicate - a non-interfering ,
stateless
predicate which returned value should matchOptional describing some matching element of this
stream, or an empty Optional if there's no matching
elementNullPointerException - if the element selected is nullfindAny(),
findFirst(Predicate)public Optional<T> findFirst(Predicate<? super T> predicate)
Optional describing the first element of this stream,
which matches given predicate, or an empty Optional if there's no
matching element.
This is a short-circuiting terminal operation.
predicate - a non-interfering ,
stateless
predicate which returned value should matchOptional describing the first matching element of this
stream, or an empty Optional if there's no matching
elementNullPointerException - if the element selected is nullfindFirst()public S reverseSorted(Comparator<? super T> comparator)
Comparator.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
comparator - a non-interfering ,
stateless
Comparator to be used to compare stream elementspublic <V extends Comparable<? super V>> S sortedBy(Function<? super T,? extends V> keyExtractor)
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
V - the type of the Comparable sort keykeyExtractor - a non-interfering ,
stateless
function to be used to extract sorting keyspublic S sortedByInt(ToIntFunction<? super T> keyExtractor)
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
keyExtractor - a non-interfering ,
stateless
function to be used to extract sorting keyspublic S sortedByLong(ToLongFunction<? super T> keyExtractor)
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
keyExtractor - a non-interfering ,
stateless
function to be used to extract sorting keyspublic S sortedByDouble(ToDoubleFunction<? super T> keyExtractor)
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
keyExtractor - a non-interfering ,
stateless
function to be used to extract sorting keyspublic <V extends Comparable<? super V>> Optional<T> minBy(Function<? super T,? extends V> keyExtractor)
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.
V - the type of the comparable keyskeyExtractor - a non-interfering ,
stateless
function to extract the comparable keys from this stream elementsOptional describing the minimum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the minimum element is nullpublic Optional<T> minByInt(ToIntFunction<? super T> keyExtractor)
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.
keyExtractor - a non-interfering ,
stateless
function to extract the int keys from this stream elementsOptional describing the minimum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the minimum element is nullpublic Optional<T> minByLong(ToLongFunction<? super T> keyExtractor)
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.
keyExtractor - a non-interfering ,
stateless
function to extract the long keys from this stream elementsOptional describing the minimum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the minimum element is nullpublic Optional<T> minByDouble(ToDoubleFunction<? super T> keyExtractor)
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.
keyExtractor - a non-interfering ,
stateless
function to extract the double keys from this stream elementsOptional describing the minimum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the minimum element is nullpublic <V extends Comparable<? super V>> Optional<T> maxBy(Function<? super T,? extends V> keyExtractor)
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.
V - the type of the comparable keyskeyExtractor - a non-interfering ,
stateless
function to extract the comparable keys from this stream elementsOptional describing the maximum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the maximum element is nullpublic Optional<T> maxByInt(ToIntFunction<? super T> keyExtractor)
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.
keyExtractor - a non-interfering ,
stateless
function to extract the int keys from this stream elementsOptional describing the maximum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the maximum element is nullpublic Optional<T> maxByLong(ToLongFunction<? super T> keyExtractor)
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.
keyExtractor - a non-interfering ,
stateless
function to extract the long keys from this stream elementsOptional describing the maximum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the maximum element is nullpublic Optional<T> maxByDouble(ToDoubleFunction<? super T> keyExtractor)
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.
keyExtractor - a non-interfering ,
stateless
function to extract the double keys from this stream elementsOptional describing the maximum element of this
stream, or an empty Optional if the stream is emptyNullPointerException - if the maximum element is nullpublic S append(Stream<? extends T> other)
This is a quasi-intermediate operation with tail-stream optimization.
May return this if the supplied stream is known to be empty.
other - the other streamStream.concat(Stream, Stream)public S prepend(Stream<? extends T> other)
This is a quasi-intermediate operation with tail-stream optimization.
May return this if the supplied stream is known to be empty.
other - the other streamStream.concat(Stream, Stream)public List<T> toList()
List containing the elements of this stream. The
returned List is guaranteed to be mutable, but there are no
guarantees on the type, serializability, or thread-safety; if more
control over the returned List is required, use
toCollection(Supplier).
This is a terminal operation.
List containing the elements of this streamCollectors.toList(),
toImmutableList()public List<T> toImmutableList()
List containing the elements of this stream.
There's no guarantees on exact type of the returned List. The
returned List is guaranteed to be serializable if all its
elements are serializable.
This is a terminal operation.
List containing the elements of this streamtoList()public <R> R toListAndThen(Function<? super List<T>,R> finisher)
List 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 the
List created.
This is a terminal operation.
R - the type of the resultfinisher - a function to be applied to the intermediate listtoList()public Set<T> toSet()
Set containing the elements of this stream. The
returned Set is guaranteed to be mutable, but there are no
guarantees on the type, serializability, or thread-safety; if more
control over the returned Set is required, use
toCollection(Supplier).
This is a terminal operation.
Set containing the elements of this streamCollectors.toSet()public Set<T> toImmutableSet()
Set containing the elements of this stream.
There's no guarantees on exact type of the returned Set. In
particular, no specific element order in the resulting set is guaranteed.
The returned Set is guaranteed to be serializable if all its
elements are serializable.
This is a terminal operation.
Set containing the elements of this streamtoSet()public <R> R toSetAndThen(Function<? super Set<T>,R> finisher)
Set 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 the
Set created.
This is a terminal operation.
R - the result typefinisher - a function to be applied to the intermediate SetSet
of the stream elements.toSet()public <C extends Collection<T>> C toCollection(Supplier<C> collectionFactory)
Collection containing the elements of this stream. The
Collection is created by the provided factory.
This is a terminal operation.
C - the type of the resulting CollectioncollectionFactory - a Supplier which returns a new, empty
Collection of the appropriate typeCollection containing the elements of this streamCollectors.toCollection(Supplier)public <U> U foldLeft(U seed,
BiFunction<U,? super T,U> accumulator)
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.
U - The type of the resultseed - the starting valueaccumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultfoldRight(Object, BiFunction),
reduce(Object, BinaryOperator),
reduce(Object, BiFunction, BinaryOperator)public Optional<T> foldLeft(BinaryOperator<T> accumulator)
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.
accumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultfoldLeft(Object, BiFunction),
foldRight(BinaryOperator),
reduce(BinaryOperator)public <U> U foldRight(U seed,
BiFunction<? super T,U,U> accumulator)
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.
U - The type of the resultseed - the starting valueaccumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultfoldLeft(Object, BiFunction),
reduce(Object, BinaryOperator),
reduce(Object, BiFunction, BinaryOperator)public Optional<T> foldRight(BinaryOperator<T> accumulator)
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.
accumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultfoldRight(Object, BiFunction),
foldLeft(BinaryOperator),
reduce(BinaryOperator)public <U> List<U> scanLeft(U seed, BiFunction<U,? super T,U> accumulator)
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.
U - The type of the resultseed - the starting valueaccumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultList 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.foldLeft(Object, BiFunction),
scanRight(Object, BiFunction)public List<T> scanLeft(BinaryOperator<T> accumulator)
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.
accumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultList 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.foldLeft(BinaryOperator),
scanRight(BinaryOperator),
prefix(BinaryOperator)public <U> List<U> scanRight(U seed, BiFunction<? super T,U,U> accumulator)
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.
U - The type of the resultseed - the starting valueaccumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultList 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.scanLeft(Object, BiFunction),
foldRight(Object, BiFunction)public List<T> scanRight(BinaryOperator<T> accumulator)
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.
accumulator - a non-interfering ,
stateless
function for incorporating an additional element into a resultList 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.scanLeft(BinaryOperator),
foldRight(BinaryOperator)public S skipOrdered(long n)
n elements of the stream even if the
stream is unordered. If this stream contains fewer than n
elements then an empty stream will be returned.
This is a stateful quasi-intermediate operation.
Unlike skip(long) it skips the first elements even if the stream
is unordered. The main purpose of this method is to workaround the
problem of skipping the first elements from non-sized source with further
parallel processing and unordered terminal operation (such as
forEach(Consumer)). For example,
StreamEx.ofLines(br).skip(1).parallel().toSet() will skip
arbitrary line, but
StreamEx.ofLines(br).skipOrdered(1).parallel().toSet() will skip
the first one. This problem was fixed in OracleJDK 8u60.
Also it behaves much better with infinite streams processed in parallel.
For sequential streams this method behaves exactly like
skip(long).
n - the number of leading elements to skipIllegalArgumentException - if n is negativeskip(long)public S takeWhile(Predicate<? super T> predicate)
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.
predicate - a non-interfering, stateless predicate to apply to
elements.takeWhileInclusive(Predicate),
dropWhile(Predicate)public S takeWhileInclusive(Predicate<? super T> predicate)
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.
predicate - a non-interfering, stateless predicate to apply to
elements.takeWhile(Predicate)public S dropWhile(Predicate<? super T> predicate)
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.
predicate - a non-interfering, stateless predicate to apply to
elements.public S prefix(BinaryOperator<T> op)
This is a stateful quasi-intermediate operation.
This operation resembles scanLeft(BinaryOperator), but unlike
scanLeft 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.
op - an associative, non-interfering ,
stateless
function for computing the next element based on the previous onescanLeft(BinaryOperator)public <U> U chain(Function<? super S,U> mapper)
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 write
batches(StreamEx.of(input).map(...), 10).filter(...). Using the
chain() 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(...).
U - the type of the function result.mapper - function to invoke.public SPLTR spliterator()
java.util.stream.BaseStreamThis is a terminal operation.
spliterator in interface BaseStream<T,S extends BaseStream<T,S>>public boolean isParallel()
java.util.stream.BaseStreamisParallel in interface BaseStream<T,S extends BaseStream<T,S>>true if this stream would execute in parallel if executedpublic void close()
java.util.stream.BaseStreamclose in interface AutoCloseableclose in interface BaseStream<T,S extends BaseStream<T,S>>AutoCloseable.close()Copyright © 2017. All rights reserved.