Streams advanced

Stream gatherers

Use gatherers for custom intermediate stream operations.

✕ Java 8
// Sliding window: manual implementation
List<List<T>> windows = new ArrayList<>();
for (int i = 0; i <= list.size()-3; i++) {
    windows.add(
        list.subList(i, i + 3));
}
✓ Java 24+
var windows = stream
    .gather(
        Gatherers.windowSliding(3)
    )
    .toList();
🧩

Composable

Gatherers compose with other stream operations.

📦

Built-in operations

windowFixed, windowSliding, fold, scan out of the box.

🔧

Extensible

Write custom gatherers for any intermediate transformation.

Old Approach
Custom Collector
Modern Approach
gather()
Since JDK
24
Difficulty
advanced
Stream gatherers
Available

Finalized in JDK 24 (JEP 485, March 2025).

How it works

Gatherers are a new intermediate stream operation that can express complex transformations like sliding windows, fixed-size groups, and scan operations that were impossible with standard stream ops.

Share 𝕏 🦋 in