Skip to content

Commit 82b4246

Browse files
committed
Add gatherer for fixedwindow
1 parent 9adba76 commit 82b4246

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/test/java/com/github/streams/practice/gatherers/ScanTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.streams.practice.gatherers;
2+
3+
import java.util.List;
4+
import java.util.stream.Gatherers;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.condition.EnabledOnJre;
8+
import org.junit.jupiter.api.condition.JRE;
9+
10+
/// The windowFixed gatherer is an intermediate stream operation used to partition an input stream
11+
// into a stream of non-overlapping, fixed-size lists (or "windows"). This is also commonly known as
12+
// chunking or batching a stream.
13+
///
14+
/// The gatherer ensures that every element is consumed by exactly one window.
15+
///
16+
///
17+
class WindowFixedTest {
18+
@Test
19+
@EnabledOnJre(JRE.JAVA_25)
20+
void windowFixedTest() {
21+
final List<Integer> input = List.of(1, 2, 3, 4, 5, 6, 7, 8);
22+
final int windowSize = 2;
23+
24+
final var output = input.stream().gather(Gatherers.windowFixed(windowSize)).toList();
25+
final var expected = List.of(List.of(1, 2), List.of(3, 4), List.of(5, 6), List.of(7, 8));
26+
27+
Assertions.assertEquals(expected, output);
28+
}
29+
}

0 commit comments

Comments
 (0)