首页 > 编程语言 >Java 8新特性之 Optional 类

Java 8新特性之 Optional 类

时间:2023-02-15 23:11:48浏览次数:40  
标签:code return value 特性 Java null Optional public

前言

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

image.png

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方法和常见用法。这些方法是最原始的方法,可以根据自己的业务开发场景,封装适合自己的工具类,方便快速开发。文章中如有错误之处 请不吝赐教,谢谢读者阅读。

标签:code,return,value,特性,Java,null,Optional,public
From: https://www.cnblogs.com/zwhdd/p/17125123.html

相关文章

  • JavaWeb文件上传(感谢狂神)
    1、准备工作采用Apache的开源工具common-fileupload这个文件上传组件。common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。(这两个jar包需要下载引入,Tomc......
  • Java基础语法
    Java基础语法注释注释是不会执行的,而是给写代码的人看的。分为单行注释、多行注释、文档注释。单行注释://注释内容多行注释:/*注释内容*/文档注释(JavaDoc)......
  • Java Lambda
    JavaLambda输出符合表达式的每一个对象employees.stream().filter(p->p.getAge()>21).forEach(System.out::println);返回一个符合表达式的集合employees.st......
  • Java编译异常捕捉与上报笔记
    异常处理机制的作用:增强程序的健壮性处理编译异常方式一:在方法声明位置上使用throws关键字抛出,谁调用该方法,就交给谁处理注意:为Exception的是需要处理的,否则编译器会报......
  • 学习笔记分享:java面试(JDK、JRE、JVM的区别)
    简答题、问答题:1.JDK、JRE、JVM的区别:1)JDK:java开发工具包,是java的核心,包括:JRE+编译、运行等命令工具2)JRE:java运行环境,是运行java程序所必须的环境集合,包括:JVM+......
  • java-studyDay03-面向对象
    生成Java帮助文档:命令格式:javadoc–d文件夹名–auther–version*.java//格式/***类描述*@author作者名*@versio......
  • Java常用类的一些基础API的使用
    数字相关类、日期时间API、系统相关类、数组工具类及自然排序和定制排序的介绍Author:MsuenbDate:2023-02-15数字相关类Math类java.lang.Math类包含用于执行基......
  • 如何使用php构造JAVA的包含数组元素的JSON对象
    提问: 最近做开发,接口是JAVA,这边使用PHP对接,接口要求一个字段是JSON对象,JSON对象中的字段是数组,格式如下:"result":{"JSON":{"ARRAY":[]......
  • Java开发工具IntelliJ IDEA 2020.2完整授权流程
    最近几年,Java的技术栈发展的非常快,Java作为一门十分流行的面向对象编程语言,其开发工具也是非常多的,当然因为接触时间长短以及个人喜好,每个人都有自己的选择。对此,我对目前......
  • Java判断两个字符串(对象是否相等)
    Java判断两个字符串(对象是否相等)在Java中,常见的判断两个字符串(对象)是否相等的方法有两个,一个是.equals()方法,还有一个是"=="操作符,这两个的主要区别如下:.equals()方法比......