J.IV · Java 25 practice question
What does the following Java 25 program print?
import java.util.*;
import java.util.stream.*;
void main() {
Map<Integer, List<String>> grouped = Stream.of("a", "bb", "c")
.collect(Collectors.groupingBy(String::length));
IO.println(grouped.get(1));
}
Check answer and explanation
Correct answer: [a, c]
Explanation
Collectors.groupingBy(String::length) groups input strings by their length into a Map<Integer, List<String>>, so key 1 maps to the list [a, c].
Common trap
This would only occur if all elements mapped to key 1, ignoring that "bb" has length 2. This is the list mapped to key 2, not key 1.
Source: Java SE 25 API: Collectors.groupingBy
Checked against the cited text on 2026-11-25.