Constructor and Description |
---|
DelegatingStream(Stream<T> delegate)
Construct a
Stream that delegates operations to another Stream . |
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(java.util.function.Predicate<? super T> predicate)
Returns whether all elements of this stream match the provided
predicate.
|
boolean |
anyMatch(java.util.function.Predicate<? super T> predicate)
Returns whether any elements of this stream match the provided
predicate.
|
<R> R |
collect(Collector<? super T,R> collector)
Performs a mutable
reduction operation on the elements of this stream using a
Collector object to describe the reduction. |
<R> R |
collect(java.util.function.Supplier<R> resultFactory,
java.util.function.BiConsumer<R,? super T> accumulator,
java.util.function.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.
|
Stream<T> |
distinct()
Produces a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream. |
Stream<T> |
filter(java.util.function.Predicate<? super T> predicate)
Produces a stream consisting of the elements of this stream that match
the given predicate.
|
java.util.Optional<T> |
findAny()
Returns an
Optional describing some element of the stream, or an
empty Optional if the stream is empty. |
java.util.Optional<T> |
findFirst()
Returns an
Optional describing the first element of this stream
(in the encounter order), or an empty Optional if the stream is
empty. |
<R> Stream<R> |
flatMap(java.util.function.Function<? super T,? extends Stream<? extends R>> mapper)
Produces a stream consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element.
|
DoubleStream |
flatMapToDouble(java.util.function.Function<? super T,? extends DoubleStream> mapper)
Produces a
DoubleStream consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element. |
IntStream |
flatMapToInt(java.util.function.Function<? super T,? extends IntStream> mapper)
Produces an
IntStream consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element. |
LongStream |
flatMapToLong(java.util.function.Function<? super T,? extends LongStream> mapper)
Produces a
LongStream consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element. |
void |
forEach(java.util.function.Consumer<? super T> action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(java.util.function.Consumer<? super T> action)
Performs an action for each element of this stream, guaranteeing that
each element is processed in encounter order for streams that have a
defined encounter order.
|
boolean |
isParallel()
Returns whether this stream, when executed, would execute in parallel
(assuming no further modification of the stream, such as appending
further intermediate operations or changing its parallelism).
|
java.util.Iterator<T> |
iterator()
Returns an iterator for the elements of this stream.
|
Stream<T> |
limit(long maxSize)
Produces a stream consisting of the elements of this stream,
truncated to be no longer than
maxSize in length. |
<R> Stream<R> |
map(java.util.function.Function<? super T,? extends R> mapper)
Produces a stream consisting of the results of applying the given
function to the elements of this stream.
|
DoubleStream |
mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
Produces a
DoubleStream consisting of the results of applying
the given function to the elements of this stream. |
IntStream |
mapToInt(java.util.function.ToIntFunction<? super T> mapper)
Produces an
IntStream consisting of the results of applying
the given function to the elements of this stream. |
LongStream |
mapToLong(java.util.function.ToLongFunction<? super T> mapper)
Produces a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
java.util.Optional<T> |
max(java.util.Comparator<? super T> comparator)
Returns the maximal element of this stream according to the provided
Comparator . |
java.util.Optional<T> |
min(java.util.Comparator<? super T> comparator)
Returns the minimal element of this stream according to the provided
Comparator . |
boolean |
noneMatch(java.util.function.Predicate<? super T> predicate)
Returns whether no elements of this stream match the provided
predicate.
|
Stream<T> |
parallel()
Produces an equivalent stream that is parallel.
|
Stream<T> |
peek(java.util.function.Consumer<? super T> consumer)
Produces 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.
|
java.util.Optional<T> |
reduce(java.util.function.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,
java.util.function.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,
java.util.function.BiFunction<U,? super T,U> accumulator,
java.util.function.BinaryOperator<U> combiner)
Performs a reduction on the
elements of this stream, using the provided identity, accumulation
function, and a combining functions.
|
Stream<T> |
sequential()
Produces an equivalent stream that is sequential.
|
Stream<T> |
sorted()
Produces a stream consisting of the elements of this stream, sorted
according to natural order.
|
Stream<T> |
sorted(java.util.Comparator<? super T> comparator)
Produces a stream consisting of the elements of this stream, sorted
according to the provided
Comparator . |
java.util.Spliterator<T> |
spliterator()
Returns a spliterator for the elements of this stream.
|
Stream<T> |
substream(long startingOffset)
Produces a stream consisting of the remaining elements of this stream
after discarding the first
startingOffset elements (or all
elements if the stream has fewer than startingOffset elements). |
Stream<T> |
substream(long startingOffset,
long endingOffset)
Produces a stream consisting of the elements of this stream after
discarding the first
startingOffset elements (or all elements
if the stream has fewer than startingOffset elements), and
truncating the remainder to be no longer than maxSize in length. |
java.lang.Object[] |
toArray()
Produces an array containing the elements of this stream.
|
<A> A[] |
toArray(java.util.function.IntFunction<A[]> generator)
Produces an array containing the elements of this stream, using the
provided
generator function to allocate the returned array. |
Stream<T> |
unordered()
Produces an equivalent stream that is
unordered.
|
public java.util.Spliterator<T> spliterator()
This is a terminal operation.
public boolean isParallel()
public java.util.Iterator<T> iterator()
This is a terminal operation.
public Stream<T> filter(java.util.function.Predicate<? super T> predicate)
Stream
This 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> Stream<R> map(java.util.function.Function<? super T,? extends R> mapper)
Stream
This 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 IntStream mapToInt(java.util.function.ToIntFunction<? super T> mapper)
Stream
IntStream
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 LongStream mapToLong(java.util.function.ToLongFunction<? super T> mapper)
Stream
LongStream
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 DoubleStream mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
Stream
DoubleStream
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 <R> Stream<R> flatMap(java.util.function.Function<? super T,? extends Stream<? extends R>> mapper)
Stream
null
, this is treated as if the
result is an empty stream.
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 IntStream flatMapToInt(java.util.function.Function<? super T,? extends IntStream> mapper)
Stream
IntStream
consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element. If the result
of the mapping function is null
, this is treated as if the
result is an empty stream.
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 valuespublic LongStream flatMapToLong(java.util.function.Function<? super T,? extends LongStream> mapper)
Stream
LongStream
consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element. If the result
of the mapping function is null
, this is treated as if the
result is an empty stream.
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 valuespublic DoubleStream flatMapToDouble(java.util.function.Function<? super T,? extends DoubleStream> mapper)
Stream
DoubleStream
consisting of the results of replacing each
element of this stream with the contents of the stream produced
by applying the provided mapping function to each element. If the result
of the mapping function is null
, this is treated as if the
result is an empty stream.
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 valuespublic Stream<T> distinct()
Stream
Object.equals(Object)
) of this stream.
This is a stateful intermediate operation.
public Stream<T> sorted()
Stream
Comparable
, a java.lang.ClassCastException
may be thrown when the stream pipeline is executed.
This is a stateful intermediate operation.
public Stream<T> sorted(java.util.Comparator<? super T> comparator)
Stream
Comparator
.
This is a stateful intermediate operation.
sorted
in interface Stream<T>
comparator
- A
non-interfering, stateless Comparator
to
be used to compare stream elementspublic void forEach(java.util.function.Consumer<? super T> action)
Stream
This is a terminal operation.
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.
forEach
in interface Stream<T>
action
- A
non-interfering action to perform on the elementspublic void forEachOrdered(java.util.function.Consumer<? super T> action)
Stream
This is a terminal operation.
forEachOrdered
in interface Stream<T>
action
- A
non-interfering action to perform on the elementsStream.forEach(Consumer)
public Stream<T> peek(java.util.function.Consumer<? super T> consumer)
Stream
This 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>
consumer
- A
non-interfering action to perform on the elements as
they are consumed from the streampublic Stream<T> limit(long maxSize)
Stream
maxSize
in length.
public Stream<T> substream(long startingOffset)
Stream
startingOffset
elements (or all
elements if the stream has fewer than startingOffset
elements).
This is a stateful intermediate operation.
public Stream<T> substream(long startingOffset, long endingOffset)
Stream
startingOffset
elements (or all elements
if the stream has fewer than startingOffset
elements), and
truncating the remainder to be no longer than maxSize
in length.
public <A> A[] toArray(java.util.function.IntFunction<A[]> generator)
Stream
generator
function to allocate the returned array.
This is a terminal operation.
public java.lang.Object[] toArray()
Stream
This is a terminal operation.
public T reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
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 java.util.Optional<T> reduce(java.util.function.BinaryOperator<T> accumulator)
Stream
Optional
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 valuesStream.reduce(Object, BinaryOperator)
public <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
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(java.util.function.Supplier<R> resultFactory, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)
Stream
ArrayList
, 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 = resultFactory.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 sychronization.
This is a terminal operation.
collect
in interface Stream<T>
R
- Type of the resultresultFactory
- 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> R collect(Collector<? super T,R> collector)
Stream
Collector
object to describe the reduction. 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.
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.
collect
in interface Stream<T>
R
- The type of the resultcollector
- The Collector
describing the reductionStream.collect(Supplier, BiConsumer, BiConsumer)
,
Collectors
public java.util.Optional<T> max(java.util.Comparator<? super T> comparator)
Stream
Comparator
. 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 use to compare
elements of this streamOptional
describing the maximal element of this stream,
or an empty Optional
if the stream is emptypublic java.util.Optional<T> min(java.util.Comparator<? super T> comparator)
Stream
Comparator
. 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 use to compare
elements of this streamOptional
describing the minimal element of this stream,
or an empty Optional
if the stream is emptypublic long count()
Stream
This is a terminal operation.
public boolean anyMatch(java.util.function.Predicate<? super T> predicate)
Stream
This is a short-circuiting terminal operation.
anyMatch
in interface Stream<T>
predicate
- A non-interfering,
stateless predicate to apply to elements of this
streampublic boolean allMatch(java.util.function.Predicate<? super T> predicate)
Stream
This is a short-circuiting terminal operation.
allMatch
in interface Stream<T>
predicate
- A non-interfering,
stateless predicate to apply to elements of this streampublic boolean noneMatch(java.util.function.Predicate<? super T> predicate)
Stream
This is a short-circuiting terminal operation.
noneMatch
in interface Stream<T>
predicate
- A non-interfering,
stateless predicate to apply to elements of this streampublic java.util.Optional<T> findFirst()
Stream
Optional
describing the first element of this stream
(in the encounter order), or an empty Optional
if the stream is
empty. If the stream has no encounter order, than any element may be
returned.
This is a short-circuiting terminal operation.
public java.util.Optional<T> findAny()
Stream
Optional
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 the first element
in the encounter order 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 Stream<T> unordered()
This is an intermediate operation.
public Stream<T> sequential()
This is an intermediate operation.
public Stream<T> parallel()
This is an intermediate operation.