← Quiz: Java Streams in Depth: all 10 questions
Quiz: Java Streams in Depth · Question 4 of 10
What does this print? (code)
What does this print?
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
List<List<Integer>> a = List.of(List.of(1, 2), List.of(3), List.of());
System.out.println(a.stream().flatMap(List::stream).mapToInt(Integer::intValue).sum());
}
}
- A.3
- B.6
- C.[1, 2, 3]
- D.Compile error
Answer & explanation
Correct answer: 6
flatMap joins the three lists into one stream 1, 2, 3; their sum is 6. The empty list adds nothing.
Can you get the rest right without peeking?
Take the timed Quiz: Java Streams in Depth quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →