Java 8新特性之 Optional 类

2023-05-24,,

前言

java.util.Optional 是java8中引进的一个新的类,我们通过Optional类的源码可以看到,该方法的作用可以对可能缺失的值进行建模,而不是直接将null赋值给变量。

Optional类产生背景

作为java开发者,业务逻辑中需要经常检查对象是否为空,因此业务中会产生大量的if else 结构和判断对象是否为空的方法(已经有很多判断对象是否为空的方法 例如: StringUtils,Objects,CollectionUtils等工具类)。为了代码的可读性和可维护性,Optional类应运而生。

Optional类API

1: static Optional empty() 返回空的 Optional 实例。

/**
* Returns an empty {@code Optional} instance. No value is present for this
* Optional.
* @return an empty {@code Optional}
*/
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}

2: Optional filter(Predicate<? super predicate) 如果值存在,并且这个值匹配给定的 predicate,返回一个Optional用以描述这个值,否则返回一个空的Optional

/**
* If a value is present, and the value matches the given predicate,
* return an {@code Optional} describing the value, otherwise return an
* empty {@code Optional}.
*/
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (!isPresent())
return this;
else
return predicate.test(value) ? this : empty();
}

3: Optional flatMap(Function<? super T,Optional> mapper) 如果值存在,返回基于Optional包含的映射方法的值,否则返回一个空的Optional

/**
* If a value is present, apply the provided {@code Optional}-bearing
* mapping function to it, return that result, otherwise return an empty
*/
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));
}
}

4:T get() 如果在这个Optional中包含这个值,返回值,否则抛出异常:NoSuchElementException

/**
* If a value is present in this {@code Optional}, returns the value,
* otherwise throws {@code NoSuchElementException}.
*/
public T get() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}

5: void ifPresent(Consumer<? super T> consumer) 如果值存在则使用该值调用 consumer , 否则不做任何事情。

/**
* If a value is present, invoke the specified consumer with the value,
* otherwise do nothing.
*
* @throws NullPointerException if value is present and {@code consumer} is
* null
*/
public void ifPresent(Consumer<? super T> consumer) {
if (value != null)
consumer.accept(value);
}

6: boolean isPresent() 如果值存在则方法会返回true,否则返回 false

/**
* Return {@code true} if there is a value present, otherwise {@code false}.
*
* @return {@code true} if there is a value present, otherwise {@code false}
*/
public boolean isPresent() {
return value != null;
}

7: Optional map(Function<? super T,? extends U> mapper) 如果有值,则对其执行调用映射函数得到返回值。如果返回值不为 null,则创建包含映射返回值的Optional作为map方法返回值,否则返回空Optional

/**
* If a value is present, apply the provided mapping function to it,
* and if the result is non-null, return an {@code Optional} describing the
* result. Otherwise return an empty {@code Optional}.
*
* @apiNote This method supports post-processing on optional values, without
* the need to explicitly check for a return status. For example, the
* following code traverses a stream of file names, selects one that has
* not yet been processed, and then opens that file, returning an
* {@code Optional<FileInputStream>}:
*
* <pre>{@code
* Optional<FileInputStream> fis =
* names.stream().filter(name -> !isProcessedYet(name))
* .findFirst()
* .map(name -> new FileInputStream(name));
* }</pre>
*
* Here, {@code findFirst} returns an {@code Optional<String>}, and then
* {@code map} returns an {@code Optional<FileInputStream>} for the desired
* file if one exists.
*/
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));
}
}

8: static Optional of(T value) 返回一个指定非null值的Optional。

/**
* Returns an {@code Optional} with the specified present non-null value.
*/
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}

9: static Optional ofNullable(T value) 如果为非空,返回 Optional 描述的指定值,否则返回空的 Optional

/**
* Returns an {@code Optional} describing the specified value, if non-null,
* otherwise returns an empty {@code Optional}.
*/
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}

10: T orElse(T other) 如果存在该值,返回值, 否则返回 other

/**
* Return the value if present, otherwise return {@code other}.
*
* @param other the value to be returned if there is no value present, may
* be null
* @return the value, if present, otherwise {@code other}
*/
public T orElse(T other) {
return value != null ? value : other;
}

11: T orElseGet(Supplier<? extends T> other) 如果存在该值,返回值, 否则触发 other,并返回 other 调用的结果。

/**
* Return the value if present, otherwise invoke {@code other} and return
* the result of that invocation.
*/
public T orElseGet(Supplier<? extends T> other) {
return value != null ? value : other.get();
}

12: T orElseThrow(Supplier<? extends X> exceptionSupplier)

如果存在该值,返回包含的值,否则抛出由 Supplier 继承的异常

/**
* Return the contained value, if present, otherwise throw an exception
* to be created by the provided supplier.
*
* @apiNote A method reference to the exception constructor with an empty
* argument list can be used as the supplier. For example,
* {@code IllegalStateException::new}
*/
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
return value;
} else {
throw exceptionSupplier.get();
}
}

Optional类使用

1: 判断方法返回值

public class OptionalTest {
public static void main(String[] args) {
Optional<User> user = Optional.of(getUser());
if (user.isPresent()) {
//执行其他代码
}
} public static User getUser() {
return new User();
} }

2: 判断方法参数值

public static List<User> getUsers(Params params) {
//判断参数params是否为空,并取OrganizationId的值
String organizationId = Optional.ofNullable(params).map(Params::getOrganizationId).orElse(null);
List<User> users = new ArrayList<>();
users.add(new User());
return users;
}

除了上面常用的方法 还有一些API根据自己的需求进行组装,满足自己的业务逻辑。

Optional工具类

// 判断List是否为空
public <T> Stream<T> ofNullable(Iterable<T> target) {
if (target != null) {
return Lists.newArrayList(target).stream();
} else {
return empty();
}
}
//过滤List中null的元素
public <T> List<T> filterNullElem(Iterable<T> target) {
return ofNullable(target).filter(Objects::nonNull).collect(Collectors.toList());
}

总结

上面介绍了Optional类的API方法和常见用法。这些方法是最原始的方法,可以根据自己的业务开发场景,封装适合自己的工具类,方便快速开发。文章中如有错误之处 请不吝赐教,谢谢读者阅读。

Java 8新特性之 Optional 类的相关教程结束。

《Java 8新特性之 Optional 类.doc》

下载本文的Word格式文档,以方便收藏与打印。