WEBVTT

0
00:00.410 --> 00:05.900
Hi Everyone,  welcome back to this tutorial. In this tutorial we  will go ahead and implement some real time

1
00:05.900 --> 00:10.480
scenario use cases using consumer functional interface.

2
00:10.730 --> 00:14.140
Here I have added a new package called "data". It has two classes

3
00:14.140 --> 00:16.880
One is a "Student" and another one is a "StudentDataBase".

4
00:16.980 --> 00:22.670
I will be attaching these class files as part of this video so that you can download and add these to your

5
00:22.670 --> 00:25.540
workspace when you view this video.

6
00:25.940 --> 00:28.690
Let's go ahead and explore what we have in those classes.

7
00:31.660 --> 00:33.900
The Student class has for four properties 

8
00:33.920 --> 00:43.520
name,  gradeLevel , "double"  which is a GPA and then gender. And we have a constructor and it has its equivalent

9
00:43.520 --> 00:45.530
getter() and setter() methods.

10
00:45.710 --> 00:48.330
And then we have a toString() method.

11
00:48.750 --> 00:52.720
Let's go ahead and explore the second class which is a "StudentDataBase" class.

12
00:52.840 --> 00:59.540
If you take a look at the class it has one static method which returns a list of students and it has a method

13
00:59.530 --> 01:00.120
called

14
01:00.140 --> 01:04.060
getAllStudents(). And it has like six students in the list.

15
01:05.170 --> 01:09.510
Two second grade students,Two third grade students and Two fourth grade students,.

16
01:09.580 --> 01:16.710
We are going to use this data in order to explore some of the real world use case scenarios. Lets go

17
01:16.730 --> 01:24.060
ahead and code some of the real world use case scenarios in the ConsumerExample class. The first step is

18
01:24.070 --> 01:25.300
what I'm going to do.

19
01:25.450 --> 01:33.210
I'm going to create a method printName() and I am going to mark this class as "static".

20
01:33.430 --> 01:37.030
void

21
01:37.170 --> 01:44.820
And then this method is going to use the StudentDataBase in order to get all the data.

22
01:45.010 --> 01:45.880
StudentDataBase

23
01:46.000 --> 01:48.460
StudentDataBase.getAllStudents()

24
01:48.590 --> 01:55.600
What this method is going to do this method is going to return you a list of students and the list is

25
01:55.600 --> 01:58.360
going to have all those six students in it.

26
02:01.970 --> 02:09.650
I am going to name this one as "studentList" = . There you go! We have the access of the  StudentDataBase class 

27
02:09.770 --> 02:12.910
which gives you the list of six students.

28
02:13.080 --> 02:22.250
Now my use case is I want to iterate and then print all the students which are present as part of this list.

29
02:22.620 --> 02:28.980
So in the past what we used to do we used to create an enhanced for loop and then print all the values

30
02:28.980 --> 02:31.160
one by one using that enhanced for loop.

31
02:31.350 --> 02:36.930
But if you take a look at it there is an enhancement that is made to the list collection.

32
02:36.990 --> 02:41.570
There is a method called forEach(). As you see here forEach() is a new method

33
02:41.700 --> 02:44.180
which is one of the default() method.

34
02:44.340 --> 02:45.820
It has the default key word.

35
02:45.930 --> 02:51.370
And if you take a look at it this method is available since 1.8. We will be covering the default

36
02:51.370 --> 02:54.480
methods as a separate section in our upcoming tutorials.

37
02:54.510 --> 03:01.220
But for now we are going to use this. What is the input that it accepts? It accepts a consumer interface.

38
03:01.230 --> 03:01.700
Right.

39
03:01.770 --> 03:04.730
Let's go ahead and implement the consumer interface.

40
03:05.130 --> 03:11.460
Now our use case is we are going to print all the values that are part of this list right.

41
03:11.510 --> 03:17.880
The consumer is going to accept the type as student. In the previous example if you take a  look at it accepted

42
03:17.940 --> 03:22.160
the type as string. But in this case the type is going to be Student.

43
03:22.350 --> 03:28.340
I'm going to name this one as "c2" = . What are we going to do as part of it ?

44
03:28.410 --> 03:31.440
We are just going to accept this type.

45
03:31.440 --> 03:32.870
I'm going to give a name.

46
03:32.880 --> 03:34.030
which is going to be "(s)"

47
03:34.080 --> 03:36.620
You can give any name you want. Followed by that, 

48
03:36.620 --> 03:46.670
or you can give it as a student. Followed by that we are going to just print all the values right ?. Just pass this

49
03:46.670 --> 03:52.090
and our student class has a  method called toString() that is going to take care of

50
03:52.140 --> 03:54.610
printing all the values in the console.

51
03:54.620 --> 04:01.510
Now what is the next step?  The next step is this forEach() accepts a consumer implementation.

52
04:01.520 --> 04:04.860
Right now we have implemented the consumer implementation.

53
04:04.880 --> 04:10.870
The next step is we are going to pass this. We have the implementation ready.

54
04:10.870 --> 04:18.170
Now I'm going to call this method. Call this method what it is going to do? It is going to print all the values

55
04:18.520 --> 04:19.240
in the console.

56
04:22.420 --> 04:27.840
There you go ! Here this forEach() method accepts the consumer implementation.

57
04:28.030 --> 04:33.880
What this consumer implementation does ? It takes a student as an input and then it prints that value

58
04:33.880 --> 04:34.740
in the console.

59
04:34.810 --> 04:36.920
That's what is happening now.

60
04:36.970 --> 04:41.930
Let's explore the consumer interface by adding some more use cases.

61
04:41.940 --> 04:48.970
Now I wanna create a method. What is the purpose of this method is that it is going to just print

62
04:48.970 --> 04:55.870
the name and the list of activities that particular student performs.  So I am gonna name this one as printNameAndActivities()

63
04:55.980 --> 04:58.240
printNameAndActivities().

64
05:02.250 --> 05:04.690
Ok.Name and Activities.

65
05:05.030 --> 05:09.060
So let's go to the Student database class if you take a look at it

66
05:09.100 --> 05:15.970
each and every student performs some kind of activities called swimming, basketball, volleyball for Student1

67
05:15.980 --> 05:18.280
Student1. Let me maximize this.

68
05:18.350 --> 05:25.400
Student2 performs swimming, gymnastics,  and soccer. All these values are part of the activities

69
05:25.610 --> 05:26.460
attribute.

70
05:26.780 --> 05:28.790
This one was below somewhere

71
05:28.790 --> 05:30.260
I moved it to the top.

72
05:33.570 --> 05:40.850
Let's go ahead and create this use case. First step we are going to get the list of students.

73
05:40.930 --> 05:41.930
Sounds good.

74
05:41.930 --> 05:45.660
Now the first step is we want to just get the name of the student.

75
05:45.870 --> 05:52.540
Lets create a consumer interface that is going to perform that operation for us.

76
05:52.540 --> 05:57.680
It is going to perform student.getName(). There you go!

77
05:57.870 --> 06:01.340
And I'm going to name this one as the "c3".

78
06:01.470 --> 06:08.650
Now we have another use case we want to print the activities also.I am going to create another functional interface called

79
06:08.740 --> 06:10.110
"c4"

80
06:10.150 --> 06:17.430
That is just going to print the activities.

81
06:17.640 --> 06:19.690
Now I want to print both of these.

82
06:19.740 --> 06:21.720
First the name and followed by that

83
06:21.740 --> 06:23.250
I want to print the activities.

84
06:23.400 --> 06:30.580
How can I achieve that? We are going to make use of the same forEach() method which takes in the consumer.

85
06:30.770 --> 06:37.930
First thing is we  are going to print the Name and then if I press "." I have another method called andThen().

86
06:38.000 --> 06:44.660
This is the method which is going to take in another consumer interface implementation and perform

87
06:44.660 --> 06:45.680
the operation.

88
06:45.710 --> 06:51.360
So this concept is called consumer chaining.

89
06:51.500 --> 06:53.930
Now what I'm going to do I'm going to call this method. 

90
07:00.530 --> 07:08.080
I am going to run this. Let's run this and check. There you go !

91
07:08.150 --> 07:13.060
It printed the name followed by the listOfActivities(). Sounds good !

92
07:13.160 --> 07:16.880
What is the next step ? I don't want this name to be printed here.

93
07:16.880 --> 07:22.370
I want the name to be printed in the same line as a list of activities. You can easily achieve that.

94
07:22.880 --> 07:30.160
Lets go ahead and do this. There you go! It printed the name followed by that student activities.

95
07:30.170 --> 07:34.680
Now I want to perform some more additional conditioning on this output.

96
07:34.940 --> 07:40.940
Basically I want to perform some filtering operations if the grade is greater than or equal to 3.0

97
07:41.180 --> 07:43.480
then I want to print those list of Students.

98
07:43.570 --> 07:45.420
If you go and take a look at the StudentDataBase

99
07:45.620 --> 07:47.990
We have out of the six students.

100
07:48.110 --> 07:56.000
We have only four students who are of greater than or equal to the grade level as "3".

101
07:56.000 --> 07:56.650
For this purpose,

102
07:56.650 --> 07:57.660
What I'm going to do?

103
07:57.710 --> 08:02.430
I'm going to create another method , I want to do the same operation.

104
08:02.480 --> 08:08.540
I'm going to print the name followed by the list of activities. I am going to name this one as printNameAndActivitiesUsingCondition().

105
08:08.780 --> 08:14.400
But if we take a look at it I'm duplicating the code. As we know from prior experiences.

106
08:14.440 --> 08:22.570
The code duplications leads to poor quality code and it leads to a lot of errors and issues in the code.

107
08:23.050 --> 08:23.530
Right.

108
08:23.640 --> 08:25.130
So What I going to do ?

109
08:25.460 --> 08:27.090
I'm going to move this to the top

110
08:34.920 --> 08:40.860
and then I'm going to mark this static because we are using these consumer interface implementation

111
08:41.020 --> 08:42.480
in the static methods.

112
08:42.540 --> 08:47.790
In that case you need to mark that one as static. I am going to remove this.

113
08:47.850 --> 08:51.400
We have successfully avoided the code duplication.

114
08:51.550 --> 08:55.930
OK now you're going to perform the filtering operation here.

115
08:57.490 --> 08:58.230
Right.

116
08:58.390 --> 09:05.710
What I'm going to do,  I'm going to write an inline Lambda consumer interface implementation.

117
09:05.770 --> 09:10.680
So the forEach() method is going to give you access to the student.

118
09:11.200 --> 09:15.320
And then you're going to perform the filtering operations right.

119
09:15.420 --> 09:18.850
So what is the Filter ? If the student grade is greater than

120
09:21.970 --> 09:28.990
student.getGradeLevel()>=3 then just print those students.

121
09:29.270 --> 09:33.390
Now this is where the code reusability comes into picture.

122
09:33.470 --> 09:42.410
I'm going to use the same consumer implementation and then I'm going to pass the  student to that one.

123
09:42.570 --> 09:52.050
Now I'm going to print this. In order to avoid the confusion I'm going to comment this one out.

124
09:52.320 --> 09:53.870
Lets see what is the result.

125
09:53.880 --> 09:58.670
If you take a look at the result it is still confusing what I'm going to do.

126
09:58.730 --> 10:05.890
I am going to add a sysout on top of this.This sysout is going to print the method name followed by the result.

127
10:09.580 --> 10:11.040
Here it is going to print this.

128
10:19.240 --> 10:24.190
If you take a look at it. There are only four students who got printed but if you take a look at this one there

129
10:24.190 --> 10:34.010
are six students who got printed here. So what is that we achieved as part of this we achieved the code re-usability

130
10:34.100 --> 10:36.000
as part of this activity.

131
10:36.040 --> 10:36.530
Right.

132
10:36.710 --> 10:41.760
So that's what lambda embraces. Lambda embraces the core re-usability.

133
10:41.820 --> 10:45.030
Lets you want to use the similar kind of condition

134
10:45.050 --> 10:52.020
somewhere below. Lets say you just want to print the student name we can still do the by reusing the "c3"

135
10:52.470 --> 10:53.450
"c3"

136
10:53.550 --> 10:56.220
Now I am gonna add one more filter.

137
10:56.600 --> 11:01.710
So I want to print all the students whose grade level is greater than or equal to "3".

138
11:01.850 --> 11:12.350
And then I wanna to perform the GPA level filter too. If the GPA is greater than or equal to 3.9

139
11:12.440 --> 11:20.430
then print those students. Now in the result we have four students. Lets see after the run of this program what

140
11:20.430 --> 11:26.340
is the result  that it is going to print ? If you take a look at the result it printed only 3 students.

141
11:26.430 --> 11:32.250
Because if you  go and take a look at StudentDataBase out of the four students who are

142
11:32.550 --> 11:41.240
having the grade greater than or equal to 3,  one student is not matching the filter value of the GPA level

143
11:41.240 --> 11:43.760
filter of greater than or equal to 3.9

144
11:43.760 --> 11:48.170
That's the reason why we see only three students in the result.

145
11:49.740 --> 11:56.520
So out of the two conditions if you want to reuse one of the conditions below in the code for a different

146
11:56.520 --> 12:02.880
use case then it is not possible now with the set up that we have. We have to duplicate the code again.

147
12:02.950 --> 12:03.750
To avoid this

148
12:03.770 --> 12:08.600
there is a dedicated functional interface which is available as part of Java 8

149
12:08.880 --> 12:12.530
for this purpose. We'll cover those in our upcoming tutorials.

150
12:12.530 --> 12:17.970
I'd like to highlight one more add on to the consumer functional interface before we wind up this session.

151
12:18.420 --> 12:21.570
In here we just chained two consumer function interface

152
12:21.560 --> 12:29.180
If you want to add you can add "N" number of an Consumer Functional Interface to the

153
12:29.560 --> 12:30.730
andThen() method.

154
12:30.850 --> 12:37.610
So we can use the andThen() method and then we can chain"N" number of consumer functional interfaces.

155
12:38.860 --> 12:40.990
With this we came to the end of this tutorial.

156
12:41.110 --> 12:41.890
Thank you for watching!