public final class Gatherers extends Object
Gatherer that provide useful intermediate operations, such as windowing functions, folding functions, transforming elements concurrently, etc.| Modifier and Type | Method | Description |
|---|---|---|
static <T, |
fold |
Returns a Gatherer that performs an ordered, reduction-like, transformation for scenarios where no combiner-function can be implemented, or for reductions which are intrinsically order-dependent. |
static <T, |
mapConcurrent |
An operation which executes a function concurrently with a configured level of max concurrency, using virtual threads. |
static <T, |
scan |
Returns a Gatherer that performs a Prefix Scan -- an incremental accumulation -- using the provided functions. |
static <TR> Gatherer |
windowFixed |
Returns a Gatherer that gathers elements into windows -- encounter-ordered groups of elements -- of a fixed size. |
static <TR> Gatherer |
windowSliding |
Returns a Gatherer that gathers elements into windows -- encounter-ordered groups of elements -- of a given size, where each subsequent window includes all elements of the previous window except for the least recent, and adds the next element in the stream. |
public static <TR> Gatherer<TR,?,List<TR>> windowFixed(int windowSize)
Example:
// will contain: [[1, 2, 3], [4, 5, 6], [7, 8]]
List<List<Integer>> windows =
Stream.of(1,2,3,4,5,6,7,8).gather(Gatherers.windowFixed(3)).toList();
UnsupportedOperationException to be thrown. There are no guarantees on the implementation type or serializability of the produced Lists.TR - the type of elements the returned gatherer consumes and the contents of the windows it produceswindowSize - the size of the windowsIllegalArgumentException - when windowSize is less than 1public static <TR> Gatherer<TR,?,List<TR>> windowSliding(int windowSize)
Example:
// will contain: [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8]]
List<List<Integer>> windows2 =
Stream.of(1,2,3,4,5,6,7,8).gather(Gatherers.windowSliding(2)).toList();
// will contain: [[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8]]
List<List<Integer>> windows6 =
Stream.of(1,2,3,4,5,6,7,8).gather(Gatherers.windowSliding(6)).toList();
UnsupportedOperationException to be thrown. There are no guarantees on the implementation type or serializability of the produced Lists.TR - the type of elements the returned gatherer consumes and the contents of the windows it produceswindowSize - the size of the windowsIllegalArgumentException - when windowSize is less than 1public static <T,R> Gatherer<T,?,R> fold(Supplier<R> initial, BiFunction<? super R, ? super T, ? extends R> folder)
Example:
// will contain: Optional["123456789"]
Optional<String> numberString =
Stream.of(1,2,3,4,5,6,7,8,9)
.gather(
Gatherers.fold(() -> "", (string, number) -> string + number)
)
.findFirst();
T - the type of elements the returned gatherer consumesR - the type of elements the returned gatherer producesinitial - the identity value for the fold operationfolder - the folding functionNullPointerException - if any of the parameters are null
public static <T,R> Gatherer<T,?,R> scan(Supplier<R> initial, BiFunction<? super R, ? super T, ? extends R> scanner)
Supplier, each subsequent value is obtained by applying the BiFunction to the current value and the next input element, after which the resulting value is produced downstream. Example:
// will contain: ["1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789"]
List<String> numberStrings =
Stream.of(1,2,3,4,5,6,7,8,9)
.gather(
Gatherers.scan(() -> "", (string, number) -> string + number)
)
.toList();
T - the type of element which this gatherer consumesR - the type of element which this gatherer producesinitial - the supplier of the initial value for the scannerscanner - the function to apply for each elementNullPointerException - if any of the parameters are null
public static <T,R> Gatherer<T,?,R> mapConcurrent(int maxConcurrency, Function<? super T, ? extends R> mapper)
RuntimeException, after which any remaining tasks are canceled.T - the type of inputR - the type of outputmaxConcurrency - the maximum concurrency desiredmapper - a function to be executed concurrentlyIllegalArgumentException - if maxConcurrency is less than 1NullPointerException - if mapper is null
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Gatherers.html