Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the map method, flat map method and the filter method that
are part of the optional class.
The first step is I'm going to create a class called Optional map Flat Map example.
Let's make this class executable by adding public static void main method.
So the methods that we are going to explore this presentation mode.
Are going to be filter flat, map and map.
So the first method that we are going to take a look at is a filter method.
Right.
I'm going to create a method specific for this one, and this method is not going to return anything
and then the method name is going to be optional filter.
So I'm going to use the student database.
Dart Pyrone supplier Dart get this is going to give me the actual student.
And then we are going to encapsulate that inside the optional object.
So this will return optional of type student.
Right.
The next step is let's get this value optional of student.
Let's give it a name, right?
The name is going to be let's import the student.
The name is going to be student optional.
There you go.
So we have the student optional, which has a student inside it, and the student is going to be Adam.
Now you want to apply some filter, right?
So filter is some operation which we have seen in the previous tutorials as part of the stream operation.
Now optional also have a filter method that it supports.
If you want to apply some filter before you get to the actual value.
So what I'm going to do, I'm going to do student optional dot filter so you can pass a predicate.
The predicate that I'm going to use.
We all know this optional object encapsulate a student, so I'm going to use a student student dot get
GPA greater than or equal to 3.5.
So I'm going to give the value as 3.5.
If that is there, then.
I'm going to build a pipeline here.
If that is there, then do a.
If present, then print the student.
Now let's go ahead and run this one.
So in the public static void main method, I'm going to call the optional filter method.
This is going to.
Check the student GPA.
If it is greater than 3.5, then it is going to get access to that student object and then printing
it.
If you want some other operation that needs to be performed, you are welcome to do it.
But in here I want to keep this example really simple.
That's the reason why I'm just printing it here.
Let's go ahead and check what is the student's GPA?
GPA?
The GPA is 3.6, right?
Let's say I'm going to give the value as 4.2.
So in that case, nothing will happen because this filter is going to filter out the student and the
student won't even reach the if present method.
Right?
So this is all about the filter method.
The filter method gives you the flexibility to apply the filter operations on an optional object.
Again, this is not a stream, this is an optional object.
Now let's go ahead and explore the map method.
I'm going to come to the flatMap method in a bit, but we'll explore the map method.
We talked about map method and we covered map method a bit in the previous tutorial.
I'm just going to give you the similar kind of example.
Nothing new here.
So what I'm going to do, I'm going to do, I'm going to create a method public static void, and then
this is going to be optional map.
And I'm going to do the same operation here.
So now we have access to the optional method in here, what we are going to do.
Now we have the optional student ready.
The next step is we are going to check if the student is present.
Right.
That should be the first check.
Then what we are going to do, we are going to get the name of the student or you can apply.
We can change this one with the filter also.
Let's go ahead and do that.
So this is going to be optional dot filter In this filter, I'm going to use the same code, whatever
that is here.
Give the logic.
This is going to be 3.5 because Adam is having really poor GPA, not poor.
It's an average GPA.
And then what is the next thing?
The next thing is going to be the map method.
In the map method, we are going to get the student name right.
And what is this going to return?
If you go to the map method, Map method returns an object of type optional.
Right?
Let's go ahead and get that optional of type string, because if you take a look at the student name,
this is of type string.
Now what we can do, we can print this, we print it, it is going to give you the actual value that
it encapsulates.
If you run this example, I didn't call this method it.
So in here, what we are going to do, we are going to call the optional map
optional map.
And then let's go ahead and run this example.
So when you run this example, this is going to call this one and it is going to print the name of the
student, which is nothing but Adam.
There you go.
It printed the value as Adam.
Now let's go ahead and explore the flat method.
I'm thinking of covering this flat method as a next tutorial.
For now, we have covered the filter and map method As part of this tutorial, let's go ahead and explore
the flat flat map in the next tutorial.
With this we came to the end of this tutorial.
Thank you for watching.