Paul Krill
Editor at Large

Possible Java streams enhancement

news
Oct 30, 20232 mins

Operations would be more flexible and expansive under the proposal by the OpenJDK community.

hot coffee java beans cup steam
Credit: Felix Brönnimann

Java’s stream operations would become more flexible and expressive and allow custom intermediate operations to manipulate streams of infinite size to the extent possible, under a proposal in the OpenJDK community.

Authored this month and in a preview stage, the stream gatherers proposal would enhance Java’s Stream API to support custom intermediate operations. This would allow stream pipelines to transform data in ways not easily achievable with existing built-in intermediate operations. It is not the intent, though, to change the Java language to better facilitate stream processing or provide a special-case compilation of code that uses the Stream API.

Stream::gather(Gatherer) is a new intermediate stream operation that processes elements of a stream by applying a user-defined entity called a gatherer. With the gather operation, developers can build efficient, parallel-ready streams implementing almost any intermediate operation. The proposal explains that Java 8, unveiled in March 2014, introduced the first API specifically for lambda expressions, the Stream API, java.util.stream. A stream is a lazily computed, potentially unbounded sequence of values. The API supports processing a stream either sequentially or in parallel. A stream pipeline consists of a source of elements, any number of intermediate operations, and a terminal operation. This programming is both expressive and efficient. 

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.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author