Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


1 Answers

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.

like image 114
Sweeper Avatar answered Oct 22 '25 19:10

Sweeper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!