CKTIS / Java
Collector — custom mutable stream reduction
Collector<T, A, R> describes accumulating stream elements into mutable state and transforming it into a result. Learn the collector functions, parallel-processing requirements, and the meaning of its characteristics in Java SE 21.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Collector<T, A, R>
Collector.of(supplier, accumulator, combiner, characteristics...)
Collector.of(supplier, accumulator, combiner, finisher, characteristics...)Collector<T, A, R> describes a mutable reduction used by Stream.collect: T is the element, A the accumulation state, and R the result. Do not confuse the Collector interface with the Collectors class, which provides ready-made collectors. Scope: Java SE 21; repository runtime and production runtime are not attested.
Collector.supplier() returns a function creating a fresh mutable container; Collector.accumulator() returns a function incorporating an element; Collector.combiner() returns a function merging partial states; Collector.finisher() returns a function converting A to R. Collector.of builds a collector from these functions. The overload without an explicit finisher has A = R and automatically declares IDENTITY_FINISH.
Collector.characteristics() returns an immutable set of declared properties. Collector.Characteristics.CONCURRENT permits simultaneous accumulator calls on the same container. Collector.Characteristics.UNORDERED means there is no commitment to preserve encounter order. Shared concurrent accumulation requires CONCURRENT and either UNORDERED or an unordered stream. Collector.Characteristics.IDENTITY_FINISH permits skipping the finisher: it must be an identity transformation, and casting A to R must succeed. Flags describe guarantees; they do not provide synchronization.
Combining must be associative and consistent with accumulation; merging an empty state must not change the result. A collector without CONCURRENT can run in parallel: the library isolates partial states and then combines them. Do not share one mutable container across supplier calls.
The example preserves order and uses StringBuilder as A and String as R. It declares no characteristics: StringBuilder does not support shared concurrent accumulation, and the finisher changes the type. Both collection paths are expected to print ABC.
Examples
import java.util.List;
import java.util.stream.Collector;
public class Main {
public static void main(String[] args) {
Collector<String, StringBuilder, String> concatenate = Collector.of(
StringBuilder::new,
(buffer, value) -> buffer.append(value),
(left, right) -> { left.append(right); return left; },
StringBuilder::toString
);
List<String> values = List.of("A", "B", "C");
System.out.println(values.stream().collect(concatenate));
System.out.println(values.parallelStream().collect(concatenate));
System.out.println(concatenate.characteristics().isEmpty());
}
}Result
ABC ABC true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.