Java 8 Optional Class

Java 8 新特性Java 8 New Features


Optional class is a container object which can be null. If the value exists, the isPresent() method returns true, and calling the get() method returns the object.

Optional is a container: it can hold a value of type T, or just null. Optional provides many useful methods, so we don't have to explicitly perform null value checks.

The introduction of the Optional class effectively solves the null pointer exception.

Class declaration

The following is ajava.util.Optional<T>class declaration:

public final class Optional<T> extends Object

Class methods

Serial number Method & Description
1 static <T> Optional<T> empty()

Returns an empty Optional instance.

2 boolean equals(Object obj)

Determines whether another object is equal to the Optional.

3 Optional<T> filter(Predicate<? super <T> predicate)

If the value exists and matches the given predicate, returns an Optional describing this value; otherwise, returns an empty Optional.

4 <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)

If a value exists, 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 the value is not present.

7 void ifPresent(Consumer<? super T> consumer)

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

8 boolean isPresent()

The method returns true if the value is present, otherwise returns false.

9 <U>Optional<U> map(Function<? super T,? extends U> mapper)

If a value is present, applies the mapping function to it and gets the 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 <T> Optional<T> of(T value)

Returns an Optional with the specified non-null value.

11 static <T> Optional<T> ofNullable(T value)

If the value is 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<? extends T> other)

If the value is present, returns the value; otherwise, invokes other and returns the result of the other call.

14 <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)

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

15 String toString()

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

Note:These methods are inherited fromjava.lang.Objectthe class.


Optional Instance

We can better understand the usage of the Optional class through the following examples:

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 null parameters Optional<Integer> a = Optional.ofNullable(value1); //Optional.of - Throws NullPointerException if the passed parameter is null 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 the value exists System.out.println("First parameter value is present:" + a.isPresent()); System.out.println("Second parameter value is present:" + b.isPresent()); //Optional.orElse - If the 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 新特性Java 8 New Features

Other Extensions