Skip to content

Commit 98985af

Browse files
feat: added another stream gatherer example
1 parent a4d6ca4 commit 98985af

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Java 23 feature examples
55
* maven-javadoc-plugin version 3.10.1 not working
66
* Module Import Declaration
77
* Primitive Types in Patterns, instanceof, and switch (Preview)
8-
* Stream Gatherer (Preview)
8+
* Stream Gatherer (Preview)
9+
* Flexible Constructor Bodies

src/main/java/de/claudioaltamura/java23/streamgatherer/StreamGathererExample.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.function.Function;
55
import java.util.stream.Gatherer;
6+
import java.util.stream.Gatherers;
67

78
public class StreamGathererExample {
89

@@ -21,4 +22,10 @@ public List<Integer> toLengths(List<String> words) {
2122
.toList();
2223
}
2324

25+
public List<List<String>> groupsOfThree(List<String> words) {
26+
return words.stream()
27+
.gather(Gatherers.windowFixed(3))
28+
.toList();
29+
}
30+
2431
}

src/test/java/de/claudioaltamura/java23/streamgatherer/StreamGathererExampleTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ void toLengths() {
1717

1818
assertThat(result).containsAll(List.of(4,2,1,4));
1919
}
20+
21+
@Test
22+
void testeGroupOfThree() {
23+
final var streamGathererExample = new StreamGathererExample();
24+
25+
final List<List<String>> result = streamGathererExample.groupsOfThree(List.of("Hello", "beautiful", "world", "You", "are", "nice"));
26+
27+
assertThat(result).hasSize(2);
28+
assertThat(result.getFirst()).contains("Hello", "beautiful", "world");
29+
}
2030
}

0 commit comments

Comments
 (0)