Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore about the bi consumer functional interface.
As I mentioned earlier in the introduction session, each and every functional interface as an extension
by consumer is an extension to the consumer.
Let's go ahead and check what are the different methods that this bi consumer interface has.
I'm going to press the shift key twice.
It gave me the search everywhere window.
Want to search for bi consumer and then press enter.
There you go.
This is the one which has an icon I which stands for.
So if you take a look at it, there are two methods.
One is accept and there is a default method called and then this method is similar to the and then method,
which is part of the consumer interface.
If you take a look at this method, it accepts two inputs compared to the consumer interface.
If you take a look at the consumer interface.
It accepts one input, but the buy consumer accepts two inputs.
Basically, you can pass two inputs and then perform some operation on the inputs that are passed to
this accept method.
Now let's go ahead and implement this.
I'm going to create a class called.
By consumer.
Example.
Let's make this class executable by adding public static void main.
Now what I'm going to do is I'm going to create a instance of my consumer functional interface.
Basically, we are going to implement the by consumer functional interface.
Why consumer?
And I'm going to pass to string.
Then I'm going to name this one as bi consumer equal to.
So there are two inputs that we are going to pass.
I'm going to name this one as A and B, So both are of type string.
And my expectation and the use case that I'm going to code is really simple.
I'm just going to print these values in the console.
Value of this.
And.
Valley of Peace.
Nothing fancy here we are just taking the inputs and printing the.
Input on the console.
So how will I pass the input by consumer dot?
The accept method using the accept method.
I'll pass the input to that one.
The Java.
Robust two strings.
One is Java seven.
Another one is Java eight.
So I'm going to run this program.
This program is going to print something in the console.
There you go.
It printed the values A and B.
Now let's enhance this use case.
Let's perform multiplication of two numbers.
Right.
How do we do that?
By consumer.
So we are going to perform multiplication operation on numbers, right?
I'm going to take this value as integer.
The type is going to be integer and.
I'm going to name this one as multiply equal to.
So in here, A comma B, you can use the same input.
And.
What is this is going to do?
This one is not going to return the result.
Keep in mind, consumer interface will only accept the input and it doesn't return any output out of
that.
So in that case, we will just print.
The values here.
The values are going to be.
Multiplication is.
Within the bracket is to be.
Okay, now I want to perform Division two, because now if we take a look at the by consumer interface,
there is this and then method.
I would like to explore this method.
So you want to perform multiplication and division for two numbers?
Okay.
Division.
I'm going to rename this one to.
Division.
Going to be A divided by B.
Right.
Sounds good.
Now let's call these by consumer functional interface implementations.
First, I'm going to perform Multiply, and then if you take a look at it, it gives me the option to
use the and then method followed by that I'm going to pass the division and then using the accept method,
we'll pass the two inputs to this one.
I'm going to pass ten comma five.
Right.
If you take a look at it, ten is the one which is going to be assigned to the T value and five is the
one which is going to be assigned to the U value.
We go and take a look at it.
T is the first argument and use the second argument.
Now I'm gonna run this.
Though it printed the multiplication result as 50 and division result as 20.
If you want, you can add addition subtraction to this use case and then you can perform the whole multiplication
division, addition and subtraction.
Now let's go ahead and code the real use case.
Whatever we coded in the previous example.
In the previous example, if you remember, we use the and then method to print the student and it's
and the students activities.
I'm going to run this class.
Is the sprint name and activities.
You take a look at it.
We are using we are printing the student and the student activities.
I'm going to implement this using the bi consumer example.
Okay.
I'm going to name this one as public.
Patrick.
This method is not going to return anything, and this method is going to have a name as name and activities.
I'm going to get the list of students.
The students.
And then.
This list is part of the Java.util package.
The next step is.
You then list using the student database class.
We are going to get all the students.
There you go.
Now we have the student list ready.
What is the next step?
The next step is we have to create the bi consumer.
Functional interface implementation.
So we are going to implement this.
Name is of type string.
And what are the list of activities?
List of activities are.
List of string.
Right.
Want to name this one as bi consumer.
So it's going to take the name comma.
Activities.
The name stands for the type string and activities.
Refer to the type of list of string.
Right.
And our job is to print both of those in the console.
Gonna have name printed first followed by that.
You need to have.
Balloon inside the string.
And then followed by that, we are going to print the activities.
This is what we are going to do.
So in the previous use case, we had to create a separate instance for name and separate consumer functional
implementation for activities.
Here we are going to achieve both in the same input itself.
Let's use a far reach method to iterate the elements in the student list.
And if you take a look at it it of the.
Instance, US consumer rate.
We can pass by consumer also as part of it.
Okay, Now what we will do.
The bosses shouldn't.
And we are going to use the.
Why consumer dot accept.
I think bars is a name.
Student Dot get name.
And shouldn't that.
Good activities.
Okay, then, Dot.
So we are going to use the accept method.
So we have passed the exit method already.
Now, as soon as we call the accept method, all these inputs are going to be passed one by one.
Much argument is going to be the name, and second argument is going to be the list of activities.
The next step is I'm going to call this method.
There you go.
It printed the values by using the by consumer interface.
So now you know the difference, right?
When to use by consumer and when to use consumer.
If you feel that both of these conditions, meaning both of these attributes can be combined into one
single instance itself, then go ahead and use by consumer.
If you think like you have value putting it in a separate consumer function interface, you can use
that one.
There is no benefit of having any performance issue or something comparing with by consumer to consumer.
It's all going to perform the same.
So with this we came to the end of the bi consumer functional interface.
Thank you for watching.