Dev

Possible Java streams enhancement


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.

Copyright © 2023 IDG Communications, Inc.



READ SOURCE

This website uses cookies. By continuing to use this site, you accept our use of cookies.