Java 8新特性之 Optional 类
前言
java.util.Optional 是java8中引进的一个新的类,我们通过Optional类的源码可以看到,该方法的作用可以对可能缺失的值进行建模,而不是直接将null赋值给变量。
Optional类产生背景
作为java开发者,业务逻辑中需要经常检查对象是否为空,因此业务中会产生大量的if else 结构和判断对象是否为空的方法(已经有很多判断对象是否为空的方法 例如: StringUtils,Objects,CollectionUtils等工具类)。为了代码的可读性和可维护性,Optional类应运而生。
Optional类API
1: static
/**
* 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
/**
* 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
/**
* If a value is present, apply the provided {@code Optional}-bearing
* mapping function to it, return that result, otherwise return an empty
*/
public<R> Optional<R> flatMap(Function<? super T, Optional<R>> 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:
/**
* 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
/**
* 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
/**
* 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:
如果存在该值,返回包含的值,否则抛出由 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方法和常见用法。这些方法是最原始的方法,可以根据自己的业务开发场景,封装适合自己的工具类,方便快速开发。文章中如有错误之处 请不吝赐教,谢谢读者阅读。
本文来自博客园,作者:笨笨的二黄子,转载请注明原文链接:https://www.cnblogs.com/zwhdd/p/17125123.html