Java 8 Stream API¶
The Java 8 Stream API is introduced as a sequence of elements supporting sequential and parallel aggregate operations.^[600-developer__java__java8__java8-stream.md]
Core Interfaces¶
The API defines a foundational hierarchy for stream operations. The BaseStream<T, S extends BaseStream<T, S>> interface extends AutoCloseable, serving as the base for streams.^[600-developer__java__java8__java8-stream.md] The primary interface for object streams is Stream<T>, which extends BaseStream<T, Stream<T>>.^[600-developer__java__java8__java8-stream.md] In this context, the generic type S represents the stream itself, and operations that return a stream typically return a new stream object.^[600-developer__java__java8__java8-stream.md]
Resource Management¶
Streams implement the AutoCloseable interface, allowing them to be managed within try-with-resources blocks.^[600-developer__java__java8__java8-stream.md] This is particularly relevant when using the onClose method, which registers handlers to be executed when the stream is closed.^[600-developer__java__java8__java8-stream.md]
For example, multiple handlers can be chained; they execute in reverse order (similar to a stack) when the stream closes, and exceptions thrown by handlers suppress those from previous handlers.^[600-developer__java__java8__java8-stream.md]
Implementation Architecture¶
The internal construction and evaluation of stream pipelines are managed by the AbstractPipeline class.^[600-developer__java__java8__java8-stream.md] This class acts as the abstract base for "pipeline" classes, forming the core implementation of the Stream interface and its primitive specializations.^[600-developer__java__java8__java8-stream.md]
Stream operations are categorized into types such as TerminalOp, which defines the behavior of terminal operations that end the stream pipeline.^[600-developer__java__java8__java8-stream.md]
Related Concepts¶
- Java
- [[Functional programming]]
Sources¶
600-developer__java__java8__java8-stream.md