《Java8 实战》读后笔记

  在阅读Spring5的源码的时候发现不少Java8的新语法不是很了解,故转而学习Java8的基础语法,就是本书《Java8 实战》, 目录结构如下:
这里写图片描述

一、基础知识
1 . 通过行为参数化传递代码
a) Java8之前主要体现在策略模式(java.io.FilenameFilter)上,以前的时候我们通常需要传递一个匿名对象或者实例化具体类才能达到;

public interface FilenameFilter {
    /**
     * Tests if a specified file should be included in a file list.
     *
     * @param   dir    the directory in which the file was found.
     * @param   name   the name of the file.
     * @return  <code>true</code> if and only if the name should be
     * included in the file list; <code>false</code> otherwise.
     */
    boolean accept(File dir, String name);
}

b) Java8可以将方法作为参数传递,这是在以前版本所不具有的,文中称这为一等函数,达到了与值(一等公民)的权利

Function<Double, Double> fn = Math::signum;

2 . Lambda表达式

二、函数式数据处理
这一部分是整本书的核心部分,里面每个章节都要细看(filter, skip, limit, reduce, map, findAny, forEach,collect)

三、高效Java8编程
1. 默认方法: 这算是Java8的一大亮点吧
2. 引入Optional替代null: 从Guava中引入但又只是个半成品,说有用也是有用的,说没有用也可以,使用Optional应该是一种理念吧。
3. 新的日期时间API: 从joda引入但依然是半成品,并没有感觉出相对于现在Date&Calendar&Joda有什么优势,比较鸡肋的补充

三、超越Java8
看了一点点,没看懂


对于Java8的看法

  1. 函数式编程:这个词在本文多次提出,也是Stream & Lambda的使用核心理念;
  2. Stream: Java8的核心核心,因此引入了Lambda语法,默认方法,并行处理流(Spliterator & CompletableFutre)等.
  3. Stream中可以将多个处理管道化,及collect实现多种结果集收集及自定义,非常值得学习;
  4. Java8中接口的默认方法&静态方法放在一起(Java8之前有Collection与Collections两个类),这说明JDK核心代码正在逐渐内聚,避免同概念的多个类同时存在;

猜你喜欢

转载自blog.csdn.net/cockroach02/article/details/81335636