Skip to content

Commit 2c5b075

Browse files
committed
JEP 487: Scoped Values
1 parent 5452fa0 commit 2c5b075

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This repository contains Java examples that are designed to track and document t
1212
* [Java 24](java-24/) (March, 2025)
1313
* [JEP 488](java-24/src/main/java/com/ibrahimatay/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
1414
* [JEP 495](java-24/src/main/java/com/ibrahimatay/JEP495SimpleSourceFilesAndInstanceMainMethods.java): Simple Source Files and Instance Main Methods
15+
* [JEP 485](java-24/src/main/java/com/ibrahimatay/JEP485StreamGatherers.java): Stream Gatherers
16+
* [JEP 487](java-24/src/main/java/com/ibrahimatay/JEP487ScopedValues.java): Scoped Values
1517

1618
* [Java 23](java-23/) (September, 2024)
1719
* [JEP 455](java-23/src/main/java/com/ibrahimatay/JEP455PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.concurrent.ThreadLocalRandom;
2+
import java.util.stream.Gatherers;
3+
import java.util.stream.Stream;
4+
5+
public class JEP485StreamGatherers {
6+
public static void main(String[] args) {
7+
// Stream Gatherers (JEP 485)
8+
// https://openjdk.java.net/jeps/485
9+
10+
// Custom Intermediate Operation distinctBy
11+
Stream.of("foo", "bar", "baz", "quux")
12+
.gather(Gatherers.distinctBy(String::length))
13+
.toList();
14+
15+
// Creating Fixed-size Windows
16+
Stream.iterate(0, i -> i + 1)
17+
.gather(Gatherers.windowFixed(3))
18+
.limit(2)
19+
.toList();
20+
21+
// Parallel Processing with selectOne
22+
Stream.generate(() -> ThreadLocalRandom.current().nextInt())
23+
.limit(1000)
24+
.parallel()
25+
.gather(Gatherers.selectOne(Math::max))
26+
.findFirst();
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import static java.lang.ScopedValue.where;
2+
3+
public class JEP487ScopedValues {
4+
// Define a scoped value
5+
private static final ScopedValue<String> USER_NAME = ScopedValue.newInstance();
6+
7+
public static void main(String[] args) {
8+
// JEP 487: Scoped Values (Fourth Preview)
9+
// https://openjdk.org/jeps/487
10+
11+
where(USER_NAME, "John").run(() -> {
12+
System.out.println("User: " + USER_NAME.get());
13+
subMethod();
14+
});
15+
}
16+
17+
public static void subMethod() {
18+
// Access scoped value within a sub-method
19+
System.out.println("User in subMethod: " + USER_NAME.get());
20+
}
21+
}

java-24/src/main/java/JEP492FlexibleConstructorBodies.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)