Java 8 Stream interface¶
The Java 8 Stream interface (java.util.stream.Stream) is a sequence of elements supporting sequential and parallel aggregate operations.^[600-developer-java-java8-java8-stream.md]
Interface Hierarchy¶
The Stream interface builds upon the BaseStream interface.
BaseStream<T, S extends BaseStream<T, S>>: This is the base interface for streams, extendingAutoCloseable. It defines the basic contract for stream functionality.^[600-developer-java-java8-java8-stream.md]Stream<T>: ExtendsBaseStream. In the generic signatureBaseStream<T, S>,Srepresents the specific type of the stream (e.g., a new stream object), whereasStreambindsStoStream<T>.^[600-developer-java-java8-java8-stream.md]
Operations and Characteristics¶
Intermediate Operations¶
Methods such as sequential() and peek(Consumer<? super T> action) are intermediate operations. When invoked, they return a new stream object, allowing operations to be chained together to form a processing pipeline.^[600-developer-java-java8-java8-stream.md]
Resource Management¶
Because BaseStream extends AutoCloseable, streams can be managed using try-with-resources blocks. This allows for the registration of handlers via onClose(), which are executed in reverse order when the stream is closed, similar to standard resource management in Java.^[600-developer-java-java8-java8-stream.md]
Internal Implementation¶
AbstractPipeline¶
The core implementation of the Stream interface and its primitive specializations relies on AbstractPipeline. This abstract class acts as the foundation for "pipeline" classes, managing both the construction and the evaluation of stream pipelines.^[600-developer-java-java8-java8-stream.md]
TerminalOp¶
TerminalOp represents the final operation in a stream pipeline, triggering the actual processing of the data.^[600-developer-java-java8-java8-stream.md]
Related Concepts¶
- Java
- [[Functional programming]]
- [[Aggregate operations]]
Sources¶
^[600-developer-java-java8-java8-stream.md]