📺 Develpreneur YouTube Episode

Video + transcript

Java Optional Example 3

2023-02-02 •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]
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
1.02

foreign

5.16

[Music]

16.6

[Music]

20.16

next let's look at example three here we

24.18

are going to look at how to get an

26.1

object out of an optional and then we're

28.92

going to look at the conditional methods

30.66

that come with the optional class like

33.66

if present if present or else or else or

37.079

else get or else throws

41.64

all right so example three

44.94

we're going to start by getting the

47.04

object from an optional

49.379

so we again will take that string

53.16

from our previous example

57.42

so we have our string onion and we have

59.94

our optional string populated string

62.039

optional from onion

64.86

now how do we get the string back out of

68.4

our optional variable here

71.22

to do that we can do an assertion so we

74.76

can assert that

78.36

our populated string

83.64

dot get

86.939

so the dot get method here

89.7

returns us the value the object value

94.38

if it is present so if we have a object

98.28

in our optional that is present we can

101.1

use get to return that object so get

104.64

will return us on you

107.34

so we have get so let's verify that so

110.52

if we're getting back this onion object

113.22

we should be able to do is equal to

119.88

is equal to and it should be the same as

123.6

onion

126.24

all right so we created our string

128.52

object onion we converted string to an

133.14

optional called populated string

135.12

optional

136.26

and we're using the get method to get

138.599

our string back out of our optional and

141.66

then make sure that is equal to the

143.64

string that we converted it to so if I

146.58

save this

150.48

and run everything passes

154.44

okay now let's look at the next option

158.459

all right so two let's look at null

160.319

checks versus optionals if present

163.8

so in the old days or the old way of

167.22

doing this

168.48

in order for us to check to see if onion

171.12

is null we need to start with the

173.7

traditional if

175.5

onion not equal to no then do something

180.12

and in this case let's just print out

182.58

the length of onion

186.48

and onion dot blank

191.04

all right so if I run this I should get

193.739

five

196.92

and I get 5. all right so we confirmed

200.76

that onion is not null

203.519

however if we want to use optionals we

206.879

no longer have to do this and I'll check

209.28

we can actually use the optional object

212.459

to simplify this logic

215.159

so here we can start with our populated

217.8

string

219.72

so we've this is the optional string we

223.019

created and converted from string onion

225.48

so we start out with the populated

228.599

string optional

230.819

we start with is present

233.76

if it's present it'll return true that

235.92

means we can do something with it

238.799

and then I can use lambdas to actually

242.04

pull out the value so all right so if

244.98

it's present I can then get the object

248.76

inside of option

250.56

so I can do

253.86

topping

256.32

so this will take the optional value so

259.799

it will return me this string as topping

263.52

and then we can do the same logic here

270.36

but in this case we want topping

278.88

oh that is person if present sorry

284.1

so if I want to do the same logic

286.8

without having to do this if check all I

289.8

have to do for readability is

292.259

take my optional

294

if present pull out the optional value

297.06

as topping and then system out print

300.479

line to topping length

304.08

close much easier easier to read and

306.9

avoids all these if statements all over

309.18

the place in our code so if I run this

311.16

they should be the same

314.699

and I get five and five

318.54

so this is how you do a null check with

321.78

optional using IF present

326.639

all right so three

329.88

let's look at the Java 9 introduction of

332.759

If present or else so this now allows us

335.58

to do an if else within the optional

339.479

chaining with using our method chain

344.039

so here we're going to start out with

346.08

our populated string again

349.8

copy this

352.199

all right so we have our populated

353.94

string

355.259

if present write out the length

358.68

all right well that doesn't really tell

360.6

me much so let me change this just so we

363.479

can for clarity so

366.36

popping

368.52

chosen

372.9

Plus

374.16

and now

376.979

we can actually get the value so I will

379.44

just name this value

382.62

so I'm getting the value of my string

386.88

and we're writing out the value

389.16

however

390.6

if I you change this from if present to

394.139

if present or else

399.78

so if present or else takes two options

403.139

or two parameters so we get a runnable

407

empty action or we have an action and

410.699

these allow us to create internal

412.979

functions to override these values so

415.86

the first one we did was the value of or

418.919

value

420.12

and then the second one

423.12

comma

425.16

is going to be

427.8

an empty

429.479

because we don't have a value

438.24

and no toppings

452.4

so now if I use if present or else if I

455.94

have a value do something otherwise do

459.18

something else so I can actually write

461.28

that logic within the method chain

464.84

so now if I run this

468.06

I should get toppings chosen I do

473.88

next let's look at use or else to

476.759

override nulls

480.419

so let's create a no toppings

484.5

string of null

487.8

and if we do an or else we can create a

492.539

string topping

496.44

equals

498.06

foreign optional

503.699

Dot of noble

506.759

no toppings

511.139

Dot or else

516.12

cheese

520.68

now here we're going to pass in a null

525.24

optional

527.279

however if it is null we want to return

530.339

cheese so we're essentially sending a

532.62

default value here so to verify that we

535.68

can do assert that

538.44

topping

541.56

dot is equal to

548.519

cheese

554.04

so let's run this

557.519

and we pass

560.94

so if we have no toppings if our

564.6

object that we're converting to

567.36

optional is null we're going to set the

570.12

value to cheese

573.12

all right now let's look at use or else

576.3

get

578.82

so here we're going to take topping

581.459

again

585.6

so we'll just take our string topping

589.019

and we're going to

592.019

take an optional

595.8

of nullable

599.399

and we're going to pass in no toppings

602.16

so we're going to do no toppings again

606.3

now this time we're going to do or else

609.54

but we're going to use the functional

611.76

interface

614.1

so we're going to use or else get

616.8

so if it's nullable we can do or else

621.24

which sets the value or we can do or

624.899

else get which takes if a value is

628.62

present return the value otherwise it

631.32

Returns the result produced by a

634.2

supplying function so we have to create

636.12

an overriding function inside of an or

639.54

else get

640.92

so in or else get we start here

645.24

we create

647.279

an open function

649.2

because we aren't pulling anything in

651

because if it's null we don't have a

652.86

value

655.8

and then we can set the value this way

663.12

so you can use or else or you can do in

666.48

or else and do some type of calculation

670.079

or function to return your default value

675.3

right so here we should get pepperoni

677.94

so we assert that

684.12

and we should get pepperoni

686.399

so

689.22

okay run this again

695.339

we pass

698.94

excellent all right now let's look at or

701.64

else bro sometimes our calculations May

704.94

throw an exception

708

so we know we're going to get an

709.62

exception so we have that nullable

712.14

option that we had

714

we can assert

716.76

that an exception

720.3

of type

722.16

in this case we're going to use the

723.779

legal

726.12

argument exception

730.32

so we can use the or else throw to throw

733.98

a specific type of exception

736.92

in this case we're going to throw an

738.48

illegal argument exception we're going

740.88

to

741.779

use the is thrown by

745.68

which takes

747.48

a function

751.579

all right so this is all search jstuff

754.74

now we're going to take our optional

759.66

of nullable

762.42

and we could just pass an all but I'm

764.7

just going to do no topping so we're

766.2

going to pass in a null string

769.139

so no toppings

772.62

or else throw

776.279

and we can pass in

779.519

in a legal argument exception

782.639

new

787.079

so here if we do optional of nullable

792.3

with a nullable

794.459

object we can use or else throw illegal

798.66

argument exception

800.399

so if we get a null for this exception

805.139

run this again

810.12

and we pass

813.18

all right now

814.92

this was the old way of doing things we

817.139

had to actually handle the exceptions if

819.959

we wanted to

821.639

with Java 10 they introduced a

824.519

simplified or else throw method so they

827.7

basically overrode the method so we can

830.639

start again

833.459

with optional

835.579

dot of noble

838.98

again we'll take in our null

842.76

optional

844.62

however in this case all I have to do is

847.68

or else thrown

850.44

this will return us a no such element

852.899

exception

856.019

much more simplified

857.88

so now all I have to do is take all this

863.04

take our assertion logic

867.72

and then wrap it

871.2

so we want to assert that exception of

873.48

type no such

877.32

element exception

879.48

is thrown by optional of nullable type

883.56

or else throw

885.779

so if we get null we will throw no such

888.6

element exception

891.54

and we passed

893.519

all right so let's review this one real

895.32

quick

896.82

so for example three we want to get an

900.72

object from an optional so we create an

903.3

optional and then we want to get the

905.459

object value out of the option so to do

908.1

that we use the get method

912.42

if we want to check to make sure that

914.579

the value is null before we try to get

918

it we can use the if present so we no

920.82

longer have to do the object not equal

922.92

to null we can just do optional if

925.74

present then get the value and use it

930.54

within a function

932.519

we can use if present or else to

934.8

override the value basically setting a

936.959

default value

938.76

using IF present or else

941.82

we can use the or else to override nulls

946.62

so if we have an optional of nullable we

950.1

can use or else to return the default

952.68

value

955.199

we can use or else get

957.54

to override nulls using a functional

960.839

interface so we can use or else get

964.68

and we can supplement a empty function

968.22

call and then do some documentation

970.5

afterwards to do something for the

973.74

return of our optional

975.48

of our object

977.76

again useful for setting default values

981.3

we also looked at or else throws

984.54

which was the original or else throw to

987.54

handle exceptions thrown by our

990.06

optionals

991.44

but Java 10 gave us a simpler method

994.68

where instead of us having to actually

997.019

create a functional interface we just

999.839

need to use or else throws to now return

1002.779

no such element exception

1010.64

[Music]

1022.779

thank you