- Guava 教程
- Guava - 首頁
- Guava - 概述
- Guava - 環境搭建
- Guava - Optional 類
- Guava - Preconditions 類
- Guava - Ordering 類
- Guava - Objects 類
- Guava - Range 類
- Guava - Throwables 類
- Guava - 集合工具類
- Guava - 快取工具類
- Guava - 字串工具類
- Guava - 基本型別工具類
- Guava - 數學工具類
- Guava 有用資源
- Guava - 快速指南
- Guava - 有用資源
- Guava - 討論
Guava - Optional 類
Optional 是一個不可變物件,用於包含一個非空物件。Optional 物件用於表示空值,即不存在的值。此類包含各種實用方法,以方便程式碼處理可用或不可用的值,而不是檢查空值。
類宣告
以下是 com.google.common.base.Optional<T> 類的宣告:
@GwtCompatible(serializable = true)
public abstract class Optional<T>
extends Object
implements Serializable
類方法
| 序號 | 方法及描述 |
|---|---|
| 1 | static <T> Optional<T> absent() 返回一個不包含引用的 Optional 例項。 |
| 2 | abstract Set<T> asSet() 如果存在包含的例項,則返回一個其唯一元素為包含例項的不可變單例 Set;否則返回一個空的不可變 Set。 |
| 3 | abstract boolean equals(Object object) 如果 object 是一個 Optional 例項,並且包含的引用彼此相等或兩者都不存在,則返回 true。 |
| 4 | static <T> Optional<T> fromNullable(T nullableReference) 如果 nullableReference 不為空,則返回包含該引用的 Optional 例項;否則返回 absent()。 |
| 5 | abstract T get() 返回包含的例項,該例項必須存在。 |
| 6 | abstract int hashCode() 返回此例項的雜湊碼。 |
| 7 | abstract boolean isPresent() 如果此持有者包含一個(非空)例項,則返回 true。 |
| 8 | static <T> Optional<T> of(T reference) 返回包含給定非空引用的 Optional 例項。 |
| 9 | abstract Optional<T> or(Optional<? extends T> secondChoice) 如果此 Optional 有值存在,則返回此 Optional;否則返回 secondChoice。 |
| 10 | abstract T or(Supplier<? extends T> supplier) 如果包含的例項存在,則返回該例項;否則返回 supplier.get()。 |
| 11 | abstract T or(T defaultValue) 如果包含的例項存在,則返回該例項;否則返回 defaultValue。 |
| 12 | abstract T orNull() 如果包含的例項存在,則返回該例項;否則返回 null。 |
| 13 | static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>> optionals) 按順序返回提供的 optionals 中每個存在例項的值,跳過 absent() 的出現。 |
| 14 | abstract String toString() 返回此例項的字串表示形式。 |
| 15 | abstract <V> Optional<V> transform(Function<? super T,V> function) 如果例項存在,則使用給定的 Function 對其進行轉換;否則,返回 absent()。 |
繼承的方法
此類繼承自以下類:
- java.lang.Object
Optional 類的示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在 C:/> Guava. 中。
GuavaTester.java
import com.google.common.base.Optional;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester guavaTester = new GuavaTester();
Integer value1 = null;
Integer value2 = new Integer(10);
//Optional.fromNullable - allows passed parameter to be null.
Optional<Integer> a = Optional.fromNullable(value1);
//Optional.of - throws NullPointerException if passed parameter is null
Optional<Integer> b = Optional.of(value2);
System.out.println(guavaTester.sum(a,b));
}
public Integer sum(Optional<Integer> a, Optional<Integer> b) {
//Optional.isPresent - checks the value is present or not
System.out.println("First parameter is present: " + a.isPresent());
System.out.println("Second parameter is present: " + b.isPresent());
//Optional.or - returns the value if present otherwise returns
//the default value passed.
Integer value1 = a.or(new Integer(0));
//Optional.get - gets the value, value should be present
Integer value2 = b.get();
return value1 + value2;
}
}
驗證結果
使用 javac 編譯器編譯該類,如下所示:
C:\Guava>javac GuavaTester.java
現在執行 GuavaTester 以檢視結果。
C:\Guava>java GuavaTester
檢視結果。
First parameter is present: false Second parameter is present: true 10