Java 8 Stream Collector interface¶
The Collector interface is a core component of the Java 8 Stream API, serving as the contract for mutable reduction operations.^[600-developer-java-java8-java8-collect.md] It is typically invoked via the stream().collect() method and provides a mechanism for accumulating input elements into a mutable result container, optionally transforming the result after all elements have been processed.^[600-developer-java-java8-java8-collect.md]
The standard library provides implementations of this interface through the java.util.stream.Collectors utility class.^[600-developer-java-java8-java8-collect.md]
Interface Characteristics¶
The Collector interface defines a strategy for a mutable reduction process, distinguishing it from immutable reduction operations like reduce.^[600-developer-java-java8-java8-collect.md] It is parameterized by three types: T (the type of input elements), A (the mutable accumulation type), and R (the final result type).^[600-developer-java-java8-java8-collect.md]
A concrete implementation, CollectorImpl, is provided within the API.^[600-developer-java-java8-java8-collect.md]
Core Components¶
The functionality of a Collector is built upon four primary methods that describe the reduction process^[600-developer-java-java8-java8-collect.md]:
- Supplier: Creates a new mutable result container.
- Accumulator: Incorporates a new data element into the result container.
- Combiner: Combines two result containers into one (used in parallel streams).
- Finisher: Performs an optional final transform on the container to convert it to the final result type.
Related Concepts¶
- [[Java Streams]]
- [[Grouping by]]
- [[Partitioning by]]
- [[Reduction operations]]
Sources¶
600-developer-java-java8-java8-collect.md