Java 8 Optional classes
Optional class is a container object can be a null. If the value exists isPresent () method returns true, call the get () method returns the object.
Optional is a container: it can hold values of type T, or simply save null. Optional offers many useful ways, so we do not carry out an explicit null value detection.
Optional introduction of class a good solution to a null pointer exception.
Class declaration
The following is a statementjava.util.Optional <T> class:
public final class Optional<T> extends Object
Class methods
No. | Method & description |
---|---|
1 | static <T> Optional <T> empty () Back empty Optional instance. |
2 | boolean equals (Object obj) Determining whether another object is equal Optional. |
3 | Optional <T> filter (Predicate < ? Super <T> predicate) If the value exists, and this value matches the given predicate, returns an Optional description for this value, otherwise it returns an empty Optional. |
4 | <U> Optional <U> flatMap (Function <? Super T, Optional <U >> mapper) If the value exists, the return value mapping method Optional contained herein is based, otherwise it returns an empty Optional |
5 | T get () If you include the value in this Optional, the return value, otherwise an exception is thrown: NoSuchElementException |
6 | int hashCode () Returns the hash code value exists, returns 0 if the value does not exist. |
7 | void ifPresent (Consumer <? super T > consumer) If the value is present value is used to call consumer, otherwise do nothing. |
8 | boolean isPresent () If the value exists method returns true, otherwise returns false. |
9 | <U> Optional <U> map (Function <? Super T ,? extends U> mapper) If the value of the mapping method, the presence provided, if it returns a non-null, returns a result Optional description. |
10 | static <T> Optional <T> of (T value) Returns a specified Optional non-null value. |
11 | static <T> Optional <T> ofNullable (T value) If non-null, returns the specified value described Optional, otherwise empty Optional. |
12 | T orElse (T other) If the value exists, the return value, otherwise other. |
13 | T orElseGet (Supplier <? Extends T > other) If the value exists, the return value, otherwise trigger other, and returns the result other call. |
14 | <X extends Throwable> T orElseThrow ( Supplier <? Extends X> exceptionSupplier) If the value is present, the return value contains otherwise thrown by the Supplier inherited abnormalities |
15 | String toString () Returns a non-empty string Optional, used to debug |
NOTE: These methods are inherited from class java.lang.Objectcome.
Optional examples
We can through the following examples to better understand the use of Optional classes:
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 - 允许传递为 null 参数 Optional<Integer> a = Optional.ofNullable(value1); // Optional.of - 如果传递的参数是 null,抛出异常 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 - 判断值是否存在 System.out.println("第一个参数值存在: " + a.isPresent()); System.out.println("第二个参数值存在: " + b.isPresent()); // Optional.orElse - 如果值存在,返回它,否则返回默认值 Integer value1 = a.orElse(new Integer(0)); //Optional.get - 获取值,值需要存在 Integer value2 = b.get(); return value1 + value2; } }
Implementation of the above script, output is:
$ javac Java8Tester.java $ java Java8Tester 第一个参数值存在: false 第二个参数值存在: true 10