From 3d99ba9c817ce0106a0ecbadafb8d6df92950d67 Mon Sep 17 00:00:00 2001 From: soyeon1806 Date: Mon, 27 Jul 2026 02:03:59 +0900 Subject: [PATCH] Fix broken sliding window logic in SlidingWindowGatherer Commit aae2714723 removed the `state.size() == 3` guard from the integrator. Without it, every element is pushed downstream and then immediately dropped, so the gatherer emits one single-element list per input instead of overlapping windows of three. For the stream 1..5, SlidingWindowGathererUnitTest expects [[1, 2, 3], [2, 3, 4], [3, 4, 5]], but the current code produces [[1], [2], [3], [4], [5]] and fails on the window-count assertion. Restoring the guard also lets the finisher() override go away: it returned an empty BiConsumer, which is exactly what the default finisher of Gatherer already does. Refs #18683 --- .../streams/gatherer/SlidingWindowGatherer.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/core-java-modules/core-java-streams-7/src/main/java/com/baeldung/streams/gatherer/SlidingWindowGatherer.java b/core-java-modules/core-java-streams-7/src/main/java/com/baeldung/streams/gatherer/SlidingWindowGatherer.java index 713d88e0e2a1..90d5b2eed747 100644 --- a/core-java-modules/core-java-streams-7/src/main/java/com/baeldung/streams/gatherer/SlidingWindowGatherer.java +++ b/core-java-modules/core-java-streams-7/src/main/java/com/baeldung/streams/gatherer/SlidingWindowGatherer.java @@ -1,7 +1,6 @@ package com.baeldung.streams.gatherer; import java.util.*; -import java.util.function.BiConsumer; import java.util.function.Supplier; import java.util.stream.Gatherer; @@ -18,15 +17,12 @@ public Integrator, Integer, List> integrator() { @Override public boolean integrate(Deque state, Integer element, Downstream> downstream) { state.addLast(element); - downstream.push(new ArrayList<>(state)); - state.removeFirst(); + if (state.size() == 3) { + downstream.push(new ArrayList<>(state)); + state.removeFirst(); + } return true; } }; } - - @Override - public BiConsumer, Downstream>> finisher() { - return (state, downstream) -> {}; - } }