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);
xs.stream().map(x -> {
System.out.print(x + " ");
return x * 2;
});
System.out.println("end");
}
}end
- Expecting
1 2 3 end:maponly adds a step to the pipeline; its lambda runs once a terminal operation such astoListstarts it, and here there is none. - Expecting the stream to run later on its own: laziness means the work waits for a terminal operation, and without one it never happens.
- What does the program print if
.toList()is added aftermap? - In what order do
filterandmapsee the elements once a terminal operation runs?