WEBVTT

00:00.750 --> 00:01.260
Hi everyone.

00:01.260 --> 00:02.460
Welcome back to this tutorial.

00:02.460 --> 00:07.670
In this tutorial we will code and explore about the bi consumer functional interface.

00:07.680 --> 00:13.650
As I mentioned earlier in the introduction session, each and every functional interface as an extension

00:13.650 --> 00:16.830
by consumer is an extension to the consumer.

00:16.860 --> 00:21.810
Let's go ahead and check what are the different methods that this bi consumer interface has.

00:22.260 --> 00:23.760
I'm going to press the shift key twice.

00:23.760 --> 00:25.770
It gave me the search everywhere window.

00:25.930 --> 00:31.410
Want to search for bi consumer and then press enter.

00:31.530 --> 00:32.220
There you go.

00:32.430 --> 00:35.010
This is the one which has an icon I which stands for.

00:36.420 --> 00:38.010
So if you take a look at it, there are two methods.

00:38.010 --> 00:44.010
One is accept and there is a default method called and then this method is similar to the and then method,

00:44.010 --> 00:47.150
which is part of the consumer interface.

00:47.160 --> 00:52.080
If you take a look at this method, it accepts two inputs compared to the consumer interface.

00:52.080 --> 00:54.090
If you take a look at the consumer interface.

00:56.650 --> 01:00.970
It accepts one input, but the buy consumer accepts two inputs.

01:01.000 --> 01:07.300
Basically, you can pass two inputs and then perform some operation on the inputs that are passed to

01:07.300 --> 01:08.590
this accept method.

01:08.620 --> 01:11.080
Now let's go ahead and implement this.

01:12.190 --> 01:13.990
I'm going to create a class called.

01:15.620 --> 01:17.060
By consumer.

01:18.080 --> 01:18.860
Example.

01:19.930 --> 01:23.620
Let's make this class executable by adding public static void main.

01:24.220 --> 01:30.320
Now what I'm going to do is I'm going to create a instance of my consumer functional interface.

01:30.340 --> 01:35.230
Basically, we are going to implement the by consumer functional interface.

01:36.620 --> 01:37.550
Why consumer?

01:38.490 --> 01:40.230
And I'm going to pass to string.

01:41.850 --> 01:46.110
Then I'm going to name this one as bi consumer equal to.

01:46.860 --> 01:48.570
So there are two inputs that we are going to pass.

01:48.570 --> 01:53.250
I'm going to name this one as A and B, So both are of type string.

01:53.700 --> 01:59.070
And my expectation and the use case that I'm going to code is really simple.

01:59.070 --> 02:01.440
I'm just going to print these values in the console.

02:02.630 --> 02:04.310
Value of this.

02:06.510 --> 02:07.110
And.

02:09.390 --> 02:10.860
Valley of Peace.

02:16.230 --> 02:19.560
Nothing fancy here we are just taking the inputs and printing the.

02:21.560 --> 02:22.700
Input on the console.

02:23.090 --> 02:26.900
So how will I pass the input by consumer dot?

02:26.930 --> 02:29.270
The accept method using the accept method.

02:29.660 --> 02:31.250
I'll pass the input to that one.

02:31.640 --> 02:32.390
The Java.

02:34.000 --> 02:34.960
Robust two strings.

02:34.960 --> 02:36.340
One is Java seven.

02:37.370 --> 02:38.820
Another one is Java eight.

02:41.060 --> 02:43.070
So I'm going to run this program.

02:43.070 --> 02:46.340
This program is going to print something in the console.

02:49.190 --> 02:49.680
There you go.

02:49.700 --> 02:51.920
It printed the values A and B.

02:51.950 --> 02:54.470
Now let's enhance this use case.

02:54.470 --> 02:58.010
Let's perform multiplication of two numbers.

02:58.220 --> 02:58.610
Right.

02:58.610 --> 02:59.600
How do we do that?

02:59.750 --> 03:01.100
By consumer.

03:01.250 --> 03:04.850
So we are going to perform multiplication operation on numbers, right?

03:04.850 --> 03:07.940
I'm going to take this value as integer.

03:08.300 --> 03:11.090
The type is going to be integer and.

03:11.940 --> 03:15.660
I'm going to name this one as multiply equal to.

03:16.650 --> 03:21.270
So in here, A comma B, you can use the same input.

03:23.520 --> 03:24.120
And.

03:25.810 --> 03:27.310
What is this is going to do?

03:27.310 --> 03:29.440
This one is not going to return the result.

03:29.440 --> 03:35.560
Keep in mind, consumer interface will only accept the input and it doesn't return any output out of

03:35.600 --> 03:35.920
that.

03:35.920 --> 03:38.590
So in that case, we will just print.

03:42.180 --> 03:43.110
The values here.

03:43.650 --> 03:45.060
The values are going to be.

03:46.300 --> 03:47.800
Multiplication is.

03:51.470 --> 03:53.330
Within the bracket is to be.

03:54.460 --> 04:01.030
Okay, now I want to perform Division two, because now if we take a look at the by consumer interface,

04:01.030 --> 04:02.500
there is this and then method.

04:02.500 --> 04:04.840
I would like to explore this method.

04:07.680 --> 04:12.100
So you want to perform multiplication and division for two numbers?

04:12.430 --> 04:12.910
Okay.

04:14.270 --> 04:15.140
Division.

04:15.140 --> 04:16.880
I'm going to rename this one to.

04:17.790 --> 04:18.480
Division.

04:19.440 --> 04:21.660
Going to be A divided by B.

04:22.630 --> 04:23.120
Right.

04:23.230 --> 04:23.750
Sounds good.

04:23.770 --> 04:29.950
Now let's call these by consumer functional interface implementations.

04:29.980 --> 04:34.690
First, I'm going to perform Multiply, and then if you take a look at it, it gives me the option to

04:34.720 --> 04:42.520
use the and then method followed by that I'm going to pass the division and then using the accept method,

04:42.520 --> 04:44.230
we'll pass the two inputs to this one.

04:44.230 --> 04:47.200
I'm going to pass ten comma five.

04:48.480 --> 04:48.690
Right.

04:48.690 --> 04:53.310
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

04:53.310 --> 04:55.470
one which is going to be assigned to the U value.

04:55.890 --> 04:56.910
We go and take a look at it.

04:56.910 --> 04:59.580
T is the first argument and use the second argument.

05:00.580 --> 05:01.690
Now I'm gonna run this.

05:04.990 --> 05:08.920
Though it printed the multiplication result as 50 and division result as 20.

05:09.400 --> 05:16.330
If you want, you can add addition subtraction to this use case and then you can perform the whole multiplication

05:16.330 --> 05:18.040
division, addition and subtraction.

05:18.830 --> 05:21.620
Now let's go ahead and code the real use case.

05:21.620 --> 05:23.920
Whatever we coded in the previous example.

05:23.930 --> 05:30.830
In the previous example, if you remember, we use the and then method to print the student and it's

05:31.250 --> 05:32.570
and the students activities.

05:32.660 --> 05:34.100
I'm going to run this class.

05:35.540 --> 05:37.510
Is the sprint name and activities.

05:42.020 --> 05:42.980
You take a look at it.

05:43.670 --> 05:46.790
We are using we are printing the student and the student activities.

05:46.790 --> 05:50.570
I'm going to implement this using the bi consumer example.

05:51.050 --> 05:51.560
Okay.

05:51.560 --> 05:54.560
I'm going to name this one as public.

05:56.560 --> 05:57.040
Patrick.

05:58.640 --> 06:06.890
This method is not going to return anything, and this method is going to have a name as name and activities.

06:10.340 --> 06:12.170
I'm going to get the list of students.

06:13.550 --> 06:14.360
The students.

06:15.720 --> 06:16.380
And then.

06:18.550 --> 06:21.940
This list is part of the Java.util package.

06:23.640 --> 06:24.570
The next step is.

06:25.440 --> 06:28.010
You then list using the student database class.

06:28.010 --> 06:29.810
We are going to get all the students.

06:30.290 --> 06:31.070
There you go.

06:31.100 --> 06:33.200
Now we have the student list ready.

06:33.320 --> 06:34.550
What is the next step?

06:34.580 --> 06:37.880
The next step is we have to create the bi consumer.

06:40.580 --> 06:42.650
Functional interface implementation.

06:42.650 --> 06:44.270
So we are going to implement this.

06:45.210 --> 06:46.620
Name is of type string.

06:46.620 --> 06:47.890
And what are the list of activities?

06:47.910 --> 06:49.890
List of activities are.

06:51.060 --> 06:51.870
List of string.

06:52.830 --> 06:53.370
Right.

06:53.790 --> 06:55.950
Want to name this one as bi consumer.

06:56.820 --> 07:00.960
So it's going to take the name comma.

07:03.200 --> 07:04.220
Activities.

07:05.280 --> 07:08.400
The name stands for the type string and activities.

07:08.400 --> 07:10.550
Refer to the type of list of string.

07:10.560 --> 07:11.280
Right.

07:11.280 --> 07:14.760
And our job is to print both of those in the console.

07:15.780 --> 07:19.970
Gonna have name printed first followed by that.

07:21.160 --> 07:22.390
You need to have.

07:24.600 --> 07:26.130
Balloon inside the string.

07:28.150 --> 07:30.920
And then followed by that, we are going to print the activities.

07:30.940 --> 07:32.380
This is what we are going to do.

07:33.590 --> 07:40.040
So in the previous use case, we had to create a separate instance for name and separate consumer functional

07:40.040 --> 07:41.750
implementation for activities.

07:41.960 --> 07:45.550
Here we are going to achieve both in the same input itself.

07:46.960 --> 07:52.240
Let's use a far reach method to iterate the elements in the student list.

07:53.230 --> 07:55.240
And if you take a look at it it of the.

07:56.770 --> 07:58.810
Instance, US consumer rate.

07:58.900 --> 08:01.930
We can pass by consumer also as part of it.

08:03.730 --> 08:05.860
Okay, Now what we will do.

08:07.930 --> 08:08.980
The bosses shouldn't.

08:09.850 --> 08:12.130
And we are going to use the.

08:13.400 --> 08:16.790
Why consumer dot accept.

08:17.710 --> 08:19.030
I think bars is a name.

08:19.300 --> 08:21.610
Student Dot get name.

08:22.660 --> 08:24.610
And shouldn't that.

08:25.570 --> 08:26.920
Good activities.

08:27.900 --> 08:29.490
Okay, then, Dot.

08:32.500 --> 08:34.240
So we are going to use the accept method.

08:35.060 --> 08:36.770
So we have passed the exit method already.

08:36.800 --> 08:42.590
Now, as soon as we call the accept method, all these inputs are going to be passed one by one.

08:43.420 --> 08:47.740
Much argument is going to be the name, and second argument is going to be the list of activities.

08:47.770 --> 08:49.750
The next step is I'm going to call this method.

08:56.410 --> 08:56.880
There you go.

08:56.890 --> 09:02.560
It printed the values by using the by consumer interface.

09:02.570 --> 09:05.050
So now you know the difference, right?

09:05.080 --> 09:08.280
When to use by consumer and when to use consumer.

09:08.290 --> 09:14.560
If you feel that both of these conditions, meaning both of these attributes can be combined into one

09:14.560 --> 09:18.040
single instance itself, then go ahead and use by consumer.

09:18.130 --> 09:24.670
If you think like you have value putting it in a separate consumer function interface, you can use

09:24.670 --> 09:25.060
that one.

09:26.380 --> 09:32.680
There is no benefit of having any performance issue or something comparing with by consumer to consumer.

09:32.680 --> 09:35.530
It's all going to perform the same.

09:37.030 --> 09:41.170
So with this we came to the end of the bi consumer functional interface.

09:42.090 --> 09:43.080
Thank you for watching.
