Java Reference
In-Depth Information
Figure 4.5. Intermediate vs. terminal operations
4.4.1. Intermediate operations
Intermediate operations such as filter or sorted return another stream as the return type. This
allows the operations to be connected to form a query. What's important is that intermediate
operations don't perform any processing until a terminal operation is invoked on the stream
pipeline—they're lazy. This is because intermediate operations can usually be merged and
processed into a single pass by the terminal operation.
To understand what's happening in the stream pipeline, modify the code so each lambda also
prints the current dish it's processing (like many demonstration and debugging techniques, this
is appalling programming style for production code but directly explains the order of evaluation
when you're learning):
This code when executed will print the following:
filtering pork
mapping pork
 
Search WWH ::




Custom Search