java小技巧(三)--Optional

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

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

2.Optional.empty()
所有null包装成的Optional对象:

1 Optional<Integer> optional1 = Optional.ofNullable(null);
2 Optional<Integer> optional2 = Optional.ofNullable(null);
3 System.out.println(optional1 == optional2);// true
4 System.out.println(optional1 == Optional.<Integer>empty());// true
5  
6 Object o1 = Optional.<Integer>empty();
7 Object o2 = Optional.<String>empty();
8 System.out.println(o1 == o2);// true

3.isPresent()
判断值是否存在

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

4.ifPresent(Consumer consumer)
如果option对象保存的值不是null,则调用consumer对象,否则不调用

 1 Optional<Integer> optional1 = Optional.ofNullable(1);
 2 Optional<Integer> optional2 = Optional.ofNullable(null);
 3  
 4 // 如果不是null,调用Consumer
 5 optional1.ifPresent(new Consumer<Integer>() {
 6     @Override
 7     public void accept(Integer t) {
 8         System.out.println("value is " + t);
 9     }
10 });
11  
12 // null,不调用Consumer
13 optional2.ifPresent(new Consumer<Integer>() {
14     @Override
15     public void accept(Integer t) {
16         System.out.println("value is " + t);
17     }
18 });

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

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

6.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

7.orElseThrow()

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

 1 Optional<Integer> optional1 = Optional.ofNullable(1);
 2 Optional<Integer> optional2 = Optional.ofNullable(null);
 3  
 4 optional1.orElseThrow(()->{throw new IllegalStateException();});
 5  
 6 try
 7 {
 8     // 抛出异常
 9     optional2.orElseThrow(()->{throw new IllegalStateException();});
10 }
11 catch(IllegalStateException e )
12 {
13     e.printStackTrace();
14 }

8.filter(Predicate)
判断Optional对象中保存的值是否满足Predicate,并返回新的Optional。

 1 Optional<Integer> optional1 = Optional.ofNullable(1);
 2 Optional<Integer> optional2 = Optional.ofNullable(null);
 3  
 4 Optional<Integer> filter1 = optional1.filter((a) -> a == null);
 5 Optional<Integer> filter2 = optional1.filter((a) -> a == 1);
 6 Optional<Integer> filter3 = optional2.filter((a) -> a == null);
 7 System.out.println(filter1.isPresent());// false
 8 System.out.println(filter2.isPresent());// true
 9 System.out.println(filter2.get().intValue() == 1);// true
10 System.out.println(filter3.isPresent());// false

9.map(Function)
对Optional中保存的值进行函数运算,并返回新的Optional(可以是任何类型)

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

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

 1 Optional<Integer> optional1 = Optional.ofNullable(1);
 2  
 3 Optional<Optional<String>> str1Optional = optional1.map((a) -> {
 4     return Optional.<String>of("key" + a);
 5 });
 6  
 7 Optional<String> str2Optional = optional1.flatMap((a) -> {
 8     return Optional.<String>of("key" + a);
 9 });
10  
11 System.out.println(str1Optional.get().get());// key1
12 System.out.println(str2Optional.get());// key

 

posted on 2023-04-11 14:38  小夏coding  阅读(244)  评论(0编辑  收藏  举报

导航