JDK1.8 - Optional
- Optional主要解决Java对象的空指针异常
一、Optional对象创建
// 1.创建一个包装对象值为空的Optional对象
Optional<Object> empty = Optional.empty();
// 2.创建包装对象值非空的Optional对象
Optional<String> optOf = Optional.of("optional");
// 3、创建包装对象值,若为空返回空的Optional对象
Optional<String> optOfNullable1 = Optional.ofNullable(null);
Optional<String> optOfNullable2 = Optional.ofNullable("optional");
源码:
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
二、获取Optional中对象的值
// 1.存在获取值、不存在抛出异常
Optional<String> optOf2 = Optional.of("optional");
String optionalValue = optOf2.get();
源码:
public T get() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}
三、判断Optional的对象是否为空
// 1.不为空返回true,否在返回false
Optional<String> optOfNullable3 = Optional.ofNullable(null);
Optional<String> optOfNullable4 = Optional.ofNullable("optional");
boolean present = optOfNullable3.isPresent(); //false
boolean present1 = optOfNullable4.isPresent(); //true
源码:
public boolean isPresent() {
return value != null;
}
四、Optional对象不为空执行函数
// 1.对象不为空执行函数,为空不做操作 (返回值为void)
Optional<String> optOfNullable5 = Optional.ofNullable("optional");
optOfNullable5.ifPresent(new Consumer<String>() {
@Override
public void accept(String s) {
System.err.println(s);
}
});
源码:
public void ifPresent(Consumer<? super T> consumer) {
if (value != null)
consumer.accept(value);
}
五、过滤Optional对象
// 1. 对对象进行过滤,符合直接返回、否则返回一个空Optional对象
Optional<String> optOfNullable6 = Optional.ofNullable("optional");
Optional<String> xxx = optOfNullable6.filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals("xxx");
}
});
源码:
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (!isPresent())
return this;
else
return predicate.test(value) ? this : empty();
}
六、Optional内的对象进行二次包装
Optional<Integer> integer = Optional.ofNullable(1);
// 1.Optional.map() map会自动将计算结果封装为Optional对象
Optional<Object> integerOptional = integer.map(new Function<Integer, Object>() {
@Override
public Object apply(Integer integer) {
return integer + 1;
}
});
System.err.println(integerOptional.get());
// 2.Optional.flatMap() flatMap需要自己进行封装
Optional<Integer> integer1 = integer.flatMap(new Function<Integer, Optional<Integer>>() {
@Override
public Optional<Integer> apply(Integer integer) {
return Optional.ofNullable(integer + 1);
}
});
System.err.println(integer1.get());
源码:
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));
}
}
七、Optional对象为空,返回的默认对象,否则返回本身 - 饿汉式
Optional<String> optOfNullable = Optional.ofNullable(null);
String orElse = optOfNullable.orElse("123");
源码:
public T orElse(T other) {
return value != null ? value : other;
}
八、Optional对象不为空,value返回原值、否则返回另一个Optional的value - 懒汉式
Optional<String> optOfNullable8 = Optional.ofNullable(null);
optOfNullable8.orElseGet(new Supplier<String>() {
@Override
public String get() {
return Optional.of("xxx").get();
}
});
源码:
public T orElseGet(Supplier<? extends T> other) {
return value != null ? value : other.get();
}
九、Optional对象为空时抛出异常
Optional<String> optOfNullable9 = Optional.ofNullable(null);
try {
String orElseThrow = optOfNullable9.orElseThrow(new Supplier<Throwable>() {
@Override
public Throwable get() {
return new RuntimeException("数据为空!");
}
});
System.err.println(orElseThrow);
} catch (Throwable e) {
e.printStackTrace();
}
源码:
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
return value;
} else {
throw exceptionSupplier.get();
}
}