| 副标题[/!--empirenews.page--] 
 最近脑袋发热追着java8源码看的很起劲,还有了执念,罪过。 本文以jdk1.8.0_111源码为例 public final class Optional<T> {} 
 Optional是一个为了解决NullPointerException设计而生可以包含对象也可以包含空的容器对象。封装了很多对空处理的方法也增加了filter、map这样的检索利器,其中函数式编程会有种炫酷到爆的感觉。 基础测试用例对象: public class Java8OptionalTest {     List<String> stringList = null;     ICar car = new WeiLaiCar(); }  public class WeiLaiCar implements ICar {     Integer wheels = new Integer(4); }  
 Api中提供的4种optional最核心的当属Optional对象,泛型的引入支持了所有对象类型,又增加对常用场景下的doubleintlong进行扩展。重点介绍一下Optional对象的方法其他三个类似。 
    public final class Optional {public final class OptionalDouble {public final class OptionalInt {public final class OptionalLong { @FunctionalInterface PredicateConsumerSupplier三个接口都是函数式接口 静态方法of private Optional() {     this.value = null; }  
 构造方法被private,不能new但提供了of这样的静态方法去初始化类; 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 static<T> Optional<T> empty() {     @SuppressWarnings("unchecked")     Optional<T> t = (Optional<T>) EMPTY;     return t; }  
 1、empty支持你去创建一个空的optional类,这样的类直接get()会报错:java.util.NoSuchElementException:  No value present 2、of(x)传入的对象不能为null,而ofNullable(x)是支持传入null的对象,一般用这两个比较多。 present 方法 isPresent是用来判断optional中对象是否为null,ifPresent的参数是当对象不为null时执行的lamdba表达式。 public boolean isPresent() {     return value != null; } public void ifPresent(Consumer<? super T> consumer) {     if (value != null)         consumer.accept(value); }  
 示例详解介绍了ifPresent特性: Java8OptionalTest test = new Java8OptionalTest(); Optional<Java8OptionalTest> optional = Optional.of(test);  pringTest(optional.isPresent()); //true optional.ifPresent( a -> pringTest(a.getCar().getClass().getName())); //com.ts.util.optional.WeiLaiCar optional.ifPresent( a -> Optional.ofNullable(a.getStringList()).ifPresent(b -> pringTest("StringList:" + (b == null)))); //第一级的ifPresent是存在test对象,所以执行了lambda表达式,而第二级的ifPresent的stringList是null,所以没有执行表达式 optional.ifPresent( a -> Optional.ofNullable(a.getCar()).ifPresent(b -> pringTest("car:" + (b == null)))); //car:false //第二级ifPresent的car对象是存在的,所以第二级的表达式执行了  
 map 方法 源码提供了两种map和flatMap。 
    map方法的参数是个当包含的对象不为null时才执行的lambda表达式,返回该表达式执行结果的封装optional对象,同理支持链式调用,逐层深入和递归递进很像;flatMap区别在于lambda表达式的返回结果必须主动包裹Optinoal,否则报错  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));     } } 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));     } }  
 (编辑:宣城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |