Java8新特性之Optional類的使用

一 Optional 簡介:

javadoc:

A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.

Optional類的出現是爲了減少java中重複繁雜的判空操作,它借鑑google guava類庫的Optional類。


二 Optional 的三種構造方式:

Optional.of(obj)

它要求傳入的 obj 不能是 null 值的, 否則報NullPointerException 異常。

Optional.ofNullable(obj)

我們常用這種。

Optional.ofNullable(obj), 傳 null 進到就得到 Optional.empty(), 非 null 就調用 Optional.of(obj)。

Optional.empty()

Optional靜態方法.empty()創建的是一個空optional 容器對象 。

 

Optional.of(obj)的源碼:

  /**
     * Returns an {@code Optional} with the specified present non-null value.
     *
     * @param <T> the class of the value
     * @param value the value to be present, which must be non-null
     * @return an {@code Optional} with the value present
     * @throws NullPointerException if value is null
     */
    public static <T> Optional<T> of(T value) {
        return new Optional<>(value);
    }
 
 
  /**
     * Constructs an instance with the value present.
     *
     * @param value the non-null value to be present
     * @throws NullPointerException if value is null
     */
    private Optional(T value) {
        this.value = Objects.requireNonNull(value);
    }
 
  /**
     * Checks that the specified object reference is not {@code null}. This
     * method is designed primarily for doing parameter validation in methods
     * and constructors, as demonstrated below:
     * <blockquote><pre>
     * public Foo(Bar bar) {
     *     this.bar = Objects.requireNonNull(bar);
     * }
     * </pre></blockquote>
     *
     * @param obj the object reference to check for nullity
     * @param <T> the type of the reference
     * @return {@code obj} if not {@code null}
     * @throws NullPointerException if {@code obj} is {@code null}
     */
    public static <T> T requireNonNull(T obj) {
        if (obj == null)
            throw new NullPointerException();
        return obj;
    }

可以看到,使用工廠方法構造Optional類的時候,利用Objects.requireNonNull()方法來校驗空值,如果傳入參數爲null,會產生空指針異常,此時應該採用另一個工廠方法Optional.ofNullable()來進行構造。


三 初學者例子

首先定義一個簡單的Person類

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

簡單的使用:

        Person person = new Person("lucas", 26);
        Optional<Person> op = Optional.ofNullable(person);
        if(op.isPresent()){
            op.get().setName("lucas2");
        }

可以發現,這種寫法並不比null判空來的方便,甚至還不如:

if(person.getName()!=null){
            System.out.println(person.getName());
        }

所以,上述例子的 ifPresent()方法,不是Optional類的正確使用方式。


四 進階使用

真正體現Optional“有效避免空指針異常”是其ifPresent()、orElse()、orElseGet()以及orElseThrow()這幾個方法。如下:

業務場景(op代表Optional對象) 正確用法示例 錯誤用法示例
如果op中的對象不爲空,則進行操作 op.ifPresent(o -> o.setUserName("小明"));

if(op.isPresent()){

        op.get().setUserName("小明"));

}

如果op中的對象不爲空,則返回它;否則返回另一個值 op.orElse(initUser);

if(op.isPresent()){

       return op.get();

}

else{

        return initUser;

}

如果op中的對象不爲空,則返回它;否則進行操作 op.orElseGet(() -> new User(0, "小明")); if(op.isPresent()){

       return op.get();

}

else{

        return new User(0, "小明");

}

如果op中的對象不爲空,則返回它;否則拋出異常 op.orElseThrow(IllegalArgumentException::new); if(op.isPresent()){

       return op.get();

}

else{

        throw new IllegalArgumentException()

}

利用上面的一些Optional提供的靜態方法,可以捨棄isPresent()和get(),並充分結合java8的Lambda與Stream特性進行鏈式調用。


    public int getPersonNameLengthPlus10(Person person){
        return Optional.ofNullable(new Person("lucas", 26))
                .map(a -> a.getName())  //拿到person的name
                .map(a -> a.length())   //拿到person name的長度
                .map(a -> a+10)         //拿到person name長度後對長度加10
                .orElse(0);             //上面過程中有爲null執行這步操作
    }

就是JAVA8的魅力所在,Optional、Lambda、Stream的綜合應用,極大的簡化了代碼的書寫。

 

參考:

https://www.jianshu.com/p/c169ddd34903

https://juejin.im/entry/5ae6b732f265da0b7f44548f

https://blog.csdn.net/jui121314/article/details/82683249

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章