Lambda Function get turned into BiConsumer

Viewed 501

I am trying to work out how this works.
I am going to list what I believe is happening and hopefully someone can tell me I am wrong in places. Or tell me how/why this works.

public BiConsumer<List<T>, T> accumulator() {
    return List::add;
}
  1. A method called accumulator that accepts nothing and returns a BiConsumer method.
  2. accumulator returns a reference to the add method on the context of a List as the BiConsumer
  3. The actual add method on a List is actually a Function<T, Boolean>, takes in T and returns a boolean

This is where I get into the guessing realm.

  1. It is calling the add method, on the instance of the List that is the first argument of the BiConsumer.

I know internally it must be doing.

public BiConsumer<List<T>, T> accumulator() {
    return (list, item) -> list.add(i);
}

I understand that it can ignore the return type of the add method, so it can pretend Function<A, B> is Consumer<A>

But I just don't understand the black magic that turns a BiConsumer runs a Consumer or Function on the instance of the first argument of the BiConsumer.

1 Answers
Related