JDK8新特性之Optional

Optional是什么

java.util.Optional

Jdk8提供Optional,一个可以包含null值的容器对象,可以用来代替xx != null的判断。

Optional常用方法

of

1
2
3
public static <T> Optional<T> of(T value) {
   return new Optional<>(value);
}

为value创建一个Optional对象,如果value为空则 会报出NullPointerException异常。

ofNullable

1
2
3
public static <T> Optional<T> ofNullable(T value) {
   return value == null ? empty() : of(value);
}

为value创建一个Optional对象,但可以允许value为null值。

isPresent

1
2
3
public boolean isPresent() {
   return value != null;
}

判断当前value是否为null,如果不为null则返回true,否则false。

ifPresent

如果不为null值就执行函数式接口的内容。

1
2
3
4
public void ifPresent(Consumer<? super T> consumer) {
   if (value != null)
       consumer.accept(value);
}

get

1
2
3
4
5
6
public T get() {
   if (value == null) {
       throw new NoSuchElementException("No value present");
   }
   return value;
}

返回当前的值,如果为空则报异常。

orElse

返回当前值,如果为null则返回other。

1
2
3
public T orElse(T other) {
   return value != null ? value : other;
}

orElseGet

orElseGet和orElse类似,只是orElseGet支持函数式接口来生成other值。

1
2
3
public T orElseGet(Supplier<? extends T> other) {
   return value != null ? value : other.get();
}

orElseThrow

如果有值则返回,没有则用函数式接口抛出生成的异常。

1
2
3
4
5
6
7
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
   if (value != null) {
       return value;
   } else {
       throw exceptionSupplier.get();
   }
}

示例

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
31
32
33
34
35
36
37
public static void main(String[] args) {
testOf();
testNullable();
}


private static void testNullable() {
User user = null;
User john = new User("john", 18);
User dick = new User("dick", 12);

System.out.println(Optional.ofNullable(user).orElse(john));
System.out.println(Optional.ofNullable(john).get());
System.out.println(Optional.ofNullable(dick).orElse(john));
System.out.println(Optional.ofNullable(user).orElseGet(() -> john));

System.out.println();
}

private static void testOf() {
try {
User user1 = new User();
Optional<User> userOptional1 = Optional.of(user1);
if (userOptional1.isPresent()) {
System.out.println("user is not null");
}

User user2 = null;
Optional<User> userOptional2 = Optional.of(user2);//NullPointerException
if (userOptional2.isPresent()) {
System.out.println("user is not null");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println();
}

Optional在jdk8中有大量使用,比如像Stream流中,但Optional用在null判断感觉也没什么鸟用。。

在Spring4中也可以用Optional来代替autowired(require=false)的情况,参考历史Spring系列文章。


发布于 2020-04-01 21:08:53
分享
海报
165
上一篇:JDK8之新特性扩展篇下一篇:JDK8新特性之函数式接口
目录

    忘记密码?

    图形验证码