Java8 Stream 使用

Stream 接口

所在包:import java.util.stream.Stream;

A sequence of elements supporting sequential and parallel aggregate operations.

  • Stream 是元素的集合,类似Iterator
  • 支持顺序和并行的聚合操作

Iterator VS Stream

  • Iterator,用户只能一个一个的遍历元素并对其执行某些操作
  • Stream,用户只要给出需要对其包含的元素执行什么操作,比如“过滤掉长度大于10的字符串”、“获取每个字符串的首字母”等,具体这些操作如何应用到每个元素上,都由 Stream 完成

使用Stream的基本步骤

  • 创建 Stream
    • 通过 Stream 接口的静态工厂方法 (注意:Java8里接口可以带静态方法)
    • 通过 Collection 接口的默认方法 stream(),把一个 Collection 对象转换成Stream (较常用)
  • 转换 Stream,每次转换原有Stream对象不改变,返回一个新的Stream对象
  • 对 Stream 进行聚合(Reduce)操作

Stream 的转换

  • distinct 对 Stream 中包含的元素进行去重操作
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
list.stream().distinct().forEach((s) -> {System.out.print(s + " ");});
  • filter(Predicate<? super T> predicate) 对 Stream 中包含的元素使用给定的过滤函数进行过滤操作
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
list.stream().filter((s) -> s != null).forEach((s) -> {System.out.print(s + " ");});
  • map(Function<? super T, ? extends R> mapper) 对 Stream 中包含的元素使用给定的转换函数进行转换操作
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
list.stream().filter((s) -> s != null).map((s) -> s + 1).forEach((s) -> {
    System.out.print(s + " ");
});
  • limit(long maxSize) 对一个 Stream 进行截断操作,获取其前 N 个元素
  • skip(long n) 返回一个丢弃原 Stream 的前 N 个元素后剩下元素组成的新 Stream
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
list.stream().filter((s) -> s != null).limit(2).forEach((s) -> {
    System.out.print(s + " ");
});

对 Stream 进行聚合(Reduce)操作

  • sum() max() count()
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
IntStream is = list.stream().filter((s) -> s != null).mapToInt(Integer::intValue);
System.out.println(is.max());
  • collect
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
List<Integer> result = list.stream().filter((s) -> s != null).collect(Collectors.toList());
result.forEach((s) -> {
    System.out.print(s + " ");
});
  • reduce(BinaryOperator<T> accumulator)
    sum() max() count() 等都可以使用 reduce() 实现

reduce 方法接受一个函数,这个函数有两个参数:
第一个参数 s1 是上次函数执行的返回值(也称为中间结果)
第二个参数 s2 是 stream 中的元素,这个函数把这两个值相加,得到的和会被赋值给下次执行这个函数的第一个参数
第一次执行的时候第一个参数的值是 Stream 的第一个元素,第二个参数是 Stream 的第二个元素
这个方法返回值类型是Optional。

List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
Optional<Integer> op = list.stream().filter((s) -> s != null).reduce((s1, s2) -> s1 + s2);
System.out.println(op.get()); // 相当于求和
  • 搜索相关
    • allMatch 是不是Stream中的所有元素都满足给定的匹配条件
    • anyMatch Stream中是否存在任何一个元素满足匹配条件
    • findFirst 返回Stream中的第一个元素,如果Stream为空,返回空Optional
    • noneMatch 是不是Stream中的所有元素都不满足给定的匹配条件
List<Integer> list = Arrays.asList(1, null, 1, 2, null, 5, 3, 9, 7);
System.out.println(list.stream().filter((s) -> s != null).allMatch((s) -> s > 0));

引用:
Java8初体验(2):Stream语法详解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言: 讲Stream之前,先来用个小需求带入本文。毕竟代码看的最清楚。 正文: 项目某个页面有个需求,将关键词和...
    T9的第三个三角阅读 6,999评论 1 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,359评论 19 139
  • 了解Stream ​ Java8中有两个最为重要的改变,一个是Lambda表达式,另一个就是Stream AP...
    龙历旗阅读 8,578评论 3 4
  • 今天是坚持写文章的第三天,慢慢的感觉写文章越来越没有东西可写了,最近很害怕自己只注重数量而毫不考虑文章的质量。因为...
    西雅图不眠阅读 3,017评论 0 1
  • 班级情况: 校区:科学创想乐高机器人沈河校区 时间:周六14.45-15.45 学员:王梓图 祖朵 任教老师:李静...
    追梦人李静阅读 4,396评论 0 0