T - Type of elements.public interface Stream<T>
filter and map, and consuming
operations, such as forEach, findFirst, and iterator. Once an operation has been
performed on a stream, it is considered consumed and no longer usable for other operations.
For sequential stream pipelines, all operations are performed respecting in the encounter order of the source, if the source has a defined encounter order. For parallel stream pipelines, unless otherwise specified, intermediate stream operations preserve the encounter order of the source, and terminal operations respect the encounter order of the source, if the source has an encounter order.
| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(Predicate<? super T> predicate)
Return whether all elements of this stream match the provided
Predicate. |
boolean |
anyMatch(Predicate<? super T> predicate)
Return whether any elements of this stream match the provided
Predicate. |
<R> R |
collect(Collector<? super T,R> collector)
Perform a mutable reduction operation on the elements of this stream using a
Collector
object to describe the reduction. |
<R> R |
collect(Supplier<R> resultFactory,
BiConsumer<R,? super T> accumulator,
BiConsumer<R,R> combiner)
Perform a mutable reduction operation on the elements of this stream.
|
<R> R |
collectUnordered(Collector<? super T,R> collector) |
Stream<T> |
distinct()
Transform this stream into one consisting of the distinct elements (according to
Object.equals(Object)) of this stream. |
Stream<T> |
filter(Predicate<? super T> predicate)
Transform this stream into one consisting of the elements that match the given
Predicate. |
Optional<T> |
findAny()
Return an
Optional describing some element of the stream, or an
empty Optional if the stream is empty. |
Optional<T> |
findFirst()
Return an
Optional describing the first element of this stream, or an
empty Optional if the stream is empty. |
DoubleStream |
flatMap(FlatMapper.ToDouble<? super T> mapper)
Transform this stream into a
DoubleStream where each element is replaced with zero or more transformed
values, according to the transformation encoded in the provided FlatMapper.OfDouble. |
IntStream |
flatMap(FlatMapper.ToInt<? super T> mapper)
Transform this stream into an
IntStream where each element is replaced with zero or more transformed
values, according to the transformation encoded in the provided FlatMapper.OfInt. |
LongStream |
flatMap(FlatMapper.ToLong<? super T> mapper)
Transform this stream into a
LongStream where each element is replaced with zero or more transformed
values, according to the transformation encoded in the provided FlatMapper.OfLong. |
<R> Stream<R> |
flatMap(FlatMapper<? super T,R> mapper)
Transform this stream into one where each element is replaced with zero or more transformed values,
according to the transformation encoded in the provided
FlatMapper. |
default <R> Stream<R> |
flatMap(Function<T,Stream<? extends R>> mapper)
Transform this stream into one where each element is replaced with the contents of the stream produced
by applying the provided @{code Function} to that element.
|
void |
forEach(Consumer<? super T> consumer)
Perform an operation for each element of this stream.
|
void |
forEachUntilCancelled(Consumer<? super T> consumer,
BooleanSupplier cancelledFunction)
Perform an operation for each element of this stream, attempting to stop early if some external termination
condition is reached.
|
int |
getStreamFlags()
Return the composition of stream flags of the stream source and all intermediate operations.
|
boolean |
isParallel()
Returns whether this stream, when executed, will execute in parallel
|
default Iterator<T> |
iterator()
Return an iterator for the elements of this stream.
|
Stream<T> |
limit(long maxSize)
Produce a
Stream consisting of the elements of this stream, discarding any elements after
the maxSize'th elements. |
<R> Stream<R> |
map(Function<? super T,? extends R> mapper)
Transform this stream into one consisting of the result of applying the given
Function to the
elements of this stream. |
DoubleStream |
map(ToDoubleFunction<? super T> mapper)
Transform this stream into a
DoubleStream consisting of the result of applying the given function
to the elements of this stream. |
IntStream |
map(ToIntFunction<? super T> mapper)
Transform this stream into an
IntStream consisting of the result of applying the given function
to the elements of this stream. |
LongStream |
map(ToLongFunction<? super T> mapper)
Transform this stream into a
LongStream consisting of the result of applying the given function
to the elements of this stream. |
default Optional<T> |
max(Comparator<? super T> comparator)
Return the maximal element of this stream according to the provided
Comparator. |
default Optional<T> |
min(Comparator<? super T> comparator)
Return the minimal element of this stream according to the provided
Comparator. |
boolean |
noneMatch(Predicate<? super T> predicate)
Return whether no elements of this stream match the provided
Predicate. |
S |
parallel()
Produce a stream which has the same contents as this stream, but is a parallel stream.
|
Stream<T> |
peek(Consumer<? super T> consumer)
Produce a
Stream containing the elements of this stream, and also provide elements
to the specified Consumer as elements are consumed from the resulting stream. |
Optional<T> |
reduce(BinaryOperator<T> reducer)
Perform a reduction on the elements of this stream using the provided identity value and
an associative reducing function, and return an
Optional describing the reduced value, if any. |
T |
reduce(T identity,
BinaryOperator<T> reducer)
Perform a reduction on the elements of this stream using the provided identity value and
an associative reducing function, and return the reduced value.
|
<U> U |
reduce(U identity,
BiFunction<U,? super T,U> reducer,
BinaryOperator<U> combiner)
Perform a reduction on the elements of this stream, using the provided identity and combining
functions.
|
S |
sequential()
Produce a stream which has the same contents as this stream, but is a sequential stream.
|
Stream<T> |
sorted()
Transform this stream into one consisting of the elements of this stream sorted according to natural
order.
|
Stream<T> |
sorted(Comparator<? super T> comparator)
Transform this stream into one consisting of the elements of this stream sorted according to the provided
Comparator. |
Spliterator<T> |
spliterator()
Return a spliterator for the elements of this stream.
|
Stream<T> |
substream(long startingOffset)
Produce a
Stream containing the contents of this stream, but discarding the first startingOffset
elements. |
Stream<T> |
substream(long startingOffset,
long endingOffset)
Produce a
Stream that is a subsequence of this stream, discarding the first startingOffset
elements of this stream, and discarding any elements after the endingOffset'th element of this stream. |
default Object[] |
toArray()
Produce an array containing the elements of this stream.
|
<A> A[] |
toArray(IntFunction<A[]> generator)
Produce an array containing the elements of this stream, using the provided
generator function
to allocate the returned array. |
Stream<T> filter(Predicate<? super T> predicate)
Predicate. This is
an intermediate operation.predicate - The test criteria to apply to each element to determine if it should be included in the output<R> Stream<R> map(Function<? super T,? extends R> mapper)
Function to the
elements of this stream. This is an intermediate operation.mapper - The function to be applied to each elementIntStream map(ToIntFunction<? super T> mapper)
IntStream consisting of the result of applying the given function
to the elements of this stream. This is an intermediate operation.mapper - The function to be applied to each elementLongStream map(ToLongFunction<? super T> mapper)
LongStream consisting of the result of applying the given function
to the elements of this stream. This is an intermediate operation.mapper - The function to be applied to each elementDoubleStream map(ToDoubleFunction<? super T> mapper)
DoubleStream consisting of the result of applying the given function
to the elements of this stream. This is an intermediate operation.mapper - The function to be applied to each elementdefault <R> Stream<R> flatMap(Function<T,Stream<? extends R>> mapper)
mapper - The function to be applied to each element, resulting in a Stream of new values<R> Stream<R> flatMap(FlatMapper<? super T,R> mapper)
FlatMapper. This is
an intermediate operation.mapper - The FlatMapper that transforms each element into zero or more resulting valuesIntStream flatMap(FlatMapper.ToInt<? super T> mapper)
IntStream where each element is replaced with zero or more transformed
values, according to the transformation encoded in the provided FlatMapper.OfInt. This is
an intermediate operation.mapper - The FlatMapper that transforms each element into zero or more resulting values#flatMap(FlatMapper)}LongStream flatMap(FlatMapper.ToLong<? super T> mapper)
LongStream where each element is replaced with zero or more transformed
values, according to the transformation encoded in the provided FlatMapper.OfLong. This is
an intermediate operation.mapper - The FlatMapper that transforms each element into zero or more resulting values#flatMap(FlatMapper)}DoubleStream flatMap(FlatMapper.ToDouble<? super T> mapper)
DoubleStream where each element is replaced with zero or more transformed
values, according to the transformation encoded in the provided FlatMapper.OfDouble. This is
an intermediate operation.mapper - The FlatMapper that transforms each element into zero or more resulting values#flatMap(FlatMapper)}Stream<T> distinct()
Object.equals(Object)) of this stream. This is
a stateful intermediate operation.Stream<T> sorted()
Comparable, a java.lang.ClassCastException
may be thrown when the stream pipeline is executed. This is
a stateful intermediate operation.Stream<T> sorted(Comparator<? super T> comparator)
Comparator. This is
a stateful intermediate operation.comparator - A Comparator to be used to compare stream elementsvoid forEach(Consumer<? super T> consumer)
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 operation may be performed at whatever time and in whatever thread the framework chooses. If the operation accesses shared state, it is responsible for providing the required synchronization.
consumer - The Consumer to receive the elementsvoid forEachUntilCancelled(Consumer<? super T> consumer, BooleanSupplier cancelledFunction)
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 operation may be performed at whatever time and in whatever thread the framework chooses. If the operation accesses shared state, it is responsible for providing the required synchronization.
consumer - The Consumer to receive the elementscancelledFunction - A BooleanSupplier that indicates whether the termination criteria has occurred.
Once it returns @{code true} the first time, it must continue to return true
for all future invocationsStream<T> peek(Consumer<? super T> consumer)
Stream containing the elements of this stream, and also provide elements
to the specified Consumer as elements are consumed from the resulting stream. This is
an intermediate operation.
This method exists mainly to support debugging, where you want to see the elements as they flow past a certain
point in a pipeline:
list.stream()
.filter(filteringFunction)
.peek(e -> {System.out.println("Filtered value: " + e); });
.map(mappingFunction)
.peek(e -> {System.out.println("Mapped value: " + e); });
.collect(Collectors.intoList());
For parallel stream pipelines, the Consumer may be called at whatever time and in whatever thread
the element is made available by the upstream operation. If the Consumer modifies shared state,
it is responsible for providing the required synchronization.
consumer - The Consumer to receive the elementsStream<T> limit(long maxSize)
Stream consisting of the elements of this stream, discarding any elements after
the maxSize'th elements. This is
a short-circuiting stateful intermediate operation.maxSize - the number elements the stream should be limited to.Stream<T> substream(long startingOffset)
Stream containing the contents of this stream, but discarding the first startingOffset
elements. This is
a stateful intermediate operation.startingOffset - the number of elements to be skipped.Stream<T> substream(long startingOffset, long endingOffset)
Stream that is a subsequence of this stream, discarding the first startingOffset
elements of this stream, and discarding any elements after the endingOffset'th element of this stream.
This is a short-circuiting stateful intermediate operation.startingOffset - the starting position of the substream, inclusiveendingOffset - the ending position of the substream, exclusivedefault Object[] toArray()
<A> A[] toArray(IntFunction<A[]> generator)
generator function
to allocate the returned array. This is
a terminal operation.A - the type of elements in resulting the arraygenerator - a function which produces a new array of the desired type and the provided lengthArrayStoreException - if the runtime type of an array returned from the array generator
is not a supertype of the runtime type of every element in this streamT reduce(T identity, BinaryOperator<T> reducer)
T result = identity;
for (T element : this stream)
result = reducer.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity value must be an identity for the reducing function. This means that for all t,
reducer(identity, t) is equal to t. The reducer must be an associative function.
This is a terminal operation.
identity - The identity value for the reducing functionreducer - An associative function for combining two elementsOptional<T> reduce(BinaryOperator<T> reducer)
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 = reducer.apply(result, element)
return foundAny ? Optional.of(result) : Optional.empty();
but is not constrained to execute sequentially.
The reducer must be an associative function.
This is a terminal operation.
reducer - An associative function for combining two elementsreduce(Object, BinaryOperator)<U> U reduce(U identity,
BiFunction<U,? super T,U> reducer,
BinaryOperator<U> combiner)
U result = identity;
for (T element : this stream)
result = reducer.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 reducer function; for all u and t, the following must hold:
combiner(u, reducer(identity, t)) == reducer(u, t)
This is a terminal operation.
U - The type of the resultidentity - The identity value for the combiner functionreducer - An function for combining two resultscombiner - A function for incorporating an additional element into a result, which must be compatible
with the reducer functionreduce(BinaryOperator),
reduce(Object, BinaryOperator)<R> R collect(Supplier<R> resultFactory, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
ArrayList, and elements are incorporated
by updating the state of the result rather than replacing the result. This is equivalent to:
R result = resultFactory.get();
for (T element : this stream)
accumulator.accept(result, element);
return result;
but is not constrained to execute sequentially.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 - Function to incorporate a new value into a result container.combiner - Function to incorporate the state of one result container into another<R> R collect(Collector<? super T,R> collector)
Collector
object to describe the reduction. A Collector encapsulates the functions used as arguments
to collect(Supplier, BiConsumer, BiConsumer), allowing for reuse of collection strategies
and composition of collect operations such as multiple-level group-by.R - The type of the resultcollector - The Collector describing the reductioncollect(Supplier, BiConsumer, BiConsumer),
Collectorsdefault Optional<T> max(Comparator<? super T> comparator)
Comparator. This is
a terminal operation.comparator - The Comparator to use to compare elements of this streamdefault Optional<T> min(Comparator<? super T> comparator)
Comparator. This is
a terminal operation.comparator - The Comparator to use to compare elements of this streamboolean anyMatch(Predicate<? super T> predicate)
Predicate. May not evaluate the
predicate on all elements if not necessary for determining the result. This is
a short-circuiting terminal operation.predicate - The predicate to apply to elements of this streamboolean allMatch(Predicate<? super T> predicate)
Predicate. May not evaluate the
predicate on all elements if not necessary for determining the result. This is
a short-circuiting terminal operation.predicate - The predicate to apply to elements of this streamboolean noneMatch(Predicate<? super T> predicate)
Predicate. May not evaluate the
predicate on all elements if not necessary for determining the result. This is
a short-circuiting terminal operation.predicate - The predicate to apply to elements of this streamOptional<T> findFirst()
Optional describing the first element of this stream, or an
empty Optional if the stream is empty. This is
a short-circuiting terminal operation.Optional if the stream has no elementsOptional<T> findAny()
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 findFirst() instead.)
Optional if the stream has no elementsfindFirst()default Iterator<T> iterator()
Spliterator<T> spliterator()
boolean isParallel()
int getStreamFlags()
StreamOpFlagS sequential()
S parallel()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.
DRAFT ea-b00