📺 Develpreneur YouTube Episode

Video + transcript

Java Optional Example 4

2023-02-07 •Youtube

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
1.02

foreign

5.16

[Music]

16.62

[Music]

20.279

so next let's look at our final example

22.439

example four

24.3

here we're going to look at using the

26.46

stream method with mapping

28.8

also look at using filters and creating

32.04

multiple filters on a string

34.92

and also looking at how to use the flat

37.86

map method

39.54

all right so for example four we're

41.94

going to jump out and we're going to use

44.219

the stream method and map to convert a

47.399

list of strings to a list of optional

49.2

strings we're then going to use the

51.96

filter method to remove nulls we're then

54.66

going to look at using a multiple filter

56.219

to get specific values and then also

59.039

look at using a flat map

61.199

now in this example I do have two

63

additional methods I have a method that

65.339

will return an arraylist with nulls

67.5

containing different toppings that can

69.659

appear on a pizza

71.22

and then I have another method which we

73.5

will use to convert to filter out the

76.32

values from get toppings to only return

78.96

us the veggie options from get toppings

84.479

all right so for this one we need a list

87.78

of optionals so we're going to start

90.84

with our list here

95.159

so we have a list of optional strings

98.7

so we have a list of options so you can

101.159

create a optional

102.96

uh you don't do optional lists it's list

105.479

of optionals so everything in lists so

108.84

here we want to get all those toppings

113.22

and the git Toppings is going to return

114.96

a list of strings

118.74

so we have a list of optional strings so

121.86

we're going to get the toppings we're

123.96

going to stream

126.299

the toppings which returns a sequential

128.58

stream with the collection as its source

133.56

and then from the stream we need to map

136.7

our incoming list values to our optional

140.099

values so here we can do dot map

144.84

and map

146.58

returns a stream

149.52

consisting of the results of applying

152.22

the given function to the elements of

154.14

this string

155.22

so we can map now

157.92

our get Toppings is a list of strings

160.8

but what we really want is

163.8

an optional string not a string so we

167.519

need to map our string to an optional

169.56

string to do that we do map

174.54

optional

177.14

of nullable

180.48

and we have to do of nullable because

183

our list does contain nulls

186.66

and then the last thing we need to do is

188.519

we need to convert this mapping back

191.519

into a collection of a list so we're

194.04

going to essentially take out the

195.84

strings and get toppings convert them

198.12

into optional strings and then put them

200.459

back into a new list

202.62

so we'll do collect

204.959

collect performs a mutable reduction

207.5

operation taking the list and converting

210.599

them back into a collection

212.519

so we have collection

215.22

so collect

220.2

selection

221.879

dot to

223.62

list

226.319

oh collectors sorry

228.72

that collection

232.14

the collectors

234.599

dot to list okay

238.319

so we use our stream and map to convert

242.34

the list of strings to a list of

244.08

optional strings

245.64

so we instantiate our new object or a

248.94

list of optional strings we get our list

252.299

of strings we then stream them and as

256.019

we're streaming them we are going to

258.18

convert them from a string

262.139

to an optional string

265.08

and then we're going to collect and

267.06

return a collector's to list so we're

269.639

going to convert these back to a list

273.419

and if we did everything correctly we

275.699

should be able to write this list out

280.139

so system.out print line everything in

282.9

list we should see a optional string

286.139

list with these values

288.12

so let's run this

293.34

and we have the list so now we have

295.74

optional cheese optional empty optional

298.08

pepperoni optional empty so we have our

301.38

list of optional strengths very cool

306

now we have a list but now we don't want

308.699

the null value so let's get rid of those

310.62

notes so how do we remove the nulls well

313.74

we can do it a couple ways let's start

316.02

with using filters

318.18

so let's create a list

321.3

of string

323.699

and we just want all toppings

326.88

no notes so all toppings

329.88

equals so here we'll start with

332.34

everything in list since it's already a

334.56

list of optionals

338.46

let's stream it

342.24

and then let's filter

345.66

so filter returns a stream consistent of

349.44

the element of the stream that matches

351.36

the given predicate so if you look at

353.639

filter you can think of filters like a

355.56

where clause in a SQL statement so our

357.96

filter here is going to be some

359.88

condition

361.199

that strips out either condition that's

364.68

true or false for when we want to pull

367.199

the value in

369.12

so to begin with let's get rid of those

371.639

notes so let's do optional

373.979

so let's only take optionals

377.1

that are present

380.88

so this filter will only return or will

384.9

only accept

386.66

non-null optionals in the list

390.24

so then we take that and then we map

393.78

what we get back

397.919

to an optional

400.56

get so we're going to get back that

402.479

string so we want to get rid of optional

405.539

now and return the actual string because

407.52

these are non-nulls so optional get

412.5

and then lastly we need to

415.02

convert them back into a list so collect

419.819

collector

423.18

dot to list

427.259

so here we want to filter out the nulls

430.5

and just return a list of non-nullable

433.139

strings

434.819

as all toppings so let's print this out

440.699

so now we should

442.44

have a second list here with no notes

445.74

and we do so we have cheese pepperoni

448.199

mushrooms and olives

451.56

all right now let's

453.96

filter out some more so let's take this

456.3

same filter and apply more logic to it

459.84

so

461.34

let's start with a new list

465.78

and this time we'll just get veggie

467.819

toppings

471.3

so let's start with everything in the

472.919

list including those nulls

476.88

let's stream again

478.919

so that we can go through the values

482.16

all right let's filter again

485.52

on the is present

490.139

and let's also map to get the string

493.02

values so if present get the map

496.5

but here's the cool part you can now

499.08

apply a second filter to the actual map

502.5

so here we can do filter again

506.58

which is cool and filter can then take

509.639

the value so now filter can take the

511.86

topping

514.02

so it's going to take the string value

516.18

from our get

519.839

and we can now pass it in a function

523.08

so we can create a function here and in

526.26

our function we will call our get veggie

529.8

toppings so this will return a list of

533.58

strings that just contain veggies

536.7

and we can use the dot contains

540.779

hopping

542.76

so if

545.519

our topping is in the veggies return it

548.7

so this will return true

552.779

all right then

554.76

we need to do our collect again and

557.58

convert this back to our list

562.62

so it reads like this we have a list of

565.74

strings veggie toppings

568.2

we start with everything in list with

570

nulls we stream through the list we go

573.779

through the list we filter out all nulls

576.899

we map to get our string so we get our

580.2

object out and then we filter on that

582.66

list to get only the toppings that are

586.32

veggie types and then we return the list

590.279

so if we did this correctly we should

592.98

get just our veggie toppings

598.5

Ron as

601.98

and we now have mushrooms and olives

606.12

excellent all right so for our last

608.279

example here we're just going to look at

610.2

using the flat map so here we were using

613.019

the map now we're going to use flat map

616.08

so I'll simplify this a little bit we'll

619.019

start with this

622.56

so we have a list of strings we have all

626.279

toppings which we don't need as a list

628.38

again because that will throw in there

629.94

all right so we have all toppings we

632.279

start with everything in the list and we

634.5

start with stream now instead of mapping

637.92

we can do a flat map

641.16

and flat map returns a string consisting

643.92

of the results of replacing each element

646.68

of the stream with the contents of the

649.44

map screen value now since we're just

651.899

doing with a single string

655.26

this will be very easy but if you're

657.66

doing with an object

659.7

you would have to actually map out each

661.62

of the values of the object so here we

664.44

will take the topping

667.8

topping

669.899

topping Dot

673.62

oh flat

676.92

map

679.14

popping

680.519

Dot

683.16

is present

685.68

so if it's present get the topping

691.2

adults

694.079

stream

695.94

empty

698.76

and then we again need to convert our

701.88

collection to a list

704.88

okay so all topics everything in list

708.24

stream flat map topping

711.6

if topping is present get the topping

717.56

otherwise the stream is empty

722.88

stream

727.68

of

730.56

topping dot get

734.88

or stream dot empty collect

740.16

oh

741.54

not steam stream

745.32

all right so what we're doing here so

747.42

all toppings everything in lists we're

749.64

taking a stream

751.5

we're flat mapping so we're taking from

754.079

the Stream

755.399

this time we're not pre-mapping so we're

758.04

not mapping an optional we're taking the

760.2

Stream

761.22

so if our topping is presents or

764.339

optional we then take our optional from

766.98

the stream

768.079

of so we're taking our option of top and

772.86

get

774.779

and return that otherwise we're going to

777.66

return string empty

780.959

so now if I print out all toppings

785.339

we get R null

787.56

uh all the non-null toppings cheese

790.62

pepperoni mushrooms and olives

793.26

but if you look at this here this is a

796.32

lot more worth to do when just a simple

799.74

map will do the same thing

803.279

this is a lot cleaner a lot easier to

805.8

follow than doing it this way but you

809.639

can break out the flat map if you want

818.38

[Music]

828.98

[Music]