Skip to content

Commit 1a1362a

Browse files
committed
JEP 473: Stream Gatherers
1 parent 76d5c9c commit 1a1362a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ This repository contains Java examples that are designed to track and document t
1515
* [JEP 474](java-23/src/main/java/com/ibrahimatay/JEP474ZGCGenerationalModeByDefault.sh): ZGC: Generational Mode by Default
1616
* [JEP 476](java-23/src/main/java/com/ibrahimatay/JEP476ModuleImportDeclarations.java): Module Import Declarations]
1717
* [JEP 477](java-23/src/main/java/com/ibrahimatay/JEP477ImplicitlyDeclaredClassesAndInstanceMainMethods.java): Implicitly Declared Classes and Instance Main Methods
18-
18+
* API Improvements
19+
* [JEP 473](java-23/src/main/java/com/ibrahimatay/StreamGathererMethod.java): Stream Gatherers
20+
1921
* [Java 22](java-22/) (March, 2024)
2022
* [JEP 458](java-22/src/main/java/com/ibrahimatay/JEP458LaunchMultiFileSourceCode.java): Launch Multi-File Source-Code Programs
2123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.ibrahimatay;
2+
3+
import java.util.List;
4+
import java.util.stream.Gatherers;
5+
import java.util.stream.Stream;
6+
7+
public class StreamGathererMethod {
8+
public static void main(String[] args) {
9+
// JEP 473: Stream Gatherers (Second Preview)
10+
// https://openjdk.org/jeps/473
11+
12+
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
13+
14+
List<List<Integer>> pairedEvenSquares = numbers.stream()
15+
.filter(n -> n % 2 == 0)
16+
.map(n -> n * n)
17+
.gather(Gatherers.windowFixed(2))
18+
.toList();
19+
20+
System.out.println(pairedEvenSquares);
21+
// [[4, 16], [36, 64], [100]]
22+
23+
List<List<Integer>> windowsOfThree
24+
= Stream.of(0,1,2,3,4,5,6,7)
25+
.gather(Gatherers.windowFixed(3))
26+
.toList();
27+
28+
System.out.println(windowsOfThree);
29+
// [[0, 1, 2], [3, 4, 5], [6, 7]]
30+
}
31+
}

0 commit comments

Comments
 (0)