Search code examples
javajava-8functional-interface

Usage of BiConsumer interface in java 8


I'm new to programming with streams and functional interfaces and the java.util.function.BiConsumer describes the method accept like below in the documentation which is not clear to understand

void accept(T t,
            U u)
Performs this operation on the given arguments.
Parameters:
t - the first input argument
u - the second input argument

Functional interfaces can be referred using the lamda expressions like below

BiConsumer<String,String> a=(a,b)->{
 
}

But what does the "this operation mean here exactly". Thanks in advance


Solution

  • Note that a BiConsumer represents an "operation" that takes 2 parameters.

    For example, if you have

    BiConsumer<String,String> foo = (a, b) -> System.out.println(a + b);
    

    foo represents the operation System.out.println(a + b); - printing the two parameters concatenated together.

    "This operation" just means the operation that the BiConsumer instance represents.

    So in the example above, "this operation" for foo just means "System.out.println(a + b);".

    The documentation is saying that if you call accept, the operation represented by the instance (for foo, it is System.out.println(a + b);) will be performed.