Skip to content
CodeKruPro

When does a stream filter actually run?

Predict the outputMedium-HardJava only
Main.java
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4);
        Stream<Integer> evens = nums.stream().filter(n -> {
            System.out.println("check " + n);
            return n % 2 == 0;
        });
        System.out.println("before");
        System.out.println(evens.findFirst().get());
    }
}

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in