标签:合并 ram pre 提高 注意 java super 数学 highlight
本文主要介绍Java8的2大主要新特性lambda表达式和Stream API,2者提供了更高层次的抽象,简化开发,提高生产效率。
创建一个线程,使用了一个Runnable匿名内部类
Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello Aron.");
            }
        });
看着问题不大,实际上弊端挺明显:模板语法太多,真正有业务意义的的语句只有System.out.println("Hello Aron."),因为如此,也严重干扰了我们阅读代码。
引入lambda表达式后,则可以这么写
Thread thread = new Thread(() -> System.out.println("Hello Aron."));
流(Stream)是Java8的新特性,是一种使程序员得以站在更高的抽象层次上对集合进行操作。在思路上,类似于SQL的存储过程,有几个步骤:
其中,第1步的定义操作叫惰性求值,给你套路(返回Stream),但是不会执行返回结果。
第2步的触发操作叫及早求值,这个人说干就干,立马要结果(返回结果数据)。
第3步的筛选类似SQL的where子句,对结果进一步的筛选。
Stream 类位于java.util.stream包,是Java8核心的类之一,拥有众多方法,下面罗列了一些比较重要的方法进行讲解。更多的是抛砖引玉,任何教程都比不上自己的悟性来得爽快,先找点感觉,先掌握基本用法尝试使用起来,慢慢自然就会了。
要使用Stream,那就必须得先创建一个String类型的Stream
Stream<String> StrStream = Stream.of("a", "b");使用收集器Collector将StrStream转化为熟悉的集合Collection
List<String> collect = StrStream.collect(Collectors.toList());
所谓map,从字面理解就是映射。这里指的是对象关系的映射,
比如从对象集合映射到属性结合:
List<String> names = Stream.of(new Student("zhangSan"), new Student("liSi"))
                        .map(student -> student.getName())
                        .collect(toList());从小写字母映射到大写字母:
List<String> collected = Stream.of("a", "b", "hello")
                        .map(string -> string.toUpperCase())
                        .collect(toList());将 字符串流 根据空格分割成 字符串数组流
Stream<String> stringStream = Stream.of("Hello Aron.");
Stream<String[]> stringArrayStream = stringStream.map(word -> word.split(" ")); filter顾名思义,过滤筛选。这里的参数函数接口是一个条件,筛选出满足条件的元素
// 筛选年龄大于19的学生
List<Student> stu = Stream.of(new Student("zhangSan", 19), new Student("liSi"), 20)
                        .filter(student -> student.getAge() > 19)
                        .collect(toList());
flatMap扁平化映射,即将数据元素为数组的Stream转化为单个元素
Stream<String> stringStream = Stream.of("Hello Aron.");
// 返回值为数组
Stream<String[]> stringArrayStream = stringStream.map(word -> word.split(" "));
// flatMap扁平化映射后,元素都合并了
Stream<String> flatResult = stringArrayStream.flatMap(arr -> Arrays.stream(arr))
max即最大,类似SQL中的函数max(),从数据中根据一定的条件筛选出最值
// 筛选年龄最大/小的学生
Stream<Student> studentStream = Stream.of(new Student("zhangSam", 19), new Student("liSi", 20));
Optional<Student> max = studentStream.max(Comparator.comparing(student -> student.getAge()));
// Optional<Student> max = studentStream.min(Comparator.comparing(student -> student.getAge()));
// 年龄最大/小的学生
Student student = max.get();
reduce操作实现从一组值中生成一个值,上面的max、min实际上都是reduce操作。
参数Identity 表示初始值,
参数binaryOperator是一个函数接口,表示二元操作,可用于数学运算
// 使用reduce() 求和 (不推荐生产环境使用) int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
上面代码,展开reduce() 操作
BinaryOperator<Integer> accumulator = (acc, element) -> acc + element; int count = accumulator.apply( accumulator.apply(accumulator.apply(0, 1),2), 3);
// 查找所有姓张的同学并按字典顺序排序,存储到list
List<Student> newList = studentList.Stream()
            .filter(student -> student.getName().startsWith("张"))
            .sorted(Comparator.comparing(student -> student.getName())
            .collect(toList());
标签:合并 ram pre 提高 注意 java super 数学 highlight
原文地址:https://www.cnblogs.com/jiehanshi/p/12125677.html