Hi Everyone, welcome back to this tutorial. In this tutorial we will go ahead and implement some real time
scenario use cases using consumer functional interface.
Here I have added a new package called "data". It has two classes
One is a "Student" and another one is a "StudentDataBase".
I will be attaching these class files as part of this video so that you can download and add these to your
workspace when you view this video.
Let's go ahead and explore what we have in those classes.
The Student class has for four properties
name, gradeLevel , "double" which is a GPA and then gender. And we have a constructor and it has its equivalent
getter() and setter() methods.
And then we have a toString() method.
Let's go ahead and explore the second class which is a "StudentDataBase" class.
If you take a look at the class it has one static method which returns a list of students and it has a method
called
getAllStudents(). And it has like six students in the list.
Two second grade students,Two third grade students and Two fourth grade students,.
We are going to use this data in order to explore some of the real world use case scenarios. Lets go
ahead and code some of the real world use case scenarios in the ConsumerExample class. The first step is
what I'm going to do.
I'm going to create a method printName() and I am going to mark this class as "static".
void
And then this method is going to use the StudentDataBase in order to get all the data.
StudentDataBase
StudentDataBase.getAllStudents()
What this method is going to do this method is going to return you a list of students and the list is
going to have all those six students in it.
I am going to name this one as "studentList" = . There you go! We have the access of the StudentDataBase class
which gives you the list of six students.
Now my use case is I want to iterate and then print all the students which are present as part of this list.
So in the past what we used to do we used to create an enhanced for loop and then print all the values
one by one using that enhanced for loop.
But if you take a look at it there is an enhancement that is made to the list collection.
There is a method called forEach(). As you see here forEach() is a new method
which is one of the default() method.
It has the default key word.
And if you take a look at it this method is available since 1.8. We will be covering the default
methods as a separate section in our upcoming tutorials.
But for now we are going to use this. What is the input that it accepts? It accepts a consumer interface.
Right.
Let's go ahead and implement the consumer interface.
Now our use case is we are going to print all the values that are part of this list right.
The consumer is going to accept the type as student. In the previous example if you take a look at it accepted
the type as string. But in this case the type is going to be Student.
I'm going to name this one as "c2" = . What are we going to do as part of it ?
We are just going to accept this type.
I'm going to give a name.
which is going to be "(s)"
You can give any name you want. Followed by that,
or you can give it as a student. Followed by that we are going to just print all the values right ?. Just pass this
and our student class has a method called toString() that is going to take care of
printing all the values in the console.
Now what is the next step? The next step is this forEach() accepts a consumer implementation.
Right now we have implemented the consumer implementation.
The next step is we are going to pass this. We have the implementation ready.
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
in the console.
There you go ! Here this forEach() method accepts the consumer implementation.
What this consumer implementation does ? It takes a student as an input and then it prints that value
in the console.
That's what is happening now.
Let's explore the consumer interface by adding some more use cases.
Now I wanna create a method. What is the purpose of this method is that it is going to just print
the name and the list of activities that particular student performs. So I am gonna name this one as printNameAndActivities()
printNameAndActivities().
Ok.Name and Activities.
So let's go to the Student database class if you take a look at it
each and every student performs some kind of activities called swimming, basketball, volleyball for Student1
Student1. Let me maximize this.
Student2 performs swimming, gymnastics, and soccer. All these values are part of the activities
attribute.
This one was below somewhere
I moved it to the top.
Let's go ahead and create this use case. First step we are going to get the list of students.
Sounds good.
Now the first step is we want to just get the name of the student.
Lets create a consumer interface that is going to perform that operation for us.
It is going to perform student.getName(). There you go!
And I'm going to name this one as the "c3".
Now we have another use case we want to print the activities also.I am going to create another functional interface called
"c4"
That is just going to print the activities.
Now I want to print both of these.
First the name and followed by that
I want to print the activities.
How can I achieve that? We are going to make use of the same forEach() method which takes in the consumer.
First thing is we are going to print the Name and then if I press "." I have another method called andThen().
This is the method which is going to take in another consumer interface implementation and perform
the operation.
So this concept is called consumer chaining.
Now what I'm going to do I'm going to call this method.
I am going to run this. Let's run this and check. There you go !
It printed the name followed by the listOfActivities(). Sounds good !
What is the next step ? I don't want this name to be printed here.
I want the name to be printed in the same line as a list of activities. You can easily achieve that.
Lets go ahead and do this. There you go! It printed the name followed by that student activities.
Now I want to perform some more additional conditioning on this output.
Basically I want to perform some filtering operations if the grade is greater than or equal to 3.0
then I want to print those list of Students.
If you go and take a look at the StudentDataBase
We have out of the six students.
We have only four students who are of greater than or equal to the grade level as "3".
For this purpose,
What I'm going to do?
I'm going to create another method , I want to do the same operation.
I'm going to print the name followed by the list of activities. I am going to name this one as printNameAndActivitiesUsingCondition().
But if we take a look at it I'm duplicating the code. As we know from prior experiences.
The code duplications leads to poor quality code and it leads to a lot of errors and issues in the code.
Right.
So What I going to do ?
I'm going to move this to the top
and then I'm going to mark this static because we are using these consumer interface implementation
in the static methods.
In that case you need to mark that one as static. I am going to remove this.
We have successfully avoided the code duplication.
OK now you're going to perform the filtering operation here.
Right.
What I'm going to do, I'm going to write an inline Lambda consumer interface implementation.
So the forEach() method is going to give you access to the student.
And then you're going to perform the filtering operations right.
So what is the Filter ? If the student grade is greater than
student.getGradeLevel()>=3 then just print those students.
Now this is where the code reusability comes into picture.
I'm going to use the same consumer implementation and then I'm going to pass the student to that one.
Now I'm going to print this. In order to avoid the confusion I'm going to comment this one out.
Lets see what is the result.
If you take a look at the result it is still confusing what I'm going to do.
I am going to add a sysout on top of this.This sysout is going to print the method name followed by the result.
Here it is going to print this.
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
are six students who got printed here. So what is that we achieved as part of this we achieved the code re-usability
as part of this activity.
Right.
So that's what lambda embraces. Lambda embraces the core re-usability.
Lets you want to use the similar kind of condition
somewhere below. Lets say you just want to print the student name we can still do the by reusing the "c3"
"c3"
Now I am gonna add one more filter.
So I want to print all the students whose grade level is greater than or equal to "3".
And then I wanna to perform the GPA level filter too. If the GPA is greater than or equal to 3.9
then print those students. Now in the result we have four students. Lets see after the run of this program what
is the result that it is going to print ? If you take a look at the result it printed only 3 students.
Because if you go and take a look at StudentDataBase out of the four students who are
having the grade greater than or equal to 3, one student is not matching the filter value of the GPA level
filter of greater than or equal to 3.9
That's the reason why we see only three students in the result.
So out of the two conditions if you want to reuse one of the conditions below in the code for a different
use case then it is not possible now with the set up that we have. We have to duplicate the code again.
To avoid this
there is a dedicated functional interface which is available as part of Java 8
for this purpose. We'll cover those in our upcoming tutorials.
I'd like to highlight one more add on to the consumer functional interface before we wind up this session.
In here we just chained two consumer function interface
If you want to add you can add "N" number of an Consumer Functional Interface to the
andThen() method.
So we can use the andThen() method and then we can chain"N" number of consumer functional interfaces.
With this we came to the end of this tutorial.
Thank you for watching!