Optional usages, defaults and checks

jvm-lukas.com 1 rok temu

We have three ways to create an instance of Optional

var opt = Optional.of("notNull")

not null otherwise NullPointerException

var opt = Optional.ofNullable(mightBeNull)

you don’t know either it will contain null or not.

var opt = Optional.empty()

no value, similar concept to null, but without NullPointerException

Defaults with Optional

For the below case we would like to search our database for the customer if we don’t find it we would like to search our legacy database/service if we failed here as well let’s return default customer, throw...

Idź do oryginalnego materiału