Are Java streams stages sequential?ExplanationBehaviour of stateful and stateless intermediate operations

2022-09-03 00:17:47

I have a question on the intermediate stages sequential state - are the operations from a stage applied to all the input stream (items) or are all the stages / operations applied to each stream item?

I'm aware the question might not be easy to understand, so I'll give an example. On the following stream processing:

List<String> strings = Arrays.asList("Are Java streams intermediate stages sequential?".split(" "));
strings.stream()
           .filter(word -> word.length() > 4)
           .peek(word -> System.out.println("f: " + word))
           .map(word -> word.length())
           .peek(length -> System.out.println("m: " + length))
           .forEach(length -> System.out.println("-> " + length + "\n"));

My expectation for this code is that it will output:

f: streams
f: intermediate
f: stages
f: sequential?

m: 7
m: 12
m: 6
m: 11

-> 7
-> 12
-> 6
-> 11

Instead, the output is:

f: streams
m: 7
-> 7

f: intermediate
m: 12
-> 12

f: stages
m: 6
-> 6

f: sequential?
m: 11
-> 11

Are the items just displayed for all the stages, due to the console output? Or are they also processed for all the stages, one at a time?

I can further detail the question, if it's not clear enough.


答案 1

This behaviour enables optimisation of the code. If each intermediate operation were to process all elements of a stream before proceeding to the next intermediate operation then there would be no chance of optimisation.

So to answer your question, each element moves along the stream pipeline vertically one at a time (except for some stateful operations discussed later), therefore enabling optimisation where possible.

Explanation

Given the example you've provided, each element will move along the stream pipeline vertically one by one as there is no stateful operation included.

Another example, say you were looking for the first whose length is greater than , processing all the elements prior to providing the result is unnecessary and time-consuming.String4

Consider this simple illustration:

List<String> stringsList = Arrays.asList("1","12","123","1234","12345","123456","1234567");
int result = stringsList.stream()
                        .filter(s -> s.length() > 4)
                        .mapToInt(Integer::valueOf)
                        .findFirst().orElse(0);

The intermediate operation above will not find all the elements whose length is greater than and return a new stream of them but rather what happens is as soon as we find the first element whose length is greater than , that element goes through to the which then says "I've found the first element" and execution stops there. Therefore the result will be .filter44.mapToIntfindFirst12345

Behaviour of stateful and stateless intermediate operations

Note that when a stateful intermediate operation as such of is included in a stream pipeline then that specific operation will traverse the entire stream. If you think about it, this makes complete sense as in order to sort elements you'll need to see all the elements to determine which elements come first in the sort order.sorted

The intermediate operation is also a stateful operation, however, as @Holger has mentioned unlike , it does not require traversing the entire stream as each distinct element can get passed down the pipeline immediately and may fulfil a short-circuiting condition.distinctsorted

stateless intermediate operations such as , etc do not have to traverse the entire stream and can freely process one element at a time vertically as mentioned above.filtermap

Lastly, but not least it's also important to note that, when the terminal operation is a short-circuiting operation the terminal-short-circuiting methods can finish before traversing all the elements of the underlying stream.

reading: Java 8 stream tutorial


答案 2

Your answer is . What we see is that the four intermediate operations have been logically joined together to constitute a single pass. They are executed in order for each of the individual element. This joining together of operations in a single pass is an optimization technique known as .loop fusionfilter() – peek() – map() – peek() – println using forEach() which is a kinda terminal operationloop fusion

More for reading: Source


推荐