Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions JavaReleases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ This project includes unit tests for key functionalities introduced in each Java
- [Java 21™](src/test/java/pl/mperor/lab/java/Java21.java)
- [Java 22™](src/test/java/pl/mperor/lab/java/Java22.java)
- [Java 23™](src/test/java/pl/mperor/lab/java/Java23.java)
- [Java 24™](src/test/java/pl/mperor/lab/java/Java24.java)
- [Java 25™](src/test/java/pl/mperor/lab/java/Java25.java)

For detailed examples and tests of each feature, please refer to the individual source files linked above.

Expand Down
27 changes: 26 additions & 1 deletion JavaReleases/src/test/java/pl/mperor/lab/java/Java24.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Gatherer;
import java.util.stream.Gatherers;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/// Java 24 (March 2025)
/// [JDK 24](https://openjdk.org/projects/jdk/24)
Expand Down Expand Up @@ -51,4 +55,25 @@ public void testWindowFixedGatherer() {
List.of(3)
), result);
}
}

@Test
public void testCreateGatherer() {
var result = Stream.of("one", "two", "three", "four", "five")
.gather(distinctBy(String::length))
.toList();

Assertions.assertEquals(List.of("one", "three", "four"), result);
}

/// Keeps only the first element for each key produced by `extractor`.
private static <T, P> Gatherer<T, ?, T> distinctBy(Function<? super T, ? extends P> extractor) {
return Gatherer.ofSequential(HashSet<P>::new, Gatherer.Integrator.ofGreedy(
(seen, element, downstream) -> {
if (seen.add(extractor.apply(element))) {
return downstream.push(element);
}
return true;
}
));
}
}
Loading