Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the is present and if present method that is part of the optional
class in here, what we are going to do.
I'm going to create a class in the optional package.
The class name is going to be optional present example and then I'm going to have the class executable
by adding the public static void main method.
Let me put this in presentation mode.
So the method that we are going to explore is going to be is present.
We have seen some examples of is present already, but still I would like to cover this in this tutorial
and if present.
First, let's take a look at the Is present method.
So I'm going to create an optional object of type string.
I'm going to give it a name optional.
And then I'm going to use the off nullable method, which is part of the optional to create an optional
object optional dot of nullable.
I'm going to give the string as hello optional and in here what I'm going to do, I'm going to use the
Is present method and then show you what is that it is going to give you as an output.
The optional dot is present.
Let's run this example and then check the result.
But on this example that is going to give me the value as true.
Basically, it checks whether this optional object has some value in it or not.
Let's say I'm going to pass null here.
Those kind of scenarios is going to give you the value as false because this option doesn't have any
value inside this.
Basically, if you want to get to the value that it encapsulates what we do, we do optional Dot is
present.
This might this content might be a repeated content from the previous tutorial.
But if you are if you're already aware of all this content, you can go ahead and move to the next topic,
which I'm going to explain in a bit.
And then this is going to be optional dot get, which will give you access to the actual value that
it encapsulates.
Here you go.
It printed the Hello optional.
Now let's go ahead and explore the if present method.
So if present method is again almost similar to the is present method, but it gives you one additional
logic.
Let's explore that.
So I'm going to do the optional dot if present.
If you take a look at it, if present takes in an input as a consumer, we all know consumer takes in
an input and it doesn't return any output, right?
So what I'm going to do as soon as I type in s, IntelliJ is going to give me the recommendation to
implement it and then I'm going to just print that string.
So what it does.
It does two things.
First thing, it checks whether the optional has a value, and after that it it gives you the access
to the actual value.
And you can do any kind of operation you want.
Here I'm just printing it.
If you want some other operation, you can still do it.
Let's say you want to call some other method or some other service.
If that object is having any value, then you can go ahead and do that too.
So the only difference is that is present will just tell you whether the value is present or not.
But if present will tell you there is a value present or not, and then it gives you an additional operation
that you can perform on top of that optional object.
So this is all about the present and if present that are part of the optional class.
With this, we came to the end of this tutorial.
Thank you for watching.