Why is takeWhile stateful?

Viewed 288

The Javadoc states that

This is a short-circuiting stateful intermediate operation.

Definition of stateful from Javadoc:

Stateful operations, such as distinct and sorted, may incorporate state from previously seen elements when processing new elements. Stateful operations may need to process the entire input before producing a result. For example, one cannot produce any results from sorting a stream until one has seen all elements of the stream. As a result, under parallel computation, some pipelines containing stateful intermediate operations may require multiple passes on the data or may need to buffer significant data. Pipelines containing exclusively stateless intermediate operations can be processed in a single pass, whether sequential or parallel, with minimal data buffering.

How is default Stream<T> takeWhile​(Predicate<? super T> predicate) stateful?It does not need look at the entire input, etc...

It's almost like filter but short-circuiting.

2 Answers

Well, takeWhile should process the longest prefix of the Stream that satisfies the given Predicate. This means that in order to know if a given element of the Stream should be processed by takeWhile, you may have to process all the elements preceding it.

Hence, you need to know the state of the processing of the previous elements of the Stream in order to know how to process the current element.

In sequential Streams you don't have to keep state, since once you reach the first element that doesn't match the Predicate, you know you are done.

In parallel Streams, however, this becomes much trickier.

It is stateful in that it changes its behavior based on internal state (whether it has already seen an element matching the predicate). It does not process elements independently from each other. This may disable certain optimizations and may reduce the usefulness of processing in parallel.

So it is stateful in the same way limit and skip are stateful - the outcome does not (only) depend on the current element, but also on elements preceding it.

Related