0

In java sources, Collectors.toList is defined as:

public static <T>
Collector<T, ?, List<T>> toList() {
    return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
                               (left, right) -> { left.addAll(right); return left; },CH_ID);
}

Corresponding CollectorImpl constructor

CollectorImpl(Supplier<A> supplier,
                      BiConsumer<A, T> accumulator,
                      BinaryOperator<A> combiner,
                      Set<Characteristics> characteristics) {
            this(supplier, accumulator, combiner, castingIdentity(), characteristics);
        }

We see List::add as second parameter of CollectorImpl's contructor,I am wondering how does it maps to accumulator which is of Type BiConsumer<A,T> since BiConsumer consumes two parameter and List::add consumes one paramter only. What am I missing here?

elect
  • 6,765
  • 10
  • 53
  • 119
Vinit89
  • 583
  • 1
  • 7
  • 31
  • The two parameters become actually `this` and the new element. See also http://stackoverflow.com/questions/25512532/lambda-parameters – Tunaki Aug 07 '16 at 17:57
  • 2
    @AndrewTobilko No, the overload with 2 parameters would be the one taking the index at which to add the element, but this isn't the one refered to here. `List::add` can be seen as `(list, elem) -> list.add(elem);`. – Tunaki Aug 07 '16 at 18:03
  • 1
    @Tunaki, I didn't know, thanks – Andrew Tobilko Aug 07 '16 at 18:08

0 Answers0