Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java 8 Optional Class

Java 8 Optional Class

Java 8 New Features Java 8 New Features


The Optional class is a container object that may hold a null value. If a value is present, the isPresent() method returns true, and calling the get() method returns that object.

Optional is a container: it can hold a value of type T, or just hold null. Optional provides many useful methods so that we do not need to perform explicit null checks.

The introduction of the Optional class solves null pointer exceptions well.

Class Declaration

The following is a java.util.Optional class declaration:

public

final

class

Optional

<

T

extends

Object

Class Methods

No. Method & Description
1 static Optional empty()

Returns an empty Optional instance.

2 boolean equals(Object obj)

Determines whether another object is equal to the Optional.

3 Optional filter(Predicate predicate)

If a value is present and the value matches the given predicate, returns an Optional describing the value; otherwise returns an empty Optional.

4 Optional flatMap(Function> mapper)

If a value is present, returns the value based on the mapping method contained in the Optional; otherwise returns an empty Optional

5 T get()

If the value is contained in this Optional, returns the value; otherwise throws an exception: NoSuchElementException

6 int hashCode()

Returns the hash code of the present value, or 0 if no value is present.

7 void ifPresent(Consumer consumer)

If a value is present, invokes consumer with the value; otherwise does nothing.

8 boolean isPresent()

Returns true if a value is present, otherwise returns false.

9 Optional map(Function mapper)

If a value is present, applies the mapping function to it to obtain a return value. If the return value is not null, creates an Optional containing the mapped return value as the return value of the map method; otherwise returns an empty Optional.

10 static Optional of(T value)

Returns an Optional with the specified non-null value.

11 static Optional ofNullable(T value)

If non-null, returns an Optional describing the specified value; otherwise returns an empty Optional.

12 T orElse(T other)

If the value is present, returns the value; otherwise returns other.

13 T orElseGet(Supplier other)

If the value is present, returns the value; otherwise triggers other and returns the result of calling other.

14 T orElseThrow(Supplier exceptionSupplier)

If the value is present, returns the contained value; otherwise throws an exception created by the Supplier

15 String toString()

Returns a non-empty string of the Optional, used for debugging

Note: These methods are inherited from the java.lang.Object class.


Optional Example

We can better understand the use of the Optional class through the following example:

Java8Tester.java file

import

java

.

util

.

Optional

;

public

class

Java8Tester

{

public

static

void

main

(

String

args

[

]

)

{

Java8Tester

java8Tester

=

new

Java8Tester

(

)

;

Integer

value1

=

null

;

Integer

value2

=

new

Integer

(

10

)

;

//

Optional.ofNullable - allows passing a null argument

Optional

<

Integer

a

=

Optional

.

ofNullable

(

value1

)

;

//

Optional.of - if the passed argument is null, throws NullPointerException

Optional

<

Integer

b

=

Optional

.

of

(

value2

)

;

System

.

out

.

println

(

java8Tester

.

sum

(

a

,

b

)

)

;

}

public

Integer

sum

(

Optional

<

Integer

a

,

Optional

<

Integer

b

)

{

//

Optional.isPresent - determines whether a value is present

System

.

out

.

println

(

"

Whether the first parameter value is present:

"

a

.

isPresent

(

)

)

;

System

.

out

.

println

(

"

Whether the second parameter value is present:

"

b

.

isPresent

(

)

)

;

//

Optional.orElse - if a value is present, returns it; otherwise returns the default value

Integer

value1

=

a

.

orElse

(

new

Integer

(

0

)

)

;

//

Optional.get - gets the value; the value must be present

Integer

value2

=

b

.

get

(

)

;

return

value1

value2

;

}

}

Executing the above script produces the following output:


$ javac Java8Tester.java 
$ java Java8Tester
第一个参数值存在: false
第二个参数值存在: true
10

Java 8 New Features Java 8 New Features