Java 8 Optional

young 395 2021-10-17

Optional最早出现在Guava工具包中,主要为了解决空指针问题,已存在Java8版本类库中

初始化


Optional.of()或者Optional.ofNullable():创建Optional对象,差别在于of不允许参数是null,而ofNullable则无限制


Optional.of()在对象为空时会抛出空指针异常

// 参数不能是null  
Optional<Integer> optional1 = Optional.of(1);  
  
// 参数可以是null  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
// 参数可以是非null  
Optional<Integer> optional3 = Optional.ofNullable(2);


Optional.empty()


所有null包装成的Optional对象

Optional<Integer> optional1 = Optional.ofNullable(null);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
System.out.println(optional1 == optional2);// true  
System.out.println(optional1 == Optional.<Integer>empty());// true  
  
Object o1 = Optional.<Integer>empty();  
Object o2 = Optional.<String>empty();  
System.out.println(o1 == o2);// true

isPresent()


判断值是否存在

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
// isPresent判断值是否存在  
System.out.println(optional1.isPresent() == true);  
System.out.println(optional2.isPresent() == false);

ifPresent(Consumer consumer)


如果option对象保存的值不是null,则调用consumer对象,否则不调用

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
// 如果不是null,调用Consumer  
optional1.ifPresent(new Consumer<Integer>() {  
    @Override  
    public void accept(Integer t) {  
        System.out.println("value is " + t);  
    }  
});  
  
// null,不调用Consumer  
optional2.ifPresent(new Consumer<Integer>() {  
    @Override  
    public void accept(Integer t) {  
        System.out.println("value is " + t);  
    }  
});

orElse(value)


如果optional对象保存的值不是null,则返回原来的值,否则返回value

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
// orElse  
System.out.println(optional1.orElse(1000) == 1);// true  
System.out.println(optional2.orElse(1000) == 1000);// true

orElseGet(Supplier supplier)


功能与orElse一样,只不过orElseGet参数是一个对象

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
System.out.println(optional1.orElseGet(() -> {  
    return 1000;  
}) == 1);//true  
  
System.out.println(optional2.orElseGet(() -> {  
    return 1000;  
}) == 1000);//true

orElseThrow()


值不存在则抛出异常,存在则什么不做,有点类似Guava的Precoditions

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
optional1.orElseThrow(()->{throw new IllegalStateException();});  
  
try {  
    optional2.orElseThrow(()->{throw new IllegalStateException();});  
} catch(IllegalStateException e ) {  
    e.printStackTrace();  
}

filter(Predicate)


判断Optional对象中保存的值是否满足Predicate,并返回新的Optional,类似于Stream中filter的作用

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
Optional<Integer> filter1 = optional1.filter((a) -> a == null);  
Optional<Integer> filter2 = optional1.filter((a) -> a == 1);  
Optional<Integer> filter3 = optional2.filter((a) -> a == null);  
System.out.println(filter1.isPresent());// false  
System.out.println(filter2.isPresent());// true  
System.out.println(filter2.get().intValue() == 1);// true  
System.out.println(filter3.isPresent());// false

map(Function)


对Optional中保存的值进行函数运算,并返回新的Optional,类似于Stream中map的作用

Optional<Integer> optional1 = Optional.ofNullable(1);  
Optional<Integer> optional2 = Optional.ofNullable(null);  
  
Optional<String> str1Optional = optional1.map((a) -> "key" + a);  
Optional<String> str2Optional = optional2.map((a) -> "key" + a);  
  
System.out.println(str1Optional.get());// key1  
System.out.println(str2Optional.isPresent());// false

flatMap()


功能与map()相似,flatMap方法与map方法类似,区别在于mapping函数的返回值不同。map方法的mapping函数返回值可以是任何类型T,而flatMap方法的mapping函数必须是Optional。

Optional<Integer> optional1 = Optional.ofNullable(1);  
  
Optional<Optional<String>> str1Optional = optional1.map((a) -> {  
    return Optional.<String>of("key" + a);  
});  
  
Optional<String> str2Optional = optional1.flatMap((a) -> {  
    return Optional.<String>of("key" + a);  
});  
  
System.out.println(str1Optional.get().get());// key1  
System.out.println(str2Optional.get());// key1