I have a Stream
of objects, of which the first part is to be handled differently from the second part (a simple test will tell when to switch).
I've tried to create the stream and use it first with a takeWhile
and then handle the rest of it:
Stream s = [...];
s.takeWhile(simplePredicate).forEach(this::handleFirstPart);
s.forEach(this::handleRest);
It doesn't work: it throws java.lang.IllegalStateException: stream has already been operated upon or closed
.
Why can't I keep using the stream after takeWhile
decides to stop its processing?
Is there a way to do it?