# 功能
null 处理,避免 NPE
# 程序示例
package com.example.google.guava.demo.clazz; | |
import com.google.common.base.Optional; | |
/** | |
* <p> | |
* <code>OptionalTest</code> | |
* </p> | |
* Description: 输出结果 | |
* > First parameter is present: false | |
* > Second parameter is present: true | |
* > 10 | |
* | |
* @author Mcchu | |
* @date 2017/10/18 15:02 | |
*/ | |
public class OptionalTest { | |
public static void main(String[] args) { | |
OptionalTest test = new OptionalTest(); | |
Integer value1 = null; | |
Integer value2 = 10; | |
// 允许传 null | |
Optional<Integer> a1 = Optional.fromNullable(value1); // 输出:Optional.absent () | |
//Optional<Integer> a2 = Optional.fromNullable (value2); // 输出:Optional.of (10) | |
//java.util.Optional<T> 是 java8 新增 | |
//java.util.Optional<Integer> b1 = java.util.Optional.ofNullable (value1); // 输出:Optional.empty | |
//java.util.Optional<Integer> b2 = java.util.Optional.ofNullable (value2); // 输出:Optional [10] | |
// 传 null 抛 NPE | |
//Optional<Integer> c1 = Optional.of (value1); // 输出:java.lang.NullPointerException | |
Optional<Integer> c2 = Optional.of(value2); // 输出:Optional.of (10) | |
Integer result = sum(a1,c2); | |
System.out.println(result); // 输出:10 | |
} | |
private static Integer sum(Optional<Integer> a,Optional<Integer> b){ | |
// 判断 a、b 是否出现(英文翻译:present:出现;absent:未出现,缺席) | |
System.out.println("First parameter is present: " + a.isPresent()); // 输出:false | |
System.out.println("Second parameter is present: " + b.isPresent()); // 输出:true | |
// 如果值存在,则返回初始值,否则返回默认值,or () 方法中是默认值 | |
Integer d1 = a.or(0); // 输出:0 | |
//Integer d2 = b.or (0); // 输出:10 | |
//Integer e1 = a.get (); // 输出:java.lang.IllegalStateException: Optional.get () cannot be called on an absent value | |
Integer e2 = b.get(); // 输出:10 | |
return d1 + e2; | |
} | |
} |
# 输出结果
First parameter is present: false
Second parameter is present: true
10
参考:http://www.yiibai.com/guava/guava_optional_class.html