The Collector Interface

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 Supplier Method
    • This returns a Supplier of 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 Accumulator Method
    • 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 Finisher Method
    • 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 Combiner Method
    • 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.
public BinaryOperator<List<T>> combiner() {
    return (list1, list2) -> {
        list1.addAll(list2);
        return list1;
    }
}

 

Parallelized reduction process using combiner method (Modern Java in Action)

  • The Characteristics Method
    • This returns an immutable set of Characteristics, which defines the behavior of the collector
    • Characteristics is 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.

 

 

 

'Java' 카테고리의 다른 글

JaCoCo + Github Action 커버리지 리포트  (0) 2023.08.28

+ Recent posts