Java 9 Improved Optional Class
The Optional class was introduced in Java 8, and its introduction nicely solves the null pointer exception problem. In Java 9, three methods were added to improve its functionality:
- stream()
- ifPresentOrElse()
- or()
The stream() method
Syntax
public Stream stream()
The stream method converts an Optional into a Stream: if the Optional contains a value, it returns a Stream containing that value; otherwise it returns an empty Stream (Stream.empty()).
Example
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
import
java
.
util
.
Optional
;
import
java
.
util
.
stream
.
Collectors
;
import
java
.
util
.
stream
.
Stream
;
public
class
Tester
{
public
static
void
main
(
String
[
]
args
)
{
List
<
Optional
<
String
list
=
Arrays.
asList
(
Optional
.
empty
(
)
,
Optional
.
of
(
"
A
"
)
,
Optional
.
empty
(
)
,
Optional
.
of
(
"
B
"
)
)
;
//
filter the list based to print non-empty values
//
if optional is non-empty, get the value in stream, otherwise return empty
List
<
String
filteredList
=
list.
stream
(
)
.
flatMap
(
o
->
o
.
isPresent
(
)
?
Stream
.
of
(
o
.
get
(
)
)
:
Stream
.
empty
(
)
)
.
collect
(
Collectors
.
toList
(
)
)
;
//
Optional::stream method will return a stream of either one
//
or zero element if data is present or not.
List
<
String
filteredListJava9
=
list.
stream
(
)
.
flatMap
(
Optional
::
stream
)
.
collect
(
Collectors
.
toList
(
)
)
;
System
.
out
.
println
(
filteredList
)
;
System
.
out
.
println
(
filteredListJava9
)
;
}
}
The execution output is:
[A, B]
[A, B]
The ifPresentOrElse() method
Syntax
public void ifPresentOrElse(Consumer super T> action, Runnable emptyAction)
The improvement of the ifPresentOrElse method is that it has an else, accepting two parameters, Consumer and Runnable.
The purpose of the ifPresentOrElse method is that if an Optional contains a value, it calls the function action on the contained value, i.e. action.accept(value), which is the same as ifPresent; the difference from the ifPresent method is that ifPresentOrElse also has a second parameter emptyAction — if the Optional does not contain a value, then ifPresentOrElse calls emptyAction, i.e. emptyAction.run().
Example
import
java
.
util
.
Optional
;
public
class
Tester
{
public
static
void
main
(
String
[
]
args
)
{
Optional
<
Integer
optional
=
Optional.
of
(
1
)
;
optional
.
ifPresentOrElse
(
x
->
System
.
out
.
println
(
"
Value:
"
x
)
,
(
)
->
System
.
out
.
println
(
"
Not Present.
"
)
)
;
optional
=
Optional
.
empty
(
)
;
optional
.
ifPresentOrElse
(
x
->
System
.
out
.
println
(
"
Value:
"
x
)
,
(
)
->
System
.
out
.
println
(
"
Not Present.
"
)
)
;
}
}
The execution output is:
Value: 1
Not Present.
The or() method
Syntax
public Optional or(Supplier extends Optional extends T>> supplier)
If a value is present, returns the value specified by the Optional; otherwise returns a preset value.
Example
import
java
.
util
.
Optional
;
import
java
.
util
.
function
.
Supplier
;
public
class
Tester
{
public
static
void
main
(
String
[
]
args
)
{
Optional
<
String
optional1
=
Optional.
of
(
"
Mahesh
"
)
;
Supplier
<
Optional
<
String
supplierString
=
(
)
->
Optional.
of
(
"
Not Present
"
)
;
optional1
=
optional1
.
or
(
supplierString
)
;
optional1
.
ifPresent
(
x
->
System
.
out
.
println
(
"
Value:
"
x
)
)
;
optional1
=
Optional
.
empty
(
)
;
optional1
=
optional1
.
or
(
supplierString
)
;
optional1
.
ifPresent
(
x
->
System
.
out
.
println
(
"
Value:
"
x
)
)
;
}
}
The execution output is:
Value: Mahesh
Value: Not Present
Java 9 New Features