concept
Optional
Scene analysis
Demand : If we want to take a man the hearts of the goddess 's name .
If you do not use Optional to achieve
Man Man.java
public class Man {
private Goddess goddess;
public Goddess getGoddess() {
return goddess;
}
public void setGoddess(Goddess goddess) {
this.goddess = goddess;
}
}
Goddess Goddess.java
public class Goddess {
private String name;
public Goddess(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Test class Main.java
public class Main {
public static void main(String[] args) {
Man man = new Man();
System.out.println(getGoddessOfMan(man));
}
// 获取男人心中的女神
public static String getGoddessOfMan(Man man) {
return man.getGoddess().getName();
}
}
Abnormal:
If you look at the contents of the error, we can know that man.getGoddess().getName();
this statement null pointer exception occurred, but we need further positioning in order to know whether man is null, or goddess is null ?
We can rewrite the test class code to avoid this exception
// 获取男人心中的女神
public static String getGoddessOfMan(Man man) {
if (man == null || man.getGoddess() == null) {
return "";
}
return man.getGoddess().getName();
}
If you spend Optional to achieve
Rewrite Man.java
public class Man {
private Optional<Goddess> goddess = Optional.empty();
public Optional<Goddess> getGoddess() {
return goddess;
}
public void setGoddess(Optional<Goddess> goddess) {
this.goddess = goddess;
}
}
Goddess.java not rewrite
Rewrite the test class Main.java
public class Main {
public static void main(String[] args) {
Man man = new Man();
System.out.println(getGoddessOfMan(Optional.ofNullable(man)));
man = null;
System.out.println(getGoddessOfMan(Optional.ofNullable(man)));
}
// 获取男人心中的女神
public static String getGoddessOfMan(Optional<Man> optionalMan) {
return optionalMan.orElse(new Man()).getGoddess().orElse(new Goddess("蒙娜丽莎")).getName();
}
}
Console print out
Mona Lisa
Mona Lisa
In fact, it did not appear to save a few lines of code thing? So we then analyze Optional class down.
Optional analysis container class
1.Optional creation method
Optional the core and unique property is T value
.
In addition, since the Optional constructors have been privatized, it can only create Optional objects through static methods.
1.1 static creation methods of (T t) --- does not allow parameter is null
Test code below, we try to pass a parameter null
to the of(T t)
method, the results wereNullPointerException
// of方法会判断参数是否为null,如果为null,会报空指针异常
Optional<Goddess> op = Optional.of(null);
Our in-depth java.util.Optional
source code
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
再追踪到java.util.Objects
的源代码
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
1.2 静态创建方法empty() --- 创建一个value=null
的Optional容器对象
再看java.util.Optional
中empty()
,返回一个成员变量value为null
的Optional容器对象
private static final Optional<?> EMPTY = new Optional<>();
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
1.3 静态创建方法ofNullable(T t) --- 允许参数为null
仍然看java.util.Optional
的源代码,ofNullable
表示可以接受null
,并使用empty()
返回。也接受参数value不为null
,使用of(T t)
返回。
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
2.Optional的判断和获取 --- 先使用isPresent()
判断,再使用get()
获取
测试代码如下
import java.util.Optional;
public class OptionalTest {
public static void main(String[] args) {
Optional<Goddess> op = Optional.of(new Goddess("蒙娜丽莎"));
if (op.isPresent()) {
System.out.println(op.get().getName());
}
Optional<Goddess> empty = Optional.ofNullable(null);
// 先通过isPresent()判断,再使用get()来避免直接使用empty.get().getName()可能带来NoSuchElementException异常
// if (empty.isPresent()) {
System.out.println(empty.get().getName());
// }
}
}
控制台输出如下
蒙娜丽莎
Exception in thread "main" java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at optional.OptionalTest.main(OptionalTest.java:15)
从控制台输出我们可以知道,在使用get()
方法之前,最好先用isPresent()
判断Optional中的成员变量value
值是否存在。
3.把判断代码放在Optional类内的方法
3.2 orElse
方法 | 描述 |
---|---|
T orElse(T other) |
表示如果调用该方法的Optional对象的成员变量value 不为null 则返回value ,否则返回other |
T orElseGet(Supplier<T> supplier) |
表示如果调用该方法的Optional对象的成员变量value 不为null 则返回value ,否则用Supplier生成一个用于返回的T对象 |
T orElseThrow(Supplier<? extends X> exceptionSupplier) |
表示如果调用该方法的Optional对象的成员变量value 不为null 则返回value ,否则用Supplier生成一个用于抛出的异常对象 |
我们看可以查看一下java.util.Optional
源码
public T orElse(T other) {
return value != null ? value : other;
}
public T orElseGet(Supplier<? extends T> other) {
return value != null ? value : other.get();
}
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
return value;
} else {
throw exceptionSupplier.get();
}
}
3.3 剩下的成员方法
方法 | 如果参数为null |
如果成员变量value 为null |
如果成员变量value 不为null |
---|---|---|---|
Optional<T> filter(Predicate<? super T> predicate) |
抛出NullPointerException |
返回empty() |
如果predicate.test(T t) 为true ,返回当前对象,否则返回empty() |
Optional<U> map(Function<? super T, ? extends U> mapper) |
抛出NullPointerException |
返回empty() |
转换T为U,再返回Optional.ofNullable(U) |
Optional<U> flatMap(Function<? super T, Optional<U>> mapper) |
抛出NullPointerException |
返回empty() |
转换T为Optional<U> ,转换后的对象如果为null ,抛出NullPointerException |
测试代码
import java.util.Optional;
public class OptionalTest {
public static void main(String[] args) {
Optional<Goddess> op = Optional.of(new Goddess("蒙娜丽莎"));
Optional<Goddess> nullOp = Optional.ofNullable(null);
// 如果女神名称不为null,filter返回op,否则返回empty()
System.out.println(op.filter(goddess->goddess.getName() != null).isPresent());
System.out.println(nullOp.filter(goddess->goddess.getName() != null).isPresent());
// 映射返回女神名称
System.out.println(op.map(Goddess::getName).get());
System.out.println(nullOp.map(Goddess::getName).isPresent());
// 映射返回装有女神名称的Optional容器对象
System.out.println(op.flatMap(goddess->Optional.ofNullable(goddess.getName())).get());
System.out.println(nullOp.flatMap(goddess->Optional.ofNullable(goddess.getName())).isPresent());
}
}
控制台输出如下:
true
false
蒙娜丽莎
false
蒙娜丽莎
false
再来看一下java.util.Optional
源码
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (!isPresent())
return this;
else
return predicate.test(value) ? this : empty();
}
public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent())
return empty();
else {
return Optional.ofNullable(mapper.apply(value));
}
}
public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent())
return empty();
else {
return Objects.requireNonNull(mapper.apply(value));
}
}
结语:回到最初的需求
假如我们扩展一下我们的需求
定义一个方法,获取男人心中的女神,要求男人不存在时抛出异常,如果男人存在,但是这个男人没有女神,那么给一个默认的女神名字-蒙娜丽莎
import java.util.Optional;
public class GetGoddessOfMan {
public static void main(String[] args) {
Man man = new Man();
System.out.println(getGoddessOfMan(Optional.ofNullable(man)));
man = null;
System.out.println(getGoddessOfMan(Optional.ofNullable(man)));
}
// 获取男人心中的女神
public static String getGoddessOfMan(Optional<Man> optionalMan) {
return optionalMan.orElseThrow(NullPointerException::new).getGoddess().orElse(new Goddess("蒙娜丽莎")).getName();
}
}
这里就比较简洁了,一句话就反映出了需求,且更接近自然语言。如果要用非Optional实现,代码类似下面这种
// 获取男人心中的女神
public static String getGoddessOfMan(Man man) {
Objects.requireNonNull(man);
Goddess goddess = man.getGoddess() == null ? new Goddess("蒙娜丽莎") : man.getGoddess();
return goddess.getName();
}
不过很多小伙伴还是会觉得这个写法看上去有些奇怪。在其他JDK1.8的新特性中,比如Stream流中也有返回Optional<T>
的函数,比如Optional<T> findFirst();
,Optional<T> findAny();
,Optional<T> max(Comparator<? super T> comparator)
, Optional<T> min(Comparator<? super T> comparator);
等等