In using Java streams in a project, I attempted to re-use a stream as such.
I start with a collection of objects, then do some filtering.
Collection<MyClass> collection = /* form the collection */;
Stream<MyClass> myStream = collection.stream().filter(/* some filter */);
I then want to re-use this same stream multiple times. For instance, first I want to just get the first item from the stream, as such.
MyClass first = myStream.findFirst().get();
Then I do some other stuff, and later I want to use the filtered myStream
again to perform an operation on each object in the stream.
myStream.forEach(/* do stuff */);
However, when I try to do this, I get this error.
java.lang.IllegalStateException: stream has already been operated upon or closed
I can solve the issue by doing one of the following:
- Create a new stream from the original collection and filter it again
- Collect the stream into a filtered collection, then create a stream on that
So I guess there are a few questions I have based on my findings.
- If you cannot re-use streams, then when would it ever be useful to return an instance of a stream for later use?
- Can streams be cloned so that they can be re-used without causing an
IllegalStateException
?