Operations Set to Expand: OpenJDK Community’s Proposal for Enhanced Flexibility
Java’s stream operations could soon see significant enhancements, making them more versatile and expressive, thanks to a proposal under consideration within the OpenJDK community.
Recently authored and currently in a preview stage, the “stream gatherers” proposal aims to augment Java’s Stream API with support for custom intermediate operations. This enhancement would empower developers to manipulate stream pipelines in ways that aren’t easily achievable with the current set of built-in intermediate operations. Notably, the proposal emphasizes that its goal isn’t to modify the Java language itself to optimize stream processing or provide specialized compilation for Stream API usage.
At the heart of the proposal is the introduction of Stream::gather(Gatherer)
, a new intermediate operation designed to process elements of a stream using a user-defined entity called a “gatherer.” This approach enables the creation of efficient and parallel-ready streams capable of implementing nearly any intermediate operation required. Since its inception in Java 8, released in March 2014, the Stream API (java.util.stream
) has provided a mechanism for handling sequences of values lazily computed and potentially unbounded. It supports both sequential and parallel processing of streams, structured as a sequence of elements from a source, followed by any number of intermediate operations, culminating in a terminal operation.
The current Stream API offers a fixed set of intermediate and terminal operations, including mapping, filtering, reduction, sorting, and more. While robust, this fixed set can sometimes limit the expressiveness of stream pipelines for complex tasks. The proposed gather
operation seeks to address these limitations by enabling custom, user-defined operations within stream pipelines.
Looking ahead, if accepted, stream gatherers could potentially debut in Java Development Kit (JDK) 22, currently under development and scheduled for release in March 2023. As of now, the preview of stream gatherers is not tied to any specific version of standard Java, but it represents a promising evolution in the capabilities of Java’s Stream API, promising greater flexibility and power to developers working with streams in Java applications.
With the builder-style API, each intermediate operation returns a new stream; evaluation begins only when a terminal operation is invoked. The Stream API provides a reasonably rich, albeit fixed set of intermediate and terminal operations, including mapping, filtering, reduction, sorting, and others. An extensible terminal operation, Stream::collect,
also is featured, enabling the output of a pipeline to be summarized in multiple ways. Use of streams in the Java ecosystem now is pervasive. However, the fixed set of intermediate operations means some complex tasks cannot easily be expressed as stream pipelines. Either a required intermediate task operation does not exist, or it exists but does not directly support the task.
Intended for the standard edition of Java, the soonest stream gatherers could appear in a formal edition would be in Java Development Kit (JDK) 22, which is in development now and set to appear in March 2023. Currently, the stream gatherers preview is not targeted to any specific version of standard Java.