Lazy evaluation of stream

You must hear about Stream are lazy. What is the mean of lazy in Java Stream? In this article, we will discuss the java lazy stream or lazy nature of Stream and how it streams works.

Here is the table content of the article will we will cover this topic.
1. Eager loading or early loading?
2. Lazy loading or lazy processing?
3. How Stream is lazy?
4. How intermediate operations and terminal operations work together with Lazy
evaluation?

5. Why Stream support laziness or the Advantage of laziness?

Eager loading or early loading

In eager loading, the operations are always performed before the action. Let’s take an example of a result of Exams. A student wants to search the result for a specific roll number. What if we are loading all students’ results without any input? It means we are doing eager loading.

While you are dealing with bigger data, we should not use the eager loading. Because in eager loading, we are not sure how the processed data will be used. It will always process the entire amount of data at the cost of performance.

Lazy loading or lazy processing

In lazy processing, the operations are performed after the action. It is processing the data only on demand.

How Stream is lazy?

It is an important characteristic of streams because the operation on the source data is only performed when the terminal operation is initiated. It doesn’t consume the source elements as in eager loading, the source elements are consumed only on demand.
As you have seen in Stream operations:
1. The intermediate stream doesn’t produce the result. They create a new Stream
only.
2. The terminal operation produces the result.

How intermediate operations and terminal operations work together with Lazy evaluation?

1. The intermediate operations are lazy in nature and only executed when a result of
processing is needed.
2. Intermediate operations don’t produce the result. It creates a new stream.
3. Intermediate operations are not evaluated until the terminal operations are not
initiated.
4. When a terminal operation is initiated, the traversal of streams starts, and the
the associated function is performed one by one.

import java.util.Arrays;
import java.util.List;

public class ExampleOfLazy {
	public static void main(String[] args) 
	{
		List<Integer> listOfNumber = Arrays.asList(10, 5, 6, 11, 8);

		listOfNumber.stream().filter((a) -> a > 5).forEach(System.out::println);
	}

}

Output: 10
6
11
8

java lazy stream

Let’s take another example of Stream. In this example, we are printing the number from the stream.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class ExampleOfLazy 
{
	public static void main(String[] args) 
	{
		List<Integer> listOfNumber = Arrays.asList(1, 2, 3, 4, 5);
		
		Stream<Integer> stream = listOfNumber.stream();
		
		// Calling an Intermediate operation
		Stream<Integer> newStream = stream.map((number) -> printData(number));
		
		// Calling an Terminal operation
		newStream.forEach(a -> 
		System.out.println("Calling by Terminal operation: "+ a));
	}
	 public static Integer printData(Object number)
	 {
		 System.out.println("Calling by Intermediate operation : "+ number);
		 return (Integer) number;
	 }
}

Output: Calling by Terminal operation: 1
Calling by Intermediate operation: 1
Calling by Terminal operation: 2
Calling by Intermediate operation: 2
Calling by Terminal operation : 3
Calling by Intermediate operation: 3
Calling by Terminal operation: 4
Calling by Intermediate operation: 4
Calling by Terminal operation: 5
Calling by Intermediate operation: 5

In this example we are using two operations: One is the intermediate operation and another is a terminal operation.

Why java lazy stream support laziness or the Advantage of laziness?

It is a very common and tricky question. So now we will discuss the role of laziness.

Let’s take the example of a shopping site. In a shopping site, you must see all products are not loaded at a time.  The product loads according to user input, it doesn’t load all products with input. What if, the site is delaying the complete loading of the entire product list. If the site loads all the products, the response time would increase, and the user might get distracted.

By use of Lazy operations, you can achieve efficiency. Lazy operations are useful when input data is consumed gradually rather than having a whole complete set of elements beforehand.

For example, you want to get the data from the remote server. Where you have an infinite stream. In this situation server, calls should be made at a terminal operation when it’s needed.