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;
}
- A method called accumulator that accepts nothing and returns a BiConsumer method.
accumulatorreturns a reference to theaddmethod on the context of aListas theBiConsumer- The actual
addmethod on aListis actually aFunction<T, Boolean>, takes inTand returns a boolean
This is where I get into the guessing realm.
- It is calling the
addmethod, on the instance of theListthat is the first argument of theBiConsumer.
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.