Skip to main content
Java Code Completion Hard +35 XP

Stream API

Complete the stream operation.

JavaFill in the blank
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
int sum = nums.stream()
    .filter(n -> n > 2)
    .___((a, b) -> a + b)
    .orElse(0);

Type the missing code

Show answer and explanation

Answer: reduce

reduce() combines stream elements into a single result using an accumulator function.

Hint: Which terminal operation combines elements?

What this challenge tests

Code Completion in Java: streams, functional.

Related challenges