Back to questions
JuniorTheoryCommon
What does the program print?
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> xs = List.of(1, 2, 3, 4, 5);
List<Integer> r = xs.stream()
.filter(x -> x % 2 == 1)
.map(x -> x * 10)
.toList();
System.out.println(r);
}
}[10, 30, 50]
- Expecting
[20, 40]:filterkeeps the elements for which the predicate returnstrue, here the odd ones, and drops the rest. - Expecting
mapto see all five numbers: each step works on the output of the step before it, somapgets only 1, 3 and 5.
- What does
System.out.println(xs)print after this pipeline, and why? - How do you get the sum of these numbers instead of a list?