Java 8 Optional 类

Java 8 Optional 类

Optional 类是一个可以为 null 的容器对象。如果值存在则 isPresent() 方法会返回 true,调用 get() 方法会返回该对象。
Optional 是个容器:它可以保存类型 T 的值,或者仅仅保存 null。Optional 提供很多有用的方法,这样我们就不用显式进行空值检测。
Optional 类的引入很好的解决空指针异常。

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;
    }
}

点击查看结果

``` 第一个参数值存在: false 第二个参数值存在: true 10 ```
import java.util.Optional;

/**
 * @author libin
 * @date 2018/12/10 13:12:33
 */
public class OptionalExample {
    public static void main(String[] args) {
        String[] str = new String[10];
        // Setting value for 5th index
        str[5] = "JAVA OPTIONAL CLASS EXAMPLE";
        // It returns an empty instance of Optional class
        Optional<String> empty = Optional.empty();
        System.out.println(empty);
        // It returns a non-empty Optional
        Optional<String> value = Optional.of(str[5]);
        // If value is present, it returns an Optional otherwise returns an empty Optional
        System.out.println("Filtered value: " + value.filter((s) -> s.equals("Abc")));
        System.out.println("Filtered value: " + value.filter((s) -> s.equals("JAVA OPTIONAL CLASS EXAMPLE")));
        // It returns value of an Optional. if value is not present, it throws an NoSuchElementException
        System.out.println("Getting value: " + value.get());
        // It returns hashCode of the value
        System.out.println("Getting hashCode: " + value.hashCode());
        // It returns true if value is present, otherwise false
        System.out.println("Is value present: " + value.isPresent());
        // It returns non-empty Optional if value is present, otherwise returns an empty Optional
        System.out.println("Nullable Optional: " + Optional.ofNullable(str[5]));
        // It returns value if available, otherwise returns specified value
        System.out.println("orElse: " + value.orElse("Value is not present"));
        System.out.println("orElse: " + empty.orElse("Value is not present"));
        // printing value by using method reference
        value.ifPresent(System.out::println);
    }
}

点击查看结果

``` Optional.empty Filtered value: Optional.empty Filtered value: Optional[JAVA OPTIONAL CLASS EXAMPLE] Getting value: JAVA OPTIONAL CLASS EXAMPLE Getting hashCode: -619947648 Is value present: true Nullable Optional: Optional[JAVA OPTIONAL CLASS EXAMPLE] orElse: JAVA OPTIONAL CLASS EXAMPLE orElse: Value is not present JAVA OPTIONAL CLASS EXAMPLE ```

参考链接

原文地址:https://www.cnblogs.com/hgnulb/p/10055041.html