What does this print?
List<String> names = List.of("Ana", "Bob", "Alicia", "Carl");
List<String> r = names.stream()
.filter(n -> n.startsWith("A"))
.map(String::toUpperCase)
.toList();
System.out.println(r);Options
- [Ana, Alicia]
- [ANA, ALICIA] (Correct option)
- [ANA, BOB, ALICIA, CARL]
- [A, A]
Answer
[ANA, ALICIA]
Why
A Stream is a pipeline for processing a collection: source, then intermediate steps, then a final (terminal) step.
filter keeps names starting with "A" (Ana, Alicia). map(String::toUpperCase) converts each to upper case. toList() collects the result.
The original list is not modified. n -> n.startsWith("A") is a lambda: a short anonymous function.
Remember
Stream = source, intermediate ops (filter/map), terminal op (toList/collect).