APIdia Launch Partner
Top Description Inners Constructors Methods
java.util.stream

public final Class Gatherers

extends Object
Class Inheritance
Imports
jdk.internal.access.SharedSecrets, jdk.internal.vm.annotation.ForceInline, java.util.ArrayDeque, .List, .Objects, java.util.concurrent.Callable, .ExecutionException, .Future, .FutureTask, .Semaphore, java.util.function.BiConsumer, .BiFunction, .BinaryOperator, .Function, .Supplier, java.util.stream.Gatherer.Integrator, .Gatherer.Downstream

Implementations of Gatherer that provide useful intermediate operations, such as windowing functions, folding functions, transforming elements concurrently, etc.
Since
24

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Gatherers.Composite<T, A, R, AA, RR>

pack-priv static record
pack-priv static enum

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <
the type of elements the returned gatherer consumes
T
,
the type of elements the returned gatherer produces
R
>
Gatherer<T, ?, R>

Returns:

a new Gatherer
fold
(Supplier<R>
the identity value for the fold operation
initial
,
BiFunction<? super R, ? super T, ? extends R>
the folding function
folder
)

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.

public static <
the type of input
T
,
the type of output
R
>
Gatherer<T, ?, R>

Returns:

a new Gatherer
mapConcurrent
(final int
the maximum concurrency desired
maxConcurrency
,
final Function<? super T, ? extends R>
a function to be executed concurrently
mapper
)

An operation which executes a function concurrently with a configured level of max concurrency, using virtual threads.

public static <
the type of element which this gatherer consumes
T
,
the type of element which this gatherer produces
R
>
Gatherer<T, ?, R>

Returns:

a new Gatherer which performs a prefix scan
scan
(Supplier<R>
the supplier of the initial value for the scanner
initial
,
BiFunction<? super R, ? super T, ? extends R>
the function to apply for each element
scanner
)

Returns a Gatherer that performs a Prefix Scan -- an incremental accumulation -- using the provided functions.

public static <
the type of elements the returned gatherer consumes and the contents of the windows it produces
TR
>
Gatherer<TR, ?, List<TR>>

Returns:

a new gatherer which groups elements into fixed-size windows
windowFixed
(int
the size of the windows
windowSize
)

Returns a Gatherer that gathers elements into windows -- encounter-ordered groups of elements -- of a fixed size.

public static <
the type of elements the returned gatherer consumes and the contents of the windows it produces
TR
>
Gatherer<TR, ?, List<TR>>

Returns:

a new gatherer which groups elements into sliding windows
windowSliding
(int
the size of the windows
windowSize
)

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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Gatherersback to summary
private Gatherers()

Method Detail

foldback to summary
public static <T, R> Gatherer<T, ?, R> fold(Supplier<R> initial, BiFunction<? super R, ? super T, ? extends R> folder)

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.

Implementation Specification

If no exceptions are thrown during processing, then this operation only ever produces a single element.

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();
// 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();
Parameters
<T>
the type of elements the returned gatherer consumes
<R>
the type of elements the returned gatherer produces
initial:Supplier<R>

the identity value for the fold operation

folder:BiFunction<? super R, ? super T, ? extends R>

the folding function

Returns:Gatherer<T, ?, R>

a new Gatherer

Exceptions
NullPointerException:
if any of the parameters are null
See Also
java.util.stream.Stream#reduce(Object, BinaryOperator)
mapConcurrentback to summary
public static <T, R> Gatherer<T, ?, R> mapConcurrent(final int maxConcurrency, final Function<? super T, ? extends R> mapper)

An operation which executes a function concurrently with a configured level of max concurrency, using virtual threads. This operation preserves the ordering of the stream.

API Note

In progress tasks will be attempted to be cancelled, on a best-effort basis, in situations where the downstream no longer wants to receive any more elements.

Implementation Specification

If a result of the function is to be pushed downstream but instead the function completed exceptionally then the corresponding exception will instead be rethrown by this method as an instance of RuntimeException, after which any remaining tasks are canceled.

Parameters
<T>
the type of input
<R>
the type of output
maxConcurrency:int

the maximum concurrency desired

mapper:Function<? super T, ? extends R>

a function to be executed concurrently

Returns:Gatherer<T, ?, R>

a new Gatherer

Exceptions
IllegalArgumentException:
if maxConcurrency is less than 1
NullPointerException:
if mapper is null
scanback to summary
public static <T, R> Gatherer<T, ?, R> scan(Supplier<R> initial, BiFunction<? super R, ? super T, ? extends R> scanner)

Returns a Gatherer that performs a Prefix Scan -- an incremental accumulation -- using the provided functions. Starting with an initial value obtained from the 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();
// 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();
Parameters
<T>
the type of element which this gatherer consumes
<R>
the type of element which this gatherer produces
initial:Supplier<R>

the supplier of the initial value for the scanner

scanner:BiFunction<? super R, ? super T, ? extends R>

the function to apply for each element

Returns:Gatherer<T, ?, R>

a new Gatherer which performs a prefix scan

Exceptions
NullPointerException:
if any of the parameters are null
windowFixedback to summary
public static <TR> Gatherer<TR, ?, List<TR>> windowFixed(int windowSize)

Returns a Gatherer that gathers elements into windows -- encounter-ordered groups of elements -- of a fixed size. If the stream is empty then no window will be produced. The last window may contain fewer elements than the supplied window size.

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();
// 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();

Implementation Specification

Each window produced is an unmodifiable List; calls to any mutator method will always cause UnsupportedOperationException to be thrown. There are no guarantees on the implementation type or serializability of the produced Lists.

API Note

For efficiency reasons, windows may be allocated contiguously and eagerly. This means that choosing large window sizes for small streams may use excessive memory for the duration of evaluation of this operation.

Parameters
<TR>
the type of elements the returned gatherer consumes and the contents of the windows it produces
windowSize:int

the size of the windows

Returns:Gatherer<TR, ?, List<TR>>

a new gatherer which groups elements into fixed-size windows

Exceptions
IllegalArgumentException:
when windowSize is less than 1
windowSlidingback to summary
public static <TR> Gatherer<TR, ?, List<TR>> windowSliding(int windowSize)

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. If the stream is empty then no window will be produced. If the size of the stream is smaller than the window size then only one window will be produced, containing all elements in the stream.

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();
// 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();

Implementation Specification

Each window produced is an unmodifiable List; calls to any mutator method will always cause UnsupportedOperationException to be thrown. There are no guarantees on the implementation type or serializability of the produced Lists.

API Note

For efficiency reasons, windows may be allocated contiguously and eagerly. This means that choosing large window sizes for small streams may use excessive memory for the duration of evaluation of this operation.

Parameters
<TR>
the type of elements the returned gatherer consumes and the contents of the windows it produces
windowSize:int

the size of the windows

Returns:Gatherer<TR, ?, List<TR>>

a new gatherer which groups elements into sliding windows

Exceptions
IllegalArgumentException:
when windowSize is less than 1
java.util.stream back to summary

pack-priv final Class Gatherers.Composite<T, A, R, AA, RR>

extends Object
implements Gatherer<T, Object, RR>
Class Inheritance
All Implemented Interfaces
java.util.stream.Gatherer

Field Summary

Modifier and TypeField and Description
private Gatherers.GathererImpl<T, Object, RR>
private final Gatherer<T, A, ? extends R>
private final Gatherer<? super R, AA, ? extends RR>

Constructor Summary

AccessConstructor and Description
private
Composite(Gatherer<T, A, ? extends R> left, Gatherer<? super R, AA, ? extends RR> right)

Method Summary

Modifier and TypeMethod and Description
public <RRR> Gatherer<T, ?, RRR>
andThen(Gatherer<? super RR, ?, ? extends RRR> that)

Overrides default java.util.stream.Gatherer.andThen.

Returns a composed Gatherer which connects the output of this Gatherer to the input of that Gatherer.

public BinaryOperator<Object>
combiner()

Overrides default java.util.stream.Gatherer.combiner.

A function which accepts two intermediate states and combines them into one.

public BiConsumer<Object, Gatherer.Downstream<? super RR>>
finisher()

Overrides default java.util.stream.Gatherer.finisher.

A function which accepts the final intermediate state and a Downstream object, allowing to perform a final action at the end of input elements.

private Gatherers.GathererImpl<T, Object, RR>
impl()

pack-priv static final <T, A, R, AA, RR> Gatherers.GathererImpl<T, ?, RR>
impl(Gatherer<T, A, R> left, Gatherer<? super R, AA, RR> right)

public Supplier<Object>
initializer()

Overrides default java.util.stream.Gatherer.initializer.

A function that produces an instance of the intermediate state used for this gathering operation.

public Gatherer.Integrator<Object, T, RR>
integrator()

Implements java.util.stream.Gatherer.integrator.

A function which integrates provided elements, potentially using the provided intermediate state, optionally producing output to the provided Downstream.

pack-priv static <T, A, R, AA, RR> Gatherers.Composite<T, A, R, AA, RR>
of(Gatherer<T, A, ? extends R> left, Gatherer<? super R, AA, ? extends RR> right)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

implback to summary
private Gatherers.GathererImpl<T, Object, RR> impl
leftback to summary
private final Gatherer<T, A, ? extends R> left
rightback to summary
private final Gatherer<? super R, AA, ? extends RR> right

Constructor Detail

Compositeback to summary
private Composite(Gatherer<T, A, ? extends R> left, Gatherer<? super R, AA, ? extends RR> right)

Method Detail

andThenback to summary
public <RRR> Gatherer<T, ?, RRR> andThen(Gatherer<? super RR, ?, ? extends RRR> that)

Overrides default java.util.stream.Gatherer.andThen.

Doc from java.util.stream.Gatherer.andThen.

Returns a composed Gatherer which connects the output of this Gatherer to the input of that Gatherer.

Annotations
@Override
combinerback to summary
public BinaryOperator<Object> combiner()

Overrides default java.util.stream.Gatherer.combiner.

Doc from java.util.stream.Gatherer.combiner.

A function which accepts two intermediate states and combines them into one.

Annotations
@Override
finisherback to summary
public BiConsumer<Object, Gatherer.Downstream<? super RR>> finisher()

Overrides default java.util.stream.Gatherer.finisher.

Doc from java.util.stream.Gatherer.finisher.

A function which accepts the final intermediate state and a Downstream object, allowing to perform a final action at the end of input elements.

Annotations
@Override
implback to summary
private Gatherers.GathererImpl<T, Object, RR> impl()
Annotations
@SuppressWarnings:unchecked
implback to summary
pack-priv static final <T, A, R, AA, RR> Gatherers.GathererImpl<T, ?, RR> impl(Gatherer<T, A, R> left, Gatherer<? super R, AA, RR> right)
initializerback to summary
public Supplier<Object> initializer()

Overrides default java.util.stream.Gatherer.initializer.

Doc from java.util.stream.Gatherer.initializer.

A function that produces an instance of the intermediate state used for this gathering operation.

Annotations
@Override
integratorback to summary
public Gatherer.Integrator<Object, T, RR> integrator()

Implements java.util.stream.Gatherer.integrator.

Doc from java.util.stream.Gatherer.integrator.

A function which integrates provided elements, potentially using the provided intermediate state, optionally producing output to the provided Downstream.

Annotations
@Override
ofback to summary
pack-priv static <T, A, R, AA, RR> Gatherers.Composite<T, A, R, AA, RR> of(Gatherer<T, A, ? extends R> left, Gatherer<? super R, AA, ? extends RR> right)
java.util.stream back to summary

pack-priv final Record Gatherers.GathererImpl<T, A, R>

extends Record
implements Gatherer<T, A, R>
Class Inheritance
All Implemented Interfaces
java.util.stream.Gatherer
Record Components
initializer:Supplier<A> 
integrator:Gatherer.Integrator<A, T, R> 
combiner:BinaryOperator<A> 
finisher:BiConsumer<A, Gatherer.Downstream<? super R>> 

Field Summary

Modifier and TypeField and Description
private final BinaryOperator<A>
combiner

Record Component accessed by combiner().

private final BiConsumer<A, Gatherer.Downstream<? super R>>
finisher

Record Component accessed by finisher().

private final Supplier<A>
initializer

Record Component accessed by initializer().

private final Gatherer.Integrator<A, T, R>
integrator

Record Component accessed by integrator().

Constructor Summary

AccessConstructor and Description
pack-priv
GathererImpl(Supplier<A> initializer, Gatherer.Integrator<A, T, R> integrator, BinaryOperator<A> combiner, BiConsumer<A, Gatherer.Downstream<? super R>> finisher)

Method Summary

Modifier and TypeMethod and Description
public BinaryOperator<A>
combiner()

Record Component getter of combiner.

Overrides default java.util.stream.Gatherer.combiner.

A function which accepts two intermediate states and combines them into one.

public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public BiConsumer<A, Gatherer.Downstream<? super R>>
finisher()

Record Component getter of finisher.

Overrides default java.util.stream.Gatherer.finisher.

A function which accepts the final intermediate state and a Downstream object, allowing to perform a final action at the end of input elements.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public Supplier<A>
initializer()

Record Component getter of initializer.

Overrides default java.util.stream.Gatherer.initializer.

A function that produces an instance of the intermediate state used for this gathering operation.

public Gatherer.Integrator<A, T, R>
integrator()

Record Component getter of integrator.

Implements java.util.stream.Gatherer.integrator.

A function which integrates provided elements, potentially using the provided intermediate state, optionally producing output to the provided Downstream.

pack-priv static <T, A, R> Gatherers.GathererImpl<T, A, R>
of(Supplier<A> initializer, Gatherer.Integrator<A, T, R> integrator, BinaryOperator<A> combiner, BiConsumer<A, Gatherer.Downstream<? super R>> finisher)

Hides java.util.stream.Gatherer.of.

Returns a new, parallelizable, Gatherer described by the given initializer, integrator, combiner and finisher.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

combinerback to summary
private final BinaryOperator<A> combiner

Record Component accessed by combiner().

finisherback to summary
private final BiConsumer<A, Gatherer.Downstream<? super R>> finisher

Record Component accessed by finisher().

initializerback to summary
private final Supplier<A> initializer

Record Component accessed by initializer().

integratorback to summary
private final Gatherer.Integrator<A, T, R> integrator

Record Component accessed by integrator().

Constructor Detail

GathererImplback to summary
pack-priv GathererImpl(Supplier<A> initializer, Gatherer.Integrator<A, T, R> integrator, BinaryOperator<A> combiner, BiConsumer<A, Gatherer.Downstream<? super R>> finisher)

Method Detail

combinerback to summary
public BinaryOperator<A> combiner()

Record Component getter of combiner.

Overrides default java.util.stream.Gatherer.combiner.

Doc from java.util.stream.Gatherer.combiner.

A function which accepts two intermediate states and combines them into one.

Annotations
@Override
equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
finisherback to summary
public BiConsumer<A, Gatherer.Downstream<? super R>> finisher()

Record Component getter of finisher.

Overrides default java.util.stream.Gatherer.finisher.

Doc from java.util.stream.Gatherer.finisher.

A function which accepts the final intermediate state and a Downstream object, allowing to perform a final action at the end of input elements.

Annotations
@Override
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

initializerback to summary
public Supplier<A> initializer()

Record Component getter of initializer.

Overrides default java.util.stream.Gatherer.initializer.

Doc from java.util.stream.Gatherer.initializer.

A function that produces an instance of the intermediate state used for this gathering operation.

Annotations
@Override
integratorback to summary
public Gatherer.Integrator<A, T, R> integrator()

Record Component getter of integrator.

Implements java.util.stream.Gatherer.integrator.

Doc from java.util.stream.Gatherer.integrator.

A function which integrates provided elements, potentially using the provided intermediate state, optionally producing output to the provided Downstream.

Annotations
@Override
ofback to summary
pack-priv static <T, A, R> Gatherers.GathererImpl<T, A, R> of(Supplier<A> initializer, Gatherer.Integrator<A, T, R> integrator, BinaryOperator<A> combiner, BiConsumer<A, Gatherer.Downstream<? super R>> finisher)

Hides java.util.stream.Gatherer.of.

Doc from java.util.stream.Gatherer.of.

Returns a new, parallelizable, Gatherer described by the given initializer, integrator, combiner and finisher.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

java.util.stream back to summary

pack-priv final Enum Gatherers.Value

extends Enum<Gatherers.Value>
implements Supplier, BinaryOperator, BiConsumer
Class Inheritance
All Implemented Interfaces
java.util.function.BiConsumer, java.util.function.BinaryOperator, java.util.function.BiFunction, java.util.function.Supplier
Annotations
@SuppressWarnings:rawtypes

Field Summary

Modifier and TypeField and Description
public static final Gatherers.Value
pack-priv final BinaryOperator<Void>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
accept(Object state, Object downstream)

Implements java.util.function.BiConsumer.accept.

Performs this operation on the given arguments.

public Object
apply(Object left, Object right)

Implements java.util.function.BiFunction.apply.

Applies this function to the given arguments.

pack-priv <T> BinaryOperator<T>
pack-priv <T, R> BiConsumer<T, Gatherer.Downstream<? super R>>
public Object
get()

Implements java.util.function.Supplier.get.

Gets a result.

pack-priv <A> Supplier<A>
public static Gatherers.Value
public static Gatherers.Value[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DEFAULTback to summary
public static final Gatherers.Value DEFAULT
statelessCombinerback to summary
pack-priv final BinaryOperator<Void> statelessCombiner

Constructor Detail

Valueback to summary
private Value()

Method Detail

acceptback to summary
public void accept(Object state, Object downstream)

Implements java.util.function.BiConsumer.accept.

Doc from java.util.function.BiConsumer.accept.

Performs this operation on the given arguments.

Annotations
@Override
applyback to summary
public Object apply(Object left, Object right)

Implements java.util.function.BiFunction.apply.

Doc from java.util.function.BiFunction.apply.

Applies this function to the given arguments.

Annotations
@Override
combinerback to summary
pack-priv <T> BinaryOperator<T> combiner()
Annotations
@ForceInline
@SuppressWarnings:unchecked
finisherback to summary
pack-priv <T, R> BiConsumer<T, Gatherer.Downstream<? super R>> finisher()
Annotations
@ForceInline
@SuppressWarnings:unchecked
getback to summary
public Object get()

Implements java.util.function.Supplier.get.

Doc from java.util.function.Supplier.get.

Gets a result.

Annotations
@Override
initializerback to summary
pack-priv <A> Supplier<A> initializer()
Annotations
@ForceInline
@SuppressWarnings:unchecked
valueOfback to summary
public static Gatherers.Value valueOf(String name)
valuesback to summary
public static Gatherers.Value[] values()