Optional 类

到目前为止,臭名昭著的空指针异常是导致Java应用程序失败的最常见原因。 以前,为了解决空指针异常,Google公司著名的Guava项目引入了Optional类, Guava通过使用检查空值的方式来防止代码污染,它鼓励程序员写更干净的代 码。受到Google Guava的启发,Optional类已经成为Java 8类库的一部分。

Optional<T> 类(java.util.Optional) 是一个容器类,它可以保存类型T的值,代表这个值存在。或者仅仅保存null,表示这个值不存在。原来用 null 表示一个值不存在,现在 Optional 可以更好的表达这个概念。并且可以避免空指针异常。

Optional类的Javadoc描述如下:这是一个可以为null的容器对象。如果值存在 则isPresent()方法会返回true,调用get()方法会返回该对象

Optional提供很多有用的方法,这样我们就不用显式进行空值检测。

创建Optional类对象的方法

  • Optional.of(T t) : 创建一个 Optional 实例,t必须非空;
  • Optional.empty() : 创建一个空的 Optional 实例
  • Optional.ofNullable(T t):t可以为null

判断Optional容器中是否包含对象

  • boolean isPresent() : 判断是否包含对象

  • void ifPresent(Consumer<? super T> consumer) :如果有值,就执行Consumer接口的实现代码,并且该值会作为参数传给它。

获取Optional容器的对象

  • T get(): 如果调用对象包含值,返回该值,否则抛异常

  • T orElse(T other) :如果有值则将其返回,否则返回指定的other对象。

  • T orElseGet(Supplier<? extends T> other) :如果有值则将其返回,否则返回由Supplier接口实现提供的对象。

  • T orElseThrow(Supplier<? extends X> exceptionSupplier) :如果有值则将其返回,否则抛出由Supplier接口实现提供的异常。

测试

首先创建一个Girl类,和一个Boy类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Girl {

private String name;

public Girl(String name) {
this.name = name;
}

@Override
public String toString() {
return "Girl{" +
"name='" + name + '\'' +
'}';
}

public String getName() {
return name;
}

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

public Girl() {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Boy {

private Girl girl;


public Girl getGirl() {
return girl;
}

public void setGirl(Girl girl) {
this.girl = girl;
}

@Override
public String toString() {
return "Boy{" +
"girl=" + girl +
'}';
}

public Boy() {
}

public Boy(Girl girl) {
this.girl = girl;
}
}

image-20201224133941955

image-20201224134000993

image-20201224134021183

image-20201224134037310

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
public void test1() {
Girl girl = null;
Optional<Girl> optional = Optional.of(girl);
}

@Test
public void test2() {
Girl girl = null;
Optional<Girl> optional = Optional.empty();
}

@Test
public void test3() {
Girl girl = null;
Optional<Girl> optional = Optional.ofNullable(girl);
System.out.println(optional);
}

image-20201224134337379

image-20201224134400030

1
2
3
4
5
6
7
8
9
public String getGirlName(Boy boy) {
return boy.getGirl().getName();
}

@Test
public void test4() {
Boy boy = new Boy();//girl为null
System.out.println(getGirlName(boy));
}

传统优化,使用if判断是否为null。

image-20201224134532870

image-20201224134549063

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 优化
*x
* @param boy
* @return
*/
public String getGirlName1(Boy boy) {
if (boy != null) {
Girl girl = boy.getGirl();
if (girl != null) {
return boy.getGirl().getName();
}
}
return null;
}

@Test
public void test5() {
Boy boy = new Boy();//girl为null
System.out.println(getGirlName1(boy));
}

使用Optional进行优化。

image-20201224135015721

orElse相当于if-else。如果自己创建了不为null则使用自己的,没有或者为null则使用指定的。

ofElse

image-20201224135134066

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 使用Optional
*
* @param boy
* @return
*/
public String getGirlName2(Boy boy) {
Optional<Boy> optionalBoy = Optional.ofNullable(boy);
//如果boy为null则使用指定的other对象。
Boy boy1 = optionalBoy.orElse(new Boy(new Girl("刘亦菲")));

Girl girl = boy1.getGirl();
Optional<Girl> optionalGirl = Optional.ofNullable(girl);

//如果girl为null则使用指定的other对象。
Girl girl1 = optionalGirl.orElse(new Girl("隋东风"));
return girl1.getName();
}

@Test
public void test6() {
Boy boy = new Boy();//boy不为null ,girl为null
System.out.println(getGirlName2(boy));//隋东风

Boy boy2 = null;//boy不为null ,girl为null
System.out.println(getGirlName2(boy2));//刘亦菲

Boy boy3 = new Boy(new Girl("貂婵"));//boy不为null ,girl不为null
System.out.println(getGirlName2(boy3));//貂婵
}