Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the orals method, the get method and the orals throw method
that are part of the optional class.
The first step is I'm going to create a class called Optional.
Or else.
Example.
And let's make this class executable.
The adding public static void main method.
So the methods that we are going to explore are.
Arrows.
And then get.
And then else.
So.
The first method that we are going to explore is a orals method.
I'm going to create a method.
Oblique static.
And this method is going to return a string.
And it's going to be optional or else.
Right.
So what is the first step?
We need an optional object.
I'm going to use optional dot optional label.
I'm going to make use of the student database class as part of this tutorial.
Student base dot students supplier dot get this is going to give you a valid student.
So if you go to the students player, this will give you a student who has the name as Adam grade level
as two and the GPA as 3.6.
And the gender is male.
And these are the activities that Adam is participating.
So since we do a lot of nullable, we have seen this in the previous tutorial.
This is going to return you an optional object of type student because the input to the the input to
the of nullable method is a student object.
Let's give it a name.
Student optional.
So there we go.
We have the student optional.
The next thing is we are going to explore the method, right?
So I want the name of the student.
That's the use case, which I'm trying to achieve here.
What would I do?
Optional.
A student optional dot.
I'm going to use a map method which we explored in our first tutorial.
We'll be exploring more on the map in the future tutorial.
For now, we are going to use a map method in the student.
We want the name right?
So we'll give Student Colon.
Colon.
We are going to use the method reference syntax here and then give the get name.
In the place of this, you get access to all these different methods that optional supports.
In here we are going to use the orals.
What I'm going to do, I'm going to return default under what scenario this code will get executed.
I will explain that in a moment.
So this is going to be string.
Name equal to this and return name.
So in here we get the student that is going to map the student name.
So if there is a valid value, then it is going to return the actual name.
Let's run this and check the result.
So what I'm going to do.
I'm going to do System.out.println and then call optional.
Else Right.
Let's run this and check the result.
The result is going to be Adam.
So under what scenarios?
It would return default.
So I'm going to comment this line.
Or I want to come on these two lines.
What I'm going to do, I'm going to do.
Thus an optional dot of nullable and then I'm going to pass null.
If you pass null, we all know this is going to return the optional dot empty.
So it's going to return optional dot empty object.
Basically option empty.
Right.
So when it checks this, it is going to try to map the student object and try to get the name.
But since it is empty, it won't be able to execute this line and then it is going to execute the else
and return you the default as a result.
So I'm going to run this example.
There you go.
We got the default as a result.
So if you want to return a default values in your use cases, if the actual value is null in those kind
of scenarios, you can use the or else method.
Okay, let's go ahead and explore the next one, which is the get.
I'm going to copy the same method.
This method is also going to return you the string, but it is going to be or else get.
And I'm going to use the same code here, whatever we had.
And then what we are going to do.
We're going to do the same logic not changing anything here.
That this is going to be orals get So orals get if you take a look at it, it it accepts a supplier
instead of a simple string, it is going to accept a supplier.
So the supplier we know the syntax for supplier supplier does.
I'm going to use a lambda syntax here.
Supplier doesn't take any input, but it is going to return you an output, right?
That's what supplier is for.
So here I have created the simple lambda implementation of the supplier, which doesn't take any input,
but it is going to return you the output.
So this is all about supplier.
This is all about sorry, optional or else get method.
It's going to return the name as an output.
So go ahead and run this.
I'm going to name it accordingly.
So this is orals.
Get right.
This is Aurel's get.
And this is Aurel's.
Gonna run this and check the result.
So the result is going to be default.
Here what's happening?
So here, if we take a look at it, we are not calling this.
That's the reason why it printed default because I'm expecting the name a student because I'm supplying
a valid student here.
Let's run this and check the result.
We take a look at it.
The result is.
Adam Right.
So under what scenarios this would return default if this is of.
Type.
Now, in that case, it is going to call this or else get method and the supplier is going to return
the default as output.
There you go.
Both are printing the same result.
So the only difference is that this accepts the actual string.
This accepts a supplier.
That's the only difference between Orioles and Orioles get.
Now let's go ahead and explore the Orioles throw.
That's the next one, right?
When I record this.
There's going to be or else.
Row and in here.
Let's go.
This one.
I'm going to copy this line of code where it's going to return a valid student first, and then we'll
go back to the null student.
And then it's going to be the same code.
In the place of orals get we are going to use orals through orals through is again a it is accepting
a supplier.
Right.
What we will do.
Will implement the supplier using the lambda expression.
And then this is going to return a runtime exception.
We all know Throw is going to.
Throw an exception back to the caller.
Right?
That's what it is.
So.
New runtime exception.
And I'm going to give let me put this below so that the code will be clear on the screen.
It is going to return you the value as no data.
Available.
Now let's run this first.
Basically, it is going to return the name of the student and then we'll pass the value as null in here.
It is going to return the runtime exception return name.
So.
This one.
Aurel Stroe.
Copy this and put it here.
The method that we are going to call is optional.
Stone.
Let's run this.
In this run, it is going to return you the actual name of the student, because we are getting the
name of the student from the student database.
So it printed this.
Now what I'm going to do, I'm going to return.
I'm going to pass null In this case, it is not going to execute this code.
It is going to execute the throw code.
Let's go ahead and run this one.
If you run this one, this gave you the exception back.
So basically, Java dot Lang dot runtime exception.
No data available in your use case.
If you feel like if there is no data available, then I want to throw the exception back to the caller
and those kind of use cases you would use or else throw.
If you want the default value in those kind of use cases, you would either use a or else or else get.
So this is all about the or else or else get and or else throw methods.
With this, we came to the end of this tutorial.
Thank you for watching.