J.IV · Java 25 practice question
What does the following Java 25 program print when the stream has fewer elements than the window size?
import java.util.stream.*;
void main() {
var windows = Stream.of("x", "y")
.gather(Gatherers.windowSliding(5))
.toList();
IO.println(windows);
}
Check answer and explanation
Correct answer: [[x, y]]
Explanation
When a non-empty stream is smaller than the requested window size, Gatherers.windowSliding emits a single window containing all elements in the stream.
Common trap
It is tempting to assume no full window can be formed, but the API specifies emitting one window containing all available elements. windowSliding never pads incomplete windows with null values.
Source: Java SE 25 API: Gatherers.windowSliding
Checked against the cited text on 2026-11-25.