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] so next let's look at our final example example four here we're going to look at using the stream method with mapping also look at using filters and creating multiple filters on a string and also looking at how to use the flat map method all right so for example four we're going to jump out and we're going to use the stream method and map to convert a list of strings to a list of optional strings we're then going to use the filter method to remove nulls we're then going to look at using a multiple filter to get specific values and then also look at using a flat map now in this example I do have two additional methods I have a method that will return an arraylist with nulls containing different toppings that can appear on a pizza and then I have another method which we will use to convert to filter out the values from get toppings to only return us the veggie options from get toppings all right so for this one we need a list of optionals so we're going to start with our list here so we have a list of optional strings so we have a list of options so you can create a optional uh you don't do optional lists it's list of optionals so everything in lists so here we want to get all those toppings and the git Toppings is going to return a list of strings so we have a list of optional strings so we're going to get the toppings we're going to stream the toppings which returns a sequential stream with the collection as its source and then from the stream we need to map our incoming list values to our optional values so here we can do dot map and map returns a stream consisting of the results of applying the given function to the elements of this string so we can map now our get Toppings is a list of strings but what we really want is an optional string not a string so we need to map our string to an optional string to do that we do map optional of nullable and we have to do of nullable because our list does contain nulls and then the last thing we need to do is we need to convert this mapping back into a collection of a list so we're going to essentially take out the strings and get toppings convert them into optional strings and then put them back into a new list so we'll do collect collect performs a mutable reduction operation taking the list and converting them back into a collection so we have collection so collect selection dot to list oh collectors sorry that collection the collectors dot to list okay so we use our stream and map to convert the list of strings to a list of optional strings so we instantiate our new object or a list of optional strings we get our list of strings we then stream them and as we're streaming them we are going to convert them from a string to an optional string and then we're going to collect and return a collector's to list so we're going to convert these back to a list and if we did everything correctly we should be able to write this list out so system.out print line everything in list we should see a optional string list with these values so let's run this and we have the list so now we have optional cheese optional empty optional pepperoni optional empty so we have our list of optional strengths very cool now we have a list but now we don't want the null value so let's get rid of those notes so how do we remove the nulls well we can do it a couple ways let's start with using filters so let's create a list of string and we just want all toppings no notes so all toppings equals so here we'll start with everything in list since it's already a list of optionals let's stream it and then let's filter so filter returns a stream consistent of the element of the stream that matches the given predicate so if you look at filter you can think of filters like a where clause in a SQL statement so our filter here is going to be some condition that strips out either condition that's true or false for when we want to pull the value in so to begin with let's get rid of those notes so let's do optional so let's only take optionals that are present so this filter will only return or will only accept non-null optionals in the list so then we take that and then we map what we get back to an optional get so we're going to get back that string so we want to get rid of optional now and return the actual string because these are non-nulls so optional get and then lastly we need to convert them back into a list so collect collector dot to list so here we want to filter out the nulls and just return a list of non-nullable strings as all toppings so let's print this out so now we should have a second list here with no notes and we do so we have cheese pepperoni mushrooms and olives all right now let's filter out some more so let's take this same filter and apply more logic to it so let's start with a new list and this time we'll just get veggie toppings so let's start with everything in the list including those nulls let's stream again so that we can go through the values all right let's filter again on the is present and let's also map to get the string values so if present get the map but here's the cool part you can now apply a second filter to the actual map so here we can do filter again which is cool and filter can then take the value so now filter can take the topping so it's going to take the string value from our get and we can now pass it in a function so we can create a function here and in our function we will call our get veggie toppings so this will return a list of strings that just contain veggies and we can use the dot contains hopping so if our topping is in the veggies return it so this will return true all right then we need to do our collect again and convert this back to our list so it reads like this we have a list of strings veggie toppings we start with everything in list with nulls we stream through the list we go through the list we filter out all nulls we map to get our string so we get our object out and then we filter on that list to get only the toppings that are veggie types and then we return the list so if we did this correctly we should get just our veggie toppings Ron as and we now have mushrooms and olives excellent all right so for our last example here we're just going to look at using the flat map so here we were using the map now we're going to use flat map so I'll simplify this a little bit we'll start with this so we have a list of strings we have all toppings which we don't need as a list again because that will throw in there all right so we have all toppings we start with everything in the list and we start with stream now instead of mapping we can do a flat map and flat map returns a string consisting of the results of replacing each element of the stream with the contents of the map screen value now since we're just doing with a single string this will be very easy but if you're doing with an object you would have to actually map out each of the values of the object so here we will take the topping topping topping Dot oh flat map popping Dot is present so if it's present get the topping adults stream empty and then we again need to convert our collection to a list okay so all topics everything in list stream flat map topping if topping is present get the topping otherwise the stream is empty stream of topping dot get or stream dot empty collect oh not steam stream all right so what we're doing here so all toppings everything in lists we're taking a stream we're flat mapping so we're taking from the Stream this time we're not pre-mapping so we're not mapping an optional we're taking the Stream so if our topping is presents or optional we then take our optional from the stream of so we're taking our option of top and get and return that otherwise we're going to return string empty so now if I print out all toppings we get R null uh all the non-null toppings cheese pepperoni mushrooms and olives but if you look at this here this is a lot more worth to do when just a simple map will do the same thing this is a lot cleaner a lot easier to follow than doing it this way but you can break out the flat map if you want [Music] [Music]
Transcript Segments
foreign
[Music]
[Music]
so next let's look at our final example
example four
here we're going to look at using the
stream method with mapping
also look at using filters and creating
multiple filters on a string
and also looking at how to use the flat
map method
all right so for example four we're
going to jump out and we're going to use
the stream method and map to convert a
list of strings to a list of optional
strings we're then going to use the
filter method to remove nulls we're then
going to look at using a multiple filter
to get specific values and then also
look at using a flat map
now in this example I do have two
additional methods I have a method that
will return an arraylist with nulls
containing different toppings that can
appear on a pizza
and then I have another method which we
will use to convert to filter out the
values from get toppings to only return
us the veggie options from get toppings
all right so for this one we need a list
of optionals so we're going to start
with our list here
so we have a list of optional strings
so we have a list of options so you can
create a optional
uh you don't do optional lists it's list
of optionals so everything in lists so
here we want to get all those toppings
and the git Toppings is going to return
a list of strings
so we have a list of optional strings so
we're going to get the toppings we're
going to stream
the toppings which returns a sequential
stream with the collection as its source
and then from the stream we need to map
our incoming list values to our optional
values so here we can do dot map
and map
returns a stream
consisting of the results of applying
the given function to the elements of
this string
so we can map now
our get Toppings is a list of strings
but what we really want is
an optional string not a string so we
need to map our string to an optional
string to do that we do map
optional
of nullable
and we have to do of nullable because
our list does contain nulls
and then the last thing we need to do is
we need to convert this mapping back
into a collection of a list so we're
going to essentially take out the
strings and get toppings convert them
into optional strings and then put them
back into a new list
so we'll do collect
collect performs a mutable reduction
operation taking the list and converting
them back into a collection
so we have collection
so collect
selection
dot to
list
oh collectors sorry
that collection
the collectors
dot to list okay
so we use our stream and map to convert
the list of strings to a list of
optional strings
so we instantiate our new object or a
list of optional strings we get our list
of strings we then stream them and as
we're streaming them we are going to
convert them from a string
to an optional string
and then we're going to collect and
return a collector's to list so we're
going to convert these back to a list
and if we did everything correctly we
should be able to write this list out
so system.out print line everything in
list we should see a optional string
list with these values
so let's run this
and we have the list so now we have
optional cheese optional empty optional
pepperoni optional empty so we have our
list of optional strengths very cool
now we have a list but now we don't want
the null value so let's get rid of those
notes so how do we remove the nulls well
we can do it a couple ways let's start
with using filters
so let's create a list
of string
and we just want all toppings
no notes so all toppings
equals so here we'll start with
everything in list since it's already a
list of optionals
let's stream it
and then let's filter
so filter returns a stream consistent of
the element of the stream that matches
the given predicate so if you look at
filter you can think of filters like a
where clause in a SQL statement so our
filter here is going to be some
condition
that strips out either condition that's
true or false for when we want to pull
the value in
so to begin with let's get rid of those
notes so let's do optional
so let's only take optionals
that are present
so this filter will only return or will
only accept
non-null optionals in the list
so then we take that and then we map
what we get back
to an optional
get so we're going to get back that
string so we want to get rid of optional
now and return the actual string because
these are non-nulls so optional get
and then lastly we need to
convert them back into a list so collect
collector
dot to list
so here we want to filter out the nulls
and just return a list of non-nullable
strings
as all toppings so let's print this out
so now we should
have a second list here with no notes
and we do so we have cheese pepperoni
mushrooms and olives
all right now let's
filter out some more so let's take this
same filter and apply more logic to it
so
let's start with a new list
and this time we'll just get veggie
toppings
so let's start with everything in the
list including those nulls
let's stream again
so that we can go through the values
all right let's filter again
on the is present
and let's also map to get the string
values so if present get the map
but here's the cool part you can now
apply a second filter to the actual map
so here we can do filter again
which is cool and filter can then take
the value so now filter can take the
topping
so it's going to take the string value
from our get
and we can now pass it in a function
so we can create a function here and in
our function we will call our get veggie
toppings so this will return a list of
strings that just contain veggies
and we can use the dot contains
hopping
so if
our topping is in the veggies return it
so this will return true
all right then
we need to do our collect again and
convert this back to our list
so it reads like this we have a list of
strings veggie toppings
we start with everything in list with
nulls we stream through the list we go
through the list we filter out all nulls
we map to get our string so we get our
object out and then we filter on that
list to get only the toppings that are
veggie types and then we return the list
so if we did this correctly we should
get just our veggie toppings
Ron as
and we now have mushrooms and olives
excellent all right so for our last
example here we're just going to look at
using the flat map so here we were using
the map now we're going to use flat map
so I'll simplify this a little bit we'll
start with this
so we have a list of strings we have all
toppings which we don't need as a list
again because that will throw in there
all right so we have all toppings we
start with everything in the list and we
start with stream now instead of mapping
we can do a flat map
and flat map returns a string consisting
of the results of replacing each element
of the stream with the contents of the
map screen value now since we're just
doing with a single string
this will be very easy but if you're
doing with an object
you would have to actually map out each
of the values of the object so here we
will take the topping
topping
topping Dot
oh flat
map
popping
Dot
is present
so if it's present get the topping
adults
stream
empty
and then we again need to convert our
collection to a list
okay so all topics everything in list
stream flat map topping
if topping is present get the topping
otherwise the stream is empty
stream
of
topping dot get
or stream dot empty collect
oh
not steam stream
all right so what we're doing here so
all toppings everything in lists we're
taking a stream
we're flat mapping so we're taking from
the Stream
this time we're not pre-mapping so we're
not mapping an optional we're taking the
Stream
so if our topping is presents or
optional we then take our optional from
the stream
of so we're taking our option of top and
get
and return that otherwise we're going to
return string empty
so now if I print out all toppings
we get R null
uh all the non-null toppings cheese
pepperoni mushrooms and olives
but if you look at this here this is a
lot more worth to do when just a simple
map will do the same thing
this is a lot cleaner a lot easier to
follow than doing it this way but you
can break out the flat map if you want
[Music]
[Music]