Intermediate Operation
Intermediate operation are lazy load. It then can follow up with the Terminal Operation or other Intermediate Operation
Intermediate operation does not execute immediately. Notes that stream only execute at Terminal Operation. The intermediate operation is only lazy load.
For example, consider the following code
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Integer> list = List.of(1,2,5,6,9,10,-1,2);
System.out.println(String.format("number %s", list.stream().filter(x -> condition(x)).findFirst().get()));
}
public static boolean condition(int number) {
System.out.println("Calling with number: " + number);
return number > 5;
}
}
The output then would be
Calling with number: 1
Calling with number: 2
Calling with number: 5
Calling with number: 6
number 6
See how it doesn't keep going up and it just stops once the thing is done.
If we change our condition to be
public static boolean condition(int number) {
System.out.println("Calling with number: " + number);
return number < 5;
}
It will be
Calling with number: 1
number 1