Top Description Inners Methods
java.util.stream

public Interface Stream<T>

extends BaseStream<T, Stream<T>>
Known Direct Implementers
java.util.stream.ReferencePipeline
Type Parameters
<T>
the type of the stream elements
Imports
jdk.internal.javac.PreviewFeature, java.nio.file.Files, .Path, java.util.*, java.util.concurrent.ConcurrentHashMap, java.util.function.BiConsumer, .BiFunction, .BinaryOperator, .Consumer, .DoubleConsumer, .Function, .IntConsumer, .IntFunction, .LongConsumer, .Predicate, .Supplier, .ToDoubleFunction, .ToIntFunction, .ToLongFunction, .UnaryOperator

A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream:
int sum = widgets.stream()
                     .filter(w -> w.getColor() == RED)
                     .mapToInt(w -> w.getWeight())
                     .sum();
In this example, widgets is a Collection<Widget>. We create a stream of Widget objects via Collection.stream(), filter it to produce a stream containing only the red widgets, and then transform it into a stream of int values representing the weight of each red widget. Then this stream is summed to produce a total weight.

In addition to Stream, which is a stream of object references, there are primitive specializations for IntStream, LongStream, and DoubleStream, all of which are referred to as "streams" and conform to the characteristics and restrictions described here.

To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as Stream#filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as Stream#count() or Stream#forEach(Consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

A stream implementation is permitted significant latitude in optimizing the computation of the result. For example, a stream implementation is free to elide operations (or entire stages) from a stream pipeline -- and therefore elide invocation of behavioral parameters -- if it can prove that it would not affect the result of the computation. This means that side-effects of behavioral parameters may not always be executed and should not be relied upon, unless otherwise specified (such as by the terminal operations forEach and forEachOrdered). (For a specific example of such an optimization, see the API note documented on the count operation. For more detail, see the side-effects section of the stream package documentation.)

Collections and streams, while bearing some superficial similarities, have different goals. Collections are primarily concerned with the efficient management of, and access to, their elements. By contrast, streams do not provide a means to directly access or manipulate their elements, and are instead concerned with declaratively describing their source and the computational operations which will be performed in aggregate on that source. However, if the provided stream operations do not offer the desired functionality, the iterator() and spliterator() operations can be used to perform a controlled traversal.

A stream pipeline, like the "widgets" example above, can be viewed as a query on the stream source. Unless the source was explicitly designed for concurrent modification (such as a ConcurrentHashMap), unpredictable or erroneous behavior may result from modifying the stream source while it is being queried.

Most stream operations accept parameters that describe user-specified behavior, such as the lambda expression w -> w.getWeight() passed to mapToInt in the example above. To preserve correct behavior, these behavioral parameters:

Such parameters are always instances of a functional interface such as java.util.function.Function, and are often lambda expressions or method references. Unless otherwise specified these parameters must be non-null.

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases.

Streams have a close() method and implement AutoCloseable. Operating on a stream after it has been closed will throw IllegalStateException. Most stream instances do not actually need to be closed after use, as they are backed by collections, arrays, or generating functions, which require no special resource management. Generally, only streams whose source is an IO channel, such as those returned by Files#lines(Path), will require closing. If a stream does require closing, it must be opened as a resource within a try-with-resources statement or similar control structure to ensure that it is closed promptly after its operations have completed.

Stream pipelines may execute either sequentially or in parallel. This execution mode is a property of the stream. Streams are created with an initial choice of sequential or parallel execution. (For example, Collection.stream() creates a sequential stream, and Collection.parallelStream() creates a parallel one.) This choice of execution mode may be modified by the sequential() or parallel() methods, and may be queried with the isParallel() method.

Since
1.8
See Also
IntStream, LongStream, DoubleStream, java.util.stream

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Stream.Builder<
the type of stream elements
T
>

A mutable builder for a Stream.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false
allMatch
(Predicate<? super T>
a non-interfering, stateless predicate to apply to elements of this stream
predicate
)

Returns whether all elements of this stream match the provided predicate.

public boolean

Returns:

true if any elements of the stream match the provided predicate, otherwise false
anyMatch
(Predicate<? super T>
a non-interfering, stateless predicate to apply to elements of this stream
predicate
)

Returns whether any elements of this stream match the provided predicate.

public static <
type of elements
T
>
Stream.Builder<T>

Returns:

a stream builder
builder
()

Returns a builder for a Stream.

public <
the type of the mutable result container
R
>
R

Returns:

the result of the reduction
collect
(Supplier<R>
a function that creates a new mutable result container. For a parallel execution, this function may be called multiple times and must return a fresh value each time.
supplier
,
BiConsumer<R, ? super T>
an associative, non-interfering, stateless function that must fold an element into a result container.
accumulator
,
BiConsumer<R, R>
an associative, non-interfering, stateless function that accepts two partial result containers and merges them, which must be compatible with the accumulator function. The combiner function must fold the elements from the second result container into the first result container.
combiner
)

Performs a mutable reduction operation on the elements of this stream.

public <
the type of the result
R
,
the intermediate accumulation type of the Collector
A
>
R

Returns:

the result of the reduction
collect
(Collector<? super T, A, R>
the Collector describing the reduction
collector
)

Performs a mutable reduction operation on the elements of this stream using a Collector.

public static <
The type of stream elements
T
>
Stream<T>

Returns:

the concatenation of the two input streams
concat
(Stream<? extends T>
the first stream
a
,
Stream<? extends T>
the second stream
b
)

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

public long

Returns:

the count of elements in this stream
count
()

Returns the count of elements in this stream.

public Stream<T>

Returns:

the new stream
distinct
()

Returns a stream consisting of the distinct elements (according to Object#equals(Object)) of this stream.

public default Stream<T>

Returns:

the new stream
dropWhile
(Predicate<? super T>
a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.
predicate
)

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

an empty sequential stream
empty
()

Returns an empty sequential Stream.

public Stream<T>

Returns:

the new stream
filter
(Predicate<? super T>
a non-interfering, stateless predicate to apply to each element to determine if it should be included
predicate
)

Returns a stream consisting of the elements of this stream that match the given predicate.

public Optional<T>

Returns:

an Optional describing some element of this stream, or an empty Optional if the stream is empty
findAny
()

Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

public Optional<T>

Returns:

an Optional describing the first element of this stream, or an empty Optional if the stream is empty
findFirst
()

Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.

public <
The element type of the new stream
R
>
Stream<R>

Returns:

the new stream
flatMap
(Function<? super T, ? extends Stream<? extends R>>
a non-interfering, stateless function to apply to each element which produces a stream of new values
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.

public DoubleStream

Returns:

the new stream
flatMapToDouble
(Function<? super T, ? extends DoubleStream>
a non-interfering, stateless function to apply to each element which produces a stream of new values
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.

public IntStream

Returns:

the new stream
flatMapToInt
(Function<? super T, ? extends IntStream>
a non-interfering, stateless function to apply to each element which produces a stream of new values
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.

public LongStream

Returns:

the new stream
flatMapToLong
(Function<? super T, ? extends LongStream>
a non-interfering, stateless function to apply to each element which produces a stream of new values
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.

public void
forEach(Consumer<? super T>
a non-interfering action to perform on the elements
action
)

Performs an action for each element of this stream.

public void
forEachOrdered(Consumer<? super T>
a non-interfering action to perform on the elements
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.

public default <
The element type of the new stream
R
>
Stream<R>

Returns:

the new stream
gather
(Gatherer<? super T, ?, R>
a gatherer
gatherer
)
Preview Second Preview of Stream Gatherers (JEP 473).

Returns a stream consisting of the results of applying the given Gatherer to the elements of this stream.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

a new infinite sequential unordered Stream
generate
(Supplier<? extends T>
the Supplier of generated elements
s
)

Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

a new sequential Stream
iterate
(final T
the initial element
seed
,
final UnaryOperator<T>
a function to be applied to the previous element to produce a new element
f
)

Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

The first element (position 0) in the Stream will be the provided seed.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

a new sequential Stream
iterate
(T
the initial element
seed
,
Predicate<? super T>
a predicate to apply to elements to determine when the stream must terminate.
hasNext
,
UnaryOperator<T>
a function to be applied to the previous element to produce a new element
next
)

Returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.

public Stream<T>

Returns:

the new stream
limit
(long
the number of elements the stream should be limited to
maxSize
)

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

public <
The element type of the new stream
R
>
Stream<R>

Returns:

the new stream
map
(Function<? super T, ? extends R>
a non-interfering, stateless function to apply to each element
mapper
)

Returns a stream consisting of the results of applying the given function to the elements of this stream.

public default <
The element type of the new stream
R
>
Stream<R>

Returns:

the new stream
mapMulti
(BiConsumer<? super T, ? super Consumer<R>>
a non-interfering, stateless function that generates replacement elements
mapper
)

Returns a stream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.

public default DoubleStream

Returns:

the new stream
mapMultiToDouble
(BiConsumer<? super T, ? super DoubleConsumer>
a non-interfering, stateless function that generates replacement elements
mapper
)

Returns a DoubleStream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.

public default IntStream

Returns:

the new stream
mapMultiToInt
(BiConsumer<? super T, ? super IntConsumer>
a non-interfering, stateless function that generates replacement elements
mapper
)

Returns an IntStream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.

public default LongStream

Returns:

the new stream
mapMultiToLong
(BiConsumer<? super T, ? super LongConsumer>
a non-interfering, stateless function that generates replacement elements
mapper
)

Returns a LongStream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.

public DoubleStream

Returns:

the new stream
mapToDouble
(ToDoubleFunction<? super T>
a non-interfering, stateless function to apply to each element
mapper
)

Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.

public IntStream

Returns:

the new stream
mapToInt
(ToIntFunction<? super T>
a non-interfering, stateless function to apply to each element
mapper
)

Returns an IntStream consisting of the results of applying the given function to the elements of this stream.

public LongStream

Returns:

the new stream
mapToLong
(ToLongFunction<? super T>
a non-interfering, stateless function to apply to each element
mapper
)

Returns a LongStream consisting of the results of applying the given function to the elements of this stream.

public Optional<T>

Returns:

an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
max
(Comparator<? super T>
a non-interfering, stateless Comparator to compare elements of this stream
comparator
)

Returns the maximum element of this stream according to the provided Comparator.

public Optional<T>

Returns:

an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
min
(Comparator<? super T>
a non-interfering, stateless Comparator to compare elements of this stream
comparator
)

Returns the minimum element of this stream according to the provided Comparator.

public boolean

Returns:

true if either no elements of the stream match the provided predicate or the stream is empty, otherwise false
noneMatch
(Predicate<? super T>
a non-interfering, stateless predicate to apply to elements of this stream
predicate
)

Returns whether no elements of this stream match the provided predicate.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

a singleton sequential stream
of
(T
the single element
t
)

Returns a sequential Stream containing a single element.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

the new stream
of
(T...
the elements of the new stream
values
)

Returns a sequential ordered stream whose elements are the specified values.

public static <
the type of stream elements
T
>
Stream<T>

Returns:

a stream with a single element if the specified element is non-null, otherwise an empty stream
ofNullable
(T
the single element
t
)

Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.

public Stream<T>

Returns:

the new stream
peek
(Consumer<? super T>
a non-interfering action to perform on the elements as they are consumed from the stream
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.

public T

Returns:

the result of the reduction
reduce
(T
the identity value for the accumulating function
identity
,
BinaryOperator<T>
an associative, non-interfering, stateless function for combining two values
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.

public Optional<T>

Returns:

an Optional describing the result of the reduction
reduce
(BinaryOperator<T>
an associative, non-interfering, stateless function for combining two values
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.

public <
The type of the result
U
>
U

Returns:

the result of the reduction
reduce
(U
the identity value for the combiner function
identity
,
BiFunction<U, ? super T, U>
an associative, non-interfering, stateless function for incorporating an additional element into a result
accumulator
,
BinaryOperator<U>
an associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function
combiner
)

Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.

public Stream<T>

Returns:

the new stream
skip
(long
the number of leading elements to skip
n
)

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.

public Stream<T>

Returns:

the new stream
sorted
()

Returns a stream consisting of the elements of this stream, sorted according to natural order.

public Stream<T>

Returns:

the new stream
sorted
(Comparator<? super T>
a non-interfering, stateless Comparator to be used to compare stream elements
comparator
)

Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

public default Stream<T>

Returns:

the new stream
takeWhile
(Predicate<? super T>
a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.
predicate
)

Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.

public Object[]

Returns:

an array, whose runtime component type is Object, containing the elements of this stream
toArray
()

Returns an array containing the elements of this stream.

public <
the component type of the resulting array
A
>
A[]

Returns:

an array containing the elements in this stream
toArray
(IntFunction<A[]>
a function which produces a new array of the desired type and the provided length
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.

public default List<T>

Returns:

a List containing the stream elements
toList
()

Accumulates the elements of this stream into a List.

Inherited from java.util.stream.BaseStream:
closeisParalleliteratoronCloseparallelsequentialspliteratorunordered