Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the flatMap method that is part of the optional class.
The first step which I'm going to do is that I'm going to introduce a new class called Bike.
New class called Bike.
And this is going to have.
Private string name and model name and model.
And we are going to generate.
The getters and setters for this one.
In red.
Select all the properties and click okay.
And then I'm going to generate the two string method to.
There you go.
So we have the bike object.
Sorry, the bike class created.
Now I'm going to build a relationship between the bike class and the student class.
If you take a look at the student class, we are going to incorporate bike in here.
So the use cases, not all the students will have the bike.
So the students, whoever has a bike, will use their bike to come to the school.
How can we represent that?
So this is going to be an optional property because not all the students is going to have the bike.
How can we represent that using optional so private optional of bike and just give it a name.
This is going to be by default optional dot empty.
So as a developer, just by looking at this model, which is the student class, I can say, okay,
I have an optional object which represent this is not a mandatory property that each and every student
will have.
Right now.
Let's go ahead and generate the getter and setter method for this.
Rate getter and setter.
Okay.
And then let's read the two string also.
Replace the existing two string method, Click on.
Yes, there you go.
And what I'm going to do, as you mentioned, not all the students are going to have this rate at the
bike.
I'm just going to add it to student one or what I can do.
I can have this added just to the supplier bike, bike equal to new bike, and then the bike is going
to have a name and model.
So set name equal to ABC and model equal to.
Let model equal to x, y, z.
So what I'm going to do, I'm going to create a student instance and then return it.
Student Equal to and then Student Dot set bike equal to going to be optional.
Dot off.
Nullable.
Bike and then return this shirt.
So now we added the supplier method, right?
So we added the supplier method in the supplier student supplier.
We added this bike optional to the student object and we are returning that as part of this supplier.
Now let's go ahead and code this.
Using the optional bike.
So what is the scenario that we are going to code?
We are going to code the flat map example.
Let me put this in presentation mode.
In here, what I'm going to do, I'm going to create public static.
Going to be.
flatMap.
Right.
Optional.
flatMap.
And then I'm going to call similar code.
So this is going to give me the it's going to be a wide method.
This is going to give me the optional object back.
Right.
So now my use case is I want to get to the bike name.
I want to get to the bike name and print that bike name as an output.
That's what I want to do.
How do I get to it?
The first thing I'm going to call student optional and then I'm going to have the filter method to filter
the student, the student GPA.
Is greater than 3.5 then only greater than or equal to 3.5.
Then only I want to get to the bike name.
And then.
So what is the type of bike name in the student class?
The type is of type optional, right?
So in order to get to the optional object inside an optional object, because it's already an optional
object, you want to get to the optional object which is inside another optional object.
The only way to get into it is by using flatMap.
So if you remember for the streams API we have used the flatmap.
flatMap is used to flatten the stream which represents multiple elements actually.
So let's say you have a list of elements inside a object.
Then in those kind of scenarios, if you want to flatten that stream to each and every element to represent
a stream, then we have used flatMap in that example.
Now we are going to use flatMap in the optional context.
So in the optional context, flatMap is used to get to the actual value of the optional object inside
another optional object.
Dot get bike.
And we're going to map that to.
Pike dot get name.
This is what we want, right?
And then this is going to return a student.
It is going to return us string name and it's going going to be of type optional.
Was a map again and optional is going to return another optional right here.
It encapsulates the name as a the bike name inside the optional.
Object.
So here we are going to call this method and this is going to print the bike name.
Let's go ahead and execute this program.
If we executed it gave you the optional ABC.
You want to get to the name of it?
We'll do the If percent.
Name Dot if present then.
We are going to print it right now.
Yes, you can give a different value here.
Name is.
Yes.
Let's run this.
Run.
Thus it gave you the name as ABC.
Okay, let's go through this pipeline again.
So what happens in this line?
In this line, this is going to still return optional of shouldn't.
Now we have to access the bike which is inside.
If we take a look at it, the bike will be inside this as an another optional object.
Right.
If you want to get to the access, you want to want to get to the actual bike, you need to perform
the flatMap.
This is going to give you the optional of bike as a output.
That's what it is going to do, but it needs to get into the student object first, which is of type
optional and then get to the bike optional object.
So that's the reason why we need flatMap here.
And this gives optional bike as an output and using that we are mapping and getting the name of that
and returning that as an optional of string.
So this is what happens behind the strings.
So here we have a two levels of optionals that's the reason why we need to have the flatMap.
So here this returns the optional of bike as an output at this step.
I hope the concepts are clear.
So this is all about flatMap.
With this, we came to the end of this tutorial.
Thank you for watching.