Java Stream Reuse – Consume Stream Multiple Times?

Java streams cannot be reused. Once a terminal operation is performed on a stream, it is considered consumed and cannot be reused.

java 8

One of the main features of Java Streams is that they are designed to be consumed only once. Once a terminal operation is performed on a stream, it is considered consumed and cannot be reused. But many times, we need to perform different operations on the same stream.

So, is it possible to reuse streams in Java? Learn the alternative of Java stream reuse and how to handle situations where we need to consume a stream multiple times.

1. Can we reuse the Stream? The answer is ‘NO’.

Java streams, once consumed, can not be reused by default. As Java docs say clearly,

“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.

So the simple answer is: NO, we cannot reuse the streams or traverse the streams multiple times. Any attempt to do so will result in an error: Stream has already been operated on or closed.

2. Why the Streams cannot be Reused?

The streams are not reusable due to their internal structure and the nature of their operations, which are generally designed to be consumed and processed in a single pass.

If we observe its internals, a Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Conceptually, a stream can be thought of as a pipeline of computational steps. The intermediate steps process the elements and return a new Stream, whereas the terminal steps trigger the actual processing of the elements.

In runtime, streams maintain the internal state during their execution. Once a terminal operation is performed, the internal state is consumed and the stream is considered closed. Allowing reuse would require additional mechanisms to reset the internal state and reinitialize the source which would complicate the design and reduce performance.

2. Alternative to Reuse a Stream

First of all, any implementation code that requires traversing the stream multiple times – isn’t efficient code and needs to be refactored.

The only usecase where we might want to create a source and get stream multiple times is – unit testing. In that case, we can always use the stream() method to create a new stream.

List<Integer> tokens = Arrays.asList(1, 2, 3, 4, 5);
 
//first use
Optional<Integer> result = tokens.stream().max(Integer::compareTo);
System.out.println(result.get());
 
//second use
result = tokens.stream().min(Integer::compareTo);
System.out.println(result.get());
 
//third use
long count = tokens.stream().count();
System.out.println(count);

Program output.

5
1
5

Happy Learning !!

Reference: Java Stream Interface

Comments

Subscribe
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.