关于css:使用Optional不再头疼NPE

57次阅读

共计 19646 个字符,预计需要花费 50 分钟才能阅读完成。

前言

在 Java 语言开发中,可能大多数程序员遇到最多的异样就是 NullPointException 空指针异样了。这个当初语言的开发者“仅仅因为这样实现起来更容易”而容许空援用所带来的代价是十分惨痛的。而咱们开发者不得不应用多重 if 嵌套判断来躲避 NPE 或者通过多个 if 联合 return 语句来终止程序。且看一个例子

如果须要解决上面的嵌套对象,这是一个用于汽车、汽车保险的客户。

public class Person {
    private Car car;
    public Car getCar() {return car;}
}
public class Car {
    private Insurance insurance;
    public Insurance getInsurance() {return insurance;}
}
public class Insurance {
    private String name;
    public String getName() {return name;}
} 

那么上面的代码会存在怎么的问题呢?

public String getCarInsuranceNames(Person person) {return person.getCar().getInsurance().getName();
} 

没错,当这个人没有车 / 他的车没有上保险时,代码会抛出 NPE。或者说这个人根本就是 null,也会间接抛出异样。咱们常见的作法就是在每次 get 办法之后,进行 if 判断,减少代码的健壮性。可是这样代码会显得非常臃肿。Java 语言的开发者们也在关注着这些问题。因而在 Java8 提供了新的 API:java.util.Optional 用来优雅的解决 null。接下来就请读者和我一起揭开 Optional 神秘的面纱吧!

PS:Optional 类提供的很多 API 联合 Lambda 表达式食用更佳,另外还有很多 API 和 Stream 流中同名 API 的思维基本一致。因而倡议读者后行理解这两个知识点,能够在我的博客 Java8 新个性 标签下学习

申明:本文首发于博客园,作者:后青春期的 Keats;地址:https://www.cnblogs.com/keatsCoder/ 转载请注明,谢谢!

Optional 入门

Optional 类是一个封装 T 值的类,变量 T 存在时,Optional 只是对他做一个简略的封装,如果 T 不存在,缺失的值会被建模成一个空对象,由 Optional.empty() 返回。上面这张图能够形象的形容 Optional

当初咱们尝试着重构之前对于 人 车 保险 的代码

public class Person {
    private Optional<Car> car;
    public Optional<Car> getCar() {return car;}
}
public class Car {
    private Optional<Insurance> insurance;
    public Optional<Insurance> getInsurance() {return insurance;}
}
public class Insurance {
    private String name;
    public String getName() {return name;}
} 

留神:对于保险来说,咱们从逻辑层面限定每个保险公司都有名称,如果没有,那个别是数据出了问题而非代码的问题,开发者应该着手去寻找为什么数据库存在名字为空的保险公司。而不是这里抛出 NPE,故而咱们不必将 Insurance 的 name 字段应用 Optional 包裹

通过下面的代码,咱们曾经将对象由 Optional 所包裹了,那接下来咱们该如何应用它呢?

创立 Optional 对象

创立一个空对象

Optional<Object> empty = Optional.empty(); 

Optional.empty(); 该办法返回一个空对象,

依据一个非空值创立

Optional<Car> car = Optional.of(c); 

Optional.of(T t); 办法会返回一个 Optional 对象,然而须要留神,如果 of 办法参数是 null,该行会抛出 NPE。

容许空值创立

Optional car = Optional.ofNullable(c);

为了防止在创立 Optional 对象时,因为源对象为空而引发的 NPE,该类还提供了 ofNullable 办法,当参数为 null 时,返回 Optional.empty()。外部的 API 是这样的

public static <T> Optional<T> ofNullable(T value) {return value == null ? empty() : of(value);
} 

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));
    }
} 

Optional 类提供 map 办法,接管一个函数式接口 Function 的实现类,如果调用者是空的,则返回 empty(), 否则对 Optional 中的对象 value 调用 Function 实现类中的 apply() 办法,再包装成 Optional 返回。能够用上面的图直观的看到 map 执行的过程:

请留神,在 map 执行完 apply 办法拿到返回值之后,会被动将返回值再次包裹成 Optional 对象。因而咱们如果依照上面的形式革新咱们之前的办法,编译是无奈通过的:

person.map(Person::getCar).map(Car::getInsurance).map(Insurance::getName); 

咱们来剖析一下:person.map(Person::getCar) 革新后的 person 类中,getCar 办法返回 Optional 对象,而 map 又将 Optional 包装到 Optional 中,造成 Optional<Optional> emmm… 套娃式包装。两层包装的 car 是无奈通过 map 在调用 getInsurance 办法的。

侥幸的是,和 Stream 一样,Optional 也提供了扁平化流的办法 flatMap()。且看源码

public <U> Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) {Objects.requireNonNull(mapper);
    if (!isPresent()) {return empty();
    } else {@SuppressWarnings("unchecked")
        Optional<U> r = (Optional<U>) mapper.apply(value);
        return Objects.requireNonNull(r);
    }
} 

flatMap() 比 map() 办法多了一个执行完后将嵌套 Optional 强转成 Optional 的操作,防止了流不能持续应用的难堪处境。因而,咱们能够将获取保险公司名称的办法革新成上面这样:

public String getCarInsuranceName(Optional<Person> person) {return person.flatMap(Person::getCar)
        .flatMap(Car::getInsurance)
        .map(Insurance::getName)
        .orElse("Unknown");
} 

其中 orElse() 办法示意当最终 Optional 包裹的对象还是空时,返回的默认值

PS:因为 Optional 并没有实现序列化接口,因而如果你的我的项目中应用了某些要求序列化的框架,并且在某个类中应用 Optional 包裹了字段。可能会由序列化引发程序故障。

操作 Optional 中的变量

get()

通过 get() 办法获取变量,如果变量存在就间接失去该变量,否则抛出一个 throw new NoSuchElementException(“No value present”); 异样。个别不倡议应用该办法 毕竟间接用 get() 办法了,还要整 Optional 这些花里胡哨的干啥呢

orElse()

在对象为 null 时提供一个默认值

orElseGet(Supplier<? extends T> other)

在对象为 null 通过调用 supplier 提供者接口的实现,返回一个值

orElseThrow()

在对象为 null 抛出一个可定制的异样信息,能够用来抛出我的项目中的自定义异样,以便全局异样捕捉器抓取及响应数据

ifPresent(Consumer<? super T> action)

当对象不为 null 时,执行消费者操作。为 null 时啥也不干

更优雅的判断语句

咱们经常调用某个对象的某个办法去判断其属性。为了平安操作。首先须要对该对象进行非空校验。例如要查看保险公司名称是否为 Keats,须要这么写

if(i != null && "Keats".equals(i.getName())){System.out.println("yes");
} 

当初咱们能够这么写 https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…

Optional<Insurance> insurance = Optional.ofNullable(i);
insurance.filter(in -> "Keats".equals(in.getName())).ifPresent(in -> System.out.println("yes")); 

先看 filter 的源码

public Optional<T> filter(Predicate<? super T> predicate) {Objects.requireNonNull(predicate);
    if (!isPresent()) {return this;} else {return predicate.test(value) ? this : empty();}
} 

首先第一步查看了谓词实现非空,第二步判断 Optional 中的对象如果为空则返回空 Optional,如果不为空执行谓词办法,条件成立则返回该对象。否则返回空 Optional。即 仅当 Optional 中对象不为 null 且符合条件时,返回该对象 之后通过 ifPresent() 办法执行接下来的逻辑。十分不便易懂

其余

Optional 还提供了一些根底类型对象对应的类,如 OptionalInt、OptionalLong 同 Stream 流一样,采纳基本操作类型解决数据,防止了主动拆装箱带来的性能损失。但却就义了 map、flatMap、filter 办法。开发中需酌情应用

正文完
 0