- 成员变量
- 构造方法
- 成员方法
- 1. empty()
- 2. of(T value)
- 3. ofNullable(T value)
- 4. get()
- 5. isPresent()
- 6. ifPresent(Consumer<? super T> consumer)
- 7. filter(Predicate<? super T> predicate)
- 8. map(Function<? super T, ? extends U> mapper)
- 9. flatMap(Function<? super T, Optional> mapper)
- 10. orElse(T other)
- 11. orElseGet(Supplier<? extends T> other)
- 12. orElseThrow(Supplier<? extends X> exceptionSupplier)
public final class Optional<T> {
}
Optional 类是一个可以为 null 的容器对象。如果值存在则 isPresent()
方法会返回 true,调用 get()
方法会返回该对象。
Optional 是个容器:它可以保存类型 T 的值,或者仅仅保存 null。Optional 提供很多有用的方法,这样我们就不用显式进行空值检测。
Optional 类的引入很好的解决空指针异常。
成员变量
/**
* Common instance for {@code empty()}.
*/
private static final Optional<?> EMPTY = new Optional<>();
/**
* If non-null, the value; if null, indicates no value is present
*/
private final T value;
构造方法
/**
* Constructs an empty instance.
*
* @implNote Generally only one empty instance, {@link Optional#EMPTY},
* should exist per VM.
*/
private Optional() {
this.value = null;
}
/**
* Constructs an instance with the value present.
*
* @param value the non-null value to be present
* @throws NullPointerException if value is null
*/
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
成员方法
1. empty()
static <T> Optional<T> empty()
:返回空的 Optional 实例。
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
2. of(T value)
static <T> Optional<T> of(T value)
:返回一个指定非 null 值的 Optional 实例。
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
3. ofNullable(T value)
如果为非空,返回 Optional 描述的指定值,否则返回空的 Optional。
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
4. get()
如果在这个 Optional 中包含这个值,返回值,否则抛出异常:NoSuchElementException。
可以配合方法 isPresent() 使用。
public T get() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}
5. isPresent()
如果值存在则方法会返回 true,否则返回 false。
public boolean isPresent() {
return value != null;
}
6. ifPresent(Consumer<? super T> consumer)
如果值存在则使用该值调用 consumer , 否则不做任何事情。
public void ifPresent(Consumer<? super T> consumer) {
if (value != null)
consumer.accept(value);
}
7. filter(Predicate<? super T> predicate)
如果值存在,并且这个值匹配给定的 predicate,返回一个 Optional 用以描述这个值,否则返回一个空的Optional。predicate 为 null 时会抛出 NullPointerException。
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (!isPresent())
return this;
else
return predicate.test(value) ? this : empty();
}
8. map(Function<? super T, ? extends U> mapper)
如果有值,则对其执行调用映射函数得到返回值。如果返回值不为 null,则创建包含映射返回值的 Optional 作为map 方法返回值,否则返回空 Optional。
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));
}
}
9. flatMap(Function<? super T, Optional> mapper)
如果值存在,返回基于 Optional 包含的映射方法的值,否则返回一个空的 Optional。
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));
}
}
10. orElse(T other)
如果存在该值,返回值, 否则返回 other。
public T orElse(T other) {
return value != null ? value : other;
}
11. orElseGet(Supplier<? extends T> other)
如果存在该值,返回值, 否则触发 other,并返回 other 调用的结果。
public T orElseGet(Supplier<? extends T> other) {
return value != null ? value : other.get();
}
12. orElseThrow(Supplier<? extends X> exceptionSupplier)
如果存在该值,返回包含的值,否则抛出由 Supplier 继承的异常
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
return value;
} else {
throw exceptionSupplier.get();
}
}