函数式编程

注重函数 - 关注对数据进行了什么操作

中间操作

image-20221030135723197

image-20220917140401360

去重

authors.stream()
                .distinct()
        ;

查询指定匹配

.filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        author.getAge() == 18;
                    }
                })
    Lambda优化=>
    .filter(author -> author.getAge() == 18)

转换流当中的类型

.map

排序

不能有重复 - .sorted()

降序

空参 - 实现compareble接口

有参-

限制流的最大长度

limit -

跳过前n个元素, 返回剩下的元素

.skip

取出一个新的流 - 新类型

map只能把一个对象转换成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素。

.flatMap

终结操作

遍历

forEach(new Consumer<Author>() {
                    @Override
                    public void accept(Author author) {
                        System.out.println(author.getName());
                    }
                });
  Lambda优化=>
      .forEach(author -> System.out.println(author.getName()))

获取当前流中的元素的个数

.cout

求流中的最大最小值

.max

.min

把当前流转换成一个集合

.collect(Collectors.toList)

.collect(Collectors.toSet)

.collect(Collectors.toMap(a->a.getKey, a->a.getValue))

查找与匹配

.anyMatch(匿名内部类- 判断条件)

.allMatch()

.findAny

.findFirst

归并

reduce

内部操作流程

image-20220919213321625

 posted on 2022-10-30 14:04    阅读(80)  评论(0编辑  收藏  举报