Gatherers WindowSliding Overlapping Windows

Slide a fixed-size window across a stream with Gatherers.windowSliding so every adjacent pair shares a window, unlike windowFixed non-overlapping batches.

JavaData Fetching

Code

snippet
import java.util.List;
import java.util.stream.Gatherers;

public class LatencyWindow {

    public static void main(String[] args) {
        List<Integer> latencies = List.of(42, 18, 133, 7, 95, 61);

        // windowFixed:切出不重叠的定长批次,6 个元素、窗口 2 → 3 批
        List<List<Integer>> batches = latencies.stream()
                .gather(Gatherers.windowFixed(2))
                .toList();
        System.out.println("fixed  : " + batches);

        // windowSliding:窗口逐元素滑动,6 个元素、窗口 3 → 4 个重叠窗口
        List<List<Integer>> sliding = latencies.stream()
                .gather(Gatherers.windowSliding(3))
                .toList();
        System.out.println("sliding: " + sliding);

        // 相邻差值这类计算天然需要 overlapping 窗口:每对相邻元素共处一窗
        List<Integer> deltas = latencies.stream()
                .gather(Gatherers.windowSliding(2))
                .map(w -> w.get(1) - w.get(0))
                .toList();
        System.out.println("deltas : " + deltas);
    }
}

How it works

Structurally, expect the class LatencyWindow and the function main and nothing else.

The transformation runs as a stream pipeline — source, intermediate operations, terminal collector — so each stage stays independently readable.

What to watch for

  • The toolchain alone is enough — no dependency resolution happens.

Using it

The definition reads public static void main(String[] args)

Expect labeled output — fixed :, sliding:, deltas : — when the file runs.

Key statements

List<List<Integer>> batches = latencies.stream()
        .gather(Gatherers.windowFixed(2))
        .toList();
List<List<Integer>> sliding = latencies.stream()
        .gather(Gatherers.windowSliding(3))
        .toList();
List<Integer> deltas = latencies.stream()
        .gather(Gatherers.windowSliding(2))
        .map(w -> w.get(1) - w.get(0))
        .toList();

FAQ

What does Gatherers WindowSliding Overlapping Windows print when run?
Running the file directly is enough — expect labeled output lines such as `fixed :`, `sliding:`, `deltas :`.