public final class Optional<T> {
private static final Optional<?> EMPTY = new Optional<>();
private final T value;
private Optional() {
this.value = null;
}
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
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);
}
public T orElse(T other) {
return value != null ? value : other;
}
}
Optional.ofNullable((String) params.get("date")).orElse("2020-11-3");
this.value = null;所以会得到一个value为空的optional执行orElse(T other)的value != null ? value : other;因为为空就返回other