Lazy Evaluation in Streams
In this lesson, we discuss how streams are evaluated lazily.
We'll cover the following
In Java 8 streams the intermediate operations are not evaluated until a terminal operation is invoked.
Each intermediate operation creates a new stream, stores the provided operation/function, and returns the new stream. When a terminal operation is called, the traversal of streams begins and the associated function is performed one by one on each element.
Lazy evaluation example
Let’s look at an example to understand how lazy evaluation helps immensely. In the below example, we have a list of 20 integers. We need to get the first number we encounter that is greater than 5 and is divisible by 3 in this list.
Get hands-on with 1400+ tech skills courses.