public interface Collector<T, A, R> {
Supplier<A> supplier();
BiConsumer<A, T> accumulator();
Function<A, R> finisher();
BinaryOperator<A> combiner();
Set<Characteristics> characteristics();
}
T: the generic type of the items in the stream to be collected
A: the type of the accumulator - the object on which the partial result will be accumulated during the process
R: the type of the object resulting from the collect operation.
For instance, we could implement a ToListCollector<T> which will gather all elements of a Stream<T> into a List<T>
public class ToListCollector<T> implements Collector<T, List<T>, List<T>>;
Understanding the Methods Declared by Collector Interface
The SupplierMethod
This returns a Supplierof an empty accumulator - which is a function without any parameter that creates an instance of an empty accumulator used during the collection process.
For instance, a
The AccumulatorMethod
This returns a function that performs the reduction operation and adds an element to a resulting container.
For ToListCollector<T>, this function will add the current item to the list containing the previous elements.
public BiConsumer<List<T>, T> accumulator() {
return (list, item) -> list.add(item);
// or just return List::add;
}
The FinisherMethod
This returns a function that is invoked at the end of the accumulation process.
For ToListCollector<T>, the accumulator object already coincides with the final expected result, so an identity function is used.
public Function<List<T>, List<T>> finisher() {
return Function.identity();
}
The CombinerMethod
This defines how the accumulators resulting from the reduction of different subparts of the stream (processed in parallel) are combined.
For ToListCollector<T>, we just add the list to the other end of the list.
Parallelized reduction process using combiner method (Modern Java in Action)
The CharacteristicsMethod
This returns an immutable set of Characteristics, which defines the behavior of the collector
Characteristicsis an enumeration containing 3 items
UNORDERED - the result of the reduction isn't affected by the order in which the elements of the stream are traversed.
CONCURRENT - the accumulator function can be called concurrently and ran in parallel.
IDENTITY_FINISH - the function returned by the finisher method is the identity function.
The ToListCollector<T> is IDENTITY_FINISH and CONCURRENT. However, it is not UNORDERED because the order of the stream will be preserved in the resulting list.