0

How to get square of the first even number greater than 3 from the list

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

public class Main {

    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

        int asInt = numbers.stream()
                           .filter(i -> i > 2)
                           .filter(i -> i % 2 == 0)
                           .mapToInt(i -> i * i)
                           .findFirst()
                           .orElse(0);

        System.out.println(asInt);
    }
}

Output:

16
add comment
Suggest a different solution or add another example