Detailed Notes
Welcome. Today we are taking an introductory look at the Java Optional Class. We will focus primarily on the Optional Class and what it is for. Then we will cover why it is useful and how to use it in our code. Finally, we wrap up with four hands examples of using the different types of Optional methods in our code.
What is Java the Optional Class?
The optional class is essentially a container object, which may or may not contain a non-nullable value.
Why should we use the Java Optional Class?
One of the biggest problems with Java is that when we create variables or methods with return types, our intent is unclear if the value of these will be null.
String name; String getName();
void doSomething(){ assertThat("My Name").isEqualTo(getName()); } He is a simple example where we have a string name, a method that returns a string getName(), and then some method that tries to compare a string name equal to our getName(). The problem with this is name could be null and throw a NullPointerException at runtime. However, just reading the code, we don't know, for instance, if the name was ever instantiated or populated so it could contain a null value.
The code is very unclear on the intent of the name. Should it always be populated, or could the name be null? How, as developers, do we know the name could be null? We don't because the name is just specified as a name.
Unfortunately, this approach leaves it up to the developer to determine the intent of the variable if it can be null. This leads the developer to implement many null checks within their code.
if (name != null) { then do something... } Primarily what do we do if the name is not known? What do we want to do with it? If we don't know that name could be null, then when we try to use it, we will get a runtime exception all over the place when we try to use the name. This is where Optional comes in.
Why Is Java Optional useful?
When we use the optional class, we imply our intent that a variable or our method could potentially return a no result or a null. Because the Optional class gives us the ability to wrap our variables and methods. Telling the developer that this variable is optional and that name might not have a value in a particular instance; therefore, the variable will not return a value.
Additionally, the Optional class has some methods that handle how we do null pointers or null checks. Same with methods, like our Optional String getName. So this time, in the above code, we create a new Optional String for both the variable and method getName.
By doing this, we are specifying that name may return no result so that it could be null, but because we're using Optional, we will know that we need to handle that case.
Transcript Text
foreign [Music] [Music] next let's look at example three here we are going to look at how to get an object out of an optional and then we're going to look at the conditional methods that come with the optional class like if present if present or else or else or else get or else throws all right so example three we're going to start by getting the object from an optional so we again will take that string from our previous example so we have our string onion and we have our optional string populated string optional from onion now how do we get the string back out of our optional variable here to do that we can do an assertion so we can assert that our populated string dot get so the dot get method here returns us the value the object value if it is present so if we have a object in our optional that is present we can use get to return that object so get will return us on you so we have get so let's verify that so if we're getting back this onion object we should be able to do is equal to is equal to and it should be the same as onion all right so we created our string object onion we converted string to an optional called populated string optional and we're using the get method to get our string back out of our optional and then make sure that is equal to the string that we converted it to so if I save this and run everything passes okay now let's look at the next option all right so two let's look at null checks versus optionals if present so in the old days or the old way of doing this in order for us to check to see if onion is null we need to start with the traditional if onion not equal to no then do something and in this case let's just print out the length of onion and onion dot blank all right so if I run this I should get five and I get 5. all right so we confirmed that onion is not null however if we want to use optionals we no longer have to do this and I'll check we can actually use the optional object to simplify this logic so here we can start with our populated string so we've this is the optional string we created and converted from string onion so we start out with the populated string optional we start with is present if it's present it'll return true that means we can do something with it and then I can use lambdas to actually pull out the value so all right so if it's present I can then get the object inside of option so I can do topping so this will take the optional value so it will return me this string as topping and then we can do the same logic here but in this case we want topping oh that is person if present sorry so if I want to do the same logic without having to do this if check all I have to do for readability is take my optional if present pull out the optional value as topping and then system out print line to topping length close much easier easier to read and avoids all these if statements all over the place in our code so if I run this they should be the same and I get five and five so this is how you do a null check with optional using IF present all right so three let's look at the Java 9 introduction of If present or else so this now allows us to do an if else within the optional chaining with using our method chain so here we're going to start out with our populated string again copy this all right so we have our populated string if present write out the length all right well that doesn't really tell me much so let me change this just so we can for clarity so popping chosen Plus and now we can actually get the value so I will just name this value so I'm getting the value of my string and we're writing out the value however if I you change this from if present to if present or else so if present or else takes two options or two parameters so we get a runnable empty action or we have an action and these allow us to create internal functions to override these values so the first one we did was the value of or value and then the second one comma is going to be an empty because we don't have a value and no toppings so now if I use if present or else if I have a value do something otherwise do something else so I can actually write that logic within the method chain so now if I run this I should get toppings chosen I do next let's look at use or else to override nulls so let's create a no toppings string of null and if we do an or else we can create a string topping equals foreign optional Dot of noble no toppings Dot or else cheese now here we're going to pass in a null optional however if it is null we want to return cheese so we're essentially sending a default value here so to verify that we can do assert that topping dot is equal to cheese so let's run this and we pass so if we have no toppings if our object that we're converting to optional is null we're going to set the value to cheese all right now let's look at use or else get so here we're going to take topping again so we'll just take our string topping and we're going to take an optional of nullable and we're going to pass in no toppings so we're going to do no toppings again now this time we're going to do or else but we're going to use the functional interface so we're going to use or else get so if it's nullable we can do or else which sets the value or we can do or else get which takes if a value is present return the value otherwise it Returns the result produced by a supplying function so we have to create an overriding function inside of an or else get so in or else get we start here we create an open function because we aren't pulling anything in because if it's null we don't have a value and then we can set the value this way so you can use or else or you can do in or else and do some type of calculation or function to return your default value right so here we should get pepperoni so we assert that and we should get pepperoni so okay run this again we pass excellent all right now let's look at or else bro sometimes our calculations May throw an exception so we know we're going to get an exception so we have that nullable option that we had we can assert that an exception of type in this case we're going to use the legal argument exception so we can use the or else throw to throw a specific type of exception in this case we're going to throw an illegal argument exception we're going to use the is thrown by which takes a function all right so this is all search jstuff now we're going to take our optional of nullable and we could just pass an all but I'm just going to do no topping so we're going to pass in a null string so no toppings or else throw and we can pass in in a legal argument exception new so here if we do optional of nullable with a nullable object we can use or else throw illegal argument exception so if we get a null for this exception run this again and we pass all right now this was the old way of doing things we had to actually handle the exceptions if we wanted to with Java 10 they introduced a simplified or else throw method so they basically overrode the method so we can start again with optional dot of noble again we'll take in our null optional however in this case all I have to do is or else thrown this will return us a no such element exception much more simplified so now all I have to do is take all this take our assertion logic and then wrap it so we want to assert that exception of type no such element exception is thrown by optional of nullable type or else throw so if we get null we will throw no such element exception and we passed all right so let's review this one real quick so for example three we want to get an object from an optional so we create an optional and then we want to get the object value out of the option so to do that we use the get method if we want to check to make sure that the value is null before we try to get it we can use the if present so we no longer have to do the object not equal to null we can just do optional if present then get the value and use it within a function we can use if present or else to override the value basically setting a default value using IF present or else we can use the or else to override nulls so if we have an optional of nullable we can use or else to return the default value we can use or else get to override nulls using a functional interface so we can use or else get and we can supplement a empty function call and then do some documentation afterwards to do something for the return of our optional of our object again useful for setting default values we also looked at or else throws which was the original or else throw to handle exceptions thrown by our optionals but Java 10 gave us a simpler method where instead of us having to actually create a functional interface we just need to use or else throws to now return no such element exception [Music] thank you
Transcript Segments
foreign
[Music]
[Music]
next let's look at example three here we
are going to look at how to get an
object out of an optional and then we're
going to look at the conditional methods
that come with the optional class like
if present if present or else or else or
else get or else throws
all right so example three
we're going to start by getting the
object from an optional
so we again will take that string
from our previous example
so we have our string onion and we have
our optional string populated string
optional from onion
now how do we get the string back out of
our optional variable here
to do that we can do an assertion so we
can assert that
our populated string
dot get
so the dot get method here
returns us the value the object value
if it is present so if we have a object
in our optional that is present we can
use get to return that object so get
will return us on you
so we have get so let's verify that so
if we're getting back this onion object
we should be able to do is equal to
is equal to and it should be the same as
onion
all right so we created our string
object onion we converted string to an
optional called populated string
optional
and we're using the get method to get
our string back out of our optional and
then make sure that is equal to the
string that we converted it to so if I
save this
and run everything passes
okay now let's look at the next option
all right so two let's look at null
checks versus optionals if present
so in the old days or the old way of
doing this
in order for us to check to see if onion
is null we need to start with the
traditional if
onion not equal to no then do something
and in this case let's just print out
the length of onion
and onion dot blank
all right so if I run this I should get
five
and I get 5. all right so we confirmed
that onion is not null
however if we want to use optionals we
no longer have to do this and I'll check
we can actually use the optional object
to simplify this logic
so here we can start with our populated
string
so we've this is the optional string we
created and converted from string onion
so we start out with the populated
string optional
we start with is present
if it's present it'll return true that
means we can do something with it
and then I can use lambdas to actually
pull out the value so all right so if
it's present I can then get the object
inside of option
so I can do
topping
so this will take the optional value so
it will return me this string as topping
and then we can do the same logic here
but in this case we want topping
oh that is person if present sorry
so if I want to do the same logic
without having to do this if check all I
have to do for readability is
take my optional
if present pull out the optional value
as topping and then system out print
line to topping length
close much easier easier to read and
avoids all these if statements all over
the place in our code so if I run this
they should be the same
and I get five and five
so this is how you do a null check with
optional using IF present
all right so three
let's look at the Java 9 introduction of
If present or else so this now allows us
to do an if else within the optional
chaining with using our method chain
so here we're going to start out with
our populated string again
copy this
all right so we have our populated
string
if present write out the length
all right well that doesn't really tell
me much so let me change this just so we
can for clarity so
popping
chosen
Plus
and now
we can actually get the value so I will
just name this value
so I'm getting the value of my string
and we're writing out the value
however
if I you change this from if present to
if present or else
so if present or else takes two options
or two parameters so we get a runnable
empty action or we have an action and
these allow us to create internal
functions to override these values so
the first one we did was the value of or
value
and then the second one
comma
is going to be
an empty
because we don't have a value
and no toppings
so now if I use if present or else if I
have a value do something otherwise do
something else so I can actually write
that logic within the method chain
so now if I run this
I should get toppings chosen I do
next let's look at use or else to
override nulls
so let's create a no toppings
string of null
and if we do an or else we can create a
string topping
equals
foreign optional
Dot of noble
no toppings
Dot or else
cheese
now here we're going to pass in a null
optional
however if it is null we want to return
cheese so we're essentially sending a
default value here so to verify that we
can do assert that
topping
dot is equal to
cheese
so let's run this
and we pass
so if we have no toppings if our
object that we're converting to
optional is null we're going to set the
value to cheese
all right now let's look at use or else
get
so here we're going to take topping
again
so we'll just take our string topping
and we're going to
take an optional
of nullable
and we're going to pass in no toppings
so we're going to do no toppings again
now this time we're going to do or else
but we're going to use the functional
interface
so we're going to use or else get
so if it's nullable we can do or else
which sets the value or we can do or
else get which takes if a value is
present return the value otherwise it
Returns the result produced by a
supplying function so we have to create
an overriding function inside of an or
else get
so in or else get we start here
we create
an open function
because we aren't pulling anything in
because if it's null we don't have a
value
and then we can set the value this way
so you can use or else or you can do in
or else and do some type of calculation
or function to return your default value
right so here we should get pepperoni
so we assert that
and we should get pepperoni
so
okay run this again
we pass
excellent all right now let's look at or
else bro sometimes our calculations May
throw an exception
so we know we're going to get an
exception so we have that nullable
option that we had
we can assert
that an exception
of type
in this case we're going to use the
legal
argument exception
so we can use the or else throw to throw
a specific type of exception
in this case we're going to throw an
illegal argument exception we're going
to
use the is thrown by
which takes
a function
all right so this is all search jstuff
now we're going to take our optional
of nullable
and we could just pass an all but I'm
just going to do no topping so we're
going to pass in a null string
so no toppings
or else throw
and we can pass in
in a legal argument exception
new
so here if we do optional of nullable
with a nullable
object we can use or else throw illegal
argument exception
so if we get a null for this exception
run this again
and we pass
all right now
this was the old way of doing things we
had to actually handle the exceptions if
we wanted to
with Java 10 they introduced a
simplified or else throw method so they
basically overrode the method so we can
start again
with optional
dot of noble
again we'll take in our null
optional
however in this case all I have to do is
or else thrown
this will return us a no such element
exception
much more simplified
so now all I have to do is take all this
take our assertion logic
and then wrap it
so we want to assert that exception of
type no such
element exception
is thrown by optional of nullable type
or else throw
so if we get null we will throw no such
element exception
and we passed
all right so let's review this one real
quick
so for example three we want to get an
object from an optional so we create an
optional and then we want to get the
object value out of the option so to do
that we use the get method
if we want to check to make sure that
the value is null before we try to get
it we can use the if present so we no
longer have to do the object not equal
to null we can just do optional if
present then get the value and use it
within a function
we can use if present or else to
override the value basically setting a
default value
using IF present or else
we can use the or else to override nulls
so if we have an optional of nullable we
can use or else to return the default
value
we can use or else get
to override nulls using a functional
interface so we can use or else get
and we can supplement a empty function
call and then do some documentation
afterwards to do something for the
return of our optional
of our object
again useful for setting default values
we also looked at or else throws
which was the original or else throw to
handle exceptions thrown by our
optionals
but Java 10 gave us a simpler method
where instead of us having to actually
create a functional interface we just
need to use or else throws to now return
no such element exception
[Music]
thank you