Return to site

☕2️⃣3️⃣JDK 23: The new features in Java 23: Previews of stream gatherers

👉enhance 💪 the stream API to support custom intermediate operations

· java,java23


This would allow custom intermediate operations to manipulate streams of infinite size.


broken image


Deeper explanation👇

The stream gatherer is a new featur 🆕 introduced in Java 23 as a preview feature.

It enhances the stream API by enabling the creation of custom intermediate operations.

This allows for more flexible and expressive stream pipelines, capable of transforming data in ways that aren’t easily achievable with existing built-in intermediate operations.

Here’s a brief overview of what stream gatherers can do:

  • 🔄️ Transform elements in various ways, such as one-to-one, one-to-many, many-to-one, or many-to-many.
  • 🕵️ Track previously seen elements to influence the transformation of later elements.
  • ✂️ Short-circuit processing, which is particularly useful for transforming infinite streams into finite ones.
  • 🧵 Process streams in parallel, given that a combiner function is specified.


To create a gatherer, you would implement the Gatherer interface.

This interface allows you to define how each element of the stream is processed and how the final result is produced.

For example, you could create a gatherer that groups elements into batches, deduplicates consecutively similar elements, or performs incremental accumulation functions.


The introduction of stream gatherers aims to make stream pipelines more flexible and expressive 😄 1allowing for manipulation of streams of infinite size and simplifying the code when collecting elements from a stream into collections or other data structures.


⚠️ Remember, since it’s a preview feature, to use stream gatherers, you must specify additional command-line options to compile and run code that contains them.


#java #java23 #feature #stream #gatherer