1、java1.8的集合/数组的Stream操作和函数式编程。

2、Stream(流)创建。

a、集合.stream()。

b、Arrays.stream(数组)

c、Stream.of(元素1,元素2。。。元素n)

3、流操作代码:

实体类 SeniorStudent

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ApiModel(description = "学生信息")
public class SeniorStudent {private String name;private Integer age;private Integer mathScore;private Integer chineseScore;}
        List<SeniorStudent> studentList = new ArrayList();studentList.add(new SeniorStudent("lilei",18,89,67));studentList.add(new SeniorStudent("hanmeimei",20,34,98));studentList.add(new SeniorStudent("zhangsan",18,90,29));studentList.add(new SeniorStudent("lisi",22,76,87));// 将对象的一个字段做key,然后对象值作为value,组装成一个MapMap<String, SeniorStudent> collectMap = studentList.stream().collect(Collectors.toMap(SeniorStudent::getName, s -> new SeniorStudent() {{setName(s.getName());setAge(s.getAge());setChineseScore(s.getChineseScore());setMathScore(s.getMathScore());}}));System.out.println(collectMap);// filter 过滤器返回 Stream 然后准换成 SeniorStudent 集合System.out.println(studentList.stream().filter(s -> s.getMathScore() > 85).collect(Collectors.toList()));System.out.println(studentList.stream().filter(s -> s.getMathScore() > 85 && s.getChineseScore() < 60).collect(Collectors.toList()));System.out.println(studentList.stream().filter(s -> s.getMathScore() + s.getChineseScore() > 160).collect(Collectors.toSet()));// count 统计数量System.out.println(studentList.stream().count());// limit 取前几个值studentList.stream().limit(2).forEach(System.out::println);// skip 跳过前几个studentList.stream().skip(2).forEach(System.out::println);// distinct 去重studentList.add(new SeniorStudent("lisi",22,76,87));System.out.println(studentList.stream().count());studentList.stream().distinct().forEach(System.out::println);// map 映射属性,可以对每个属性操作studentList.stream().map(s -> {s.setAge(s.getAge()+1);return s;}).forEach(System.out::println);System.out.println();// sort 正序studentList.stream().sorted( (a,b) -> {return a.getChineseScore().compareTo(b.getChineseScore());}).forEach(System.out::println);System.out.println();// sort 反序studentList.stream().sorted( (a,b) -> {return b.getChineseScore().compareTo(a.getChineseScore());}).forEach(System.out::println);System.out.println();// 获取第一个System.out.println(studentList.stream().findFirst().get());System.out.println();// stream 的 findAny方法获取的是第一条,stream的paralle的findAny是随机获取一个System.out.println(studentList.stream().findAny().get());System.out.println(studentList.stream().parallel().findAny().get());System.out.println();// anyMatch 任何一个元素符合条件,allMatch 所有元素都符合条件,noneMatch 没有元素符合条件System.out.println(studentList.stream().anyMatch(s -> s.getName().equals("lilei")));System.out.println(studentList.stream().allMatch(s -> s.getName().equals("lilei")));System.out.println(studentList.stream().noneMatch(s -> s.getName().equals("lilei")));System.out.println();// max min 元素中某个属性最大和最小的System.out.println(studentList.stream().max((a,b) -> a.getChineseScore().compareTo(b.getChineseScore())).get());System.out.println(studentList.stream().min((a,b) -> a.getChineseScore().compareTo(b.getChineseScore())).get());System.out.println(studentList.stream().max((a,b) -> b.getChineseScore().compareTo(a.getChineseScore())).get());System.out.println();// 对集合中某个元素的属性进行统计System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)));System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getCount());System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getSum());System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getMax());System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getMin());System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getAverage());

Java1.8特性之Stream流操作相关推荐

  1. 跟我学 Java 8 新特性之 Stream 流(三)缩减操作

    转载自   跟我学 Java 8 新特性之 Stream 流(三)缩减操作 和前面两篇文章一起服用,效果会更佳.通过对流API的基础体验Demo和关键知识点的讲解,相信大家对流API都有一定的认识了, ...

  2. 跟我学 Java 8 新特性之 Stream 流(七)流与迭代器,流系列大结局

    转载自   跟我学 Java 8 新特性之 Stream 流(七)流与迭代器,流系列大结局 恭喜你们,马上就要学完Java8 Stream流的一整系列了,其实我相信Stream流对很多使用Java的同 ...

  3. 跟我学 Java 8 新特性之 Stream 流(六)收集

    转载自   跟我学 Java 8 新特性之 Stream 流(六)收集 我们前面的五篇文章基本都是在说将一个集合转成一个流,然后对流进行操作,其实这种操作是最多的,但有时候我们也是需要从流中收集起一些 ...

  4. 跟我学 Java 8 新特性之 Stream 流基础体验

    转载自   跟我学 Java 8 新特性之 Stream 流基础体验 Java8新增的功能中,要数lambda表达式和流API最为重要了.这篇文章主要介绍流API的基础,也是流API系列的第一篇文章, ...

  5. 跟我学 Java 8 新特性之 Stream 流(二)关键知识点

    转载自   跟我学 Java 8 新特性之 Stream 流(二)关键知识点 我们的第一篇文章,主要是通过一个Demo,让大家体验了一下使用流API的那种酣畅淋漓的感觉.如果你没有实践,我还是再次呼吁 ...

  6. 跟我学 Java 8 新特性之 Stream 流(四)并行流

    转载自   跟我学 Java 8 新特性之 Stream 流(四)并行流 随着对流API认识的慢慢深入,本章我们要讨论的知识点是流API里面的并行流了. 在开始讨论并行流之前,我先引发一下大家的思考, ...

  7. 跟我学 Java 8 新特性之 Stream 流(五)映射

    转载自   跟我学 Java 8 新特性之 Stream 流(五)映射 经过了前面四篇文章的学习,相信大家对Stream流已经是相当的熟悉了,同时也掌握了一些高级功能了,如果你之前有阅读过集合框架的基 ...

  8. java 新特性之 stream 流

    java 新特性之 stream 流 Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. 这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在 ...

  9. List增删元素后size大小发生变化带来的影响、Stream流操作、Lambda表达式

    目录 List增删元素后size大小发生变化带来的影响 List的几种遍历方式 报异常原因 增强for循环原理 异常原理 建议删除操作 性能对比 Stream流操作 Lambda表达式 语法 Lamb ...

最新文章

  1. ZZULIOJ 1918: G 【二分图匹配】
  2. 对于fmri的设计矩阵构造的一个很直观的解释-by 西南大学xulei教授
  3. Entity Framework 无法对没有主键的视图映射实体的解决办法
  4. 使用cocoapods
  5. iphone手机型号获取
  6. 前端学习(3099):vue+element今日头条管理-使用富文本比编辑器
  7. C语言三目运算符 - C语言零基础入门教程
  8. mysql可重复读实验_Mysql可重复读测试
  9. 5个冷门的MacOS快捷键,小众但好用
  10. mac 下 sublime text 运行c++/c 不能使用scanf/cin
  11. Spring Cloud:服务消费(Ribbon)【Dalston版】
  12. 网工学习笔记——reboot
  13. 2022-2028年中国有色金属市场供需前景预测及投资策略研究报告
  14. “三权分立”模型之角色模型
  15. CSDN-JayChou测试
  16. lerna 使用详解
  17. 智能客服选型产品选型比较:晓多、奇智、春松客服
  18. 二.java-jak和jre安装与配置
  19. php幻灯片图片不显示不出来,首页幻灯片中图片无法显示的解决办法
  20. XPC connection interrupted

热门文章

  1. linux服务器如何修改mysql端口,两种方法
  2. 困住外卖骑手的系统,用的是什么算法?
  3. android 折叠动画,Android:展开/折叠动画
  4. Linux安装tomcat方法步骤
  5. Appium+python自动化29-appium对博客园APP进行自动化测试
  6. Jquery 获取元素节点
  7. 1.docker网桥自定义配置
  8. Cordova嵌入Android项目(CordovaWebView)
  9. 用你的邮箱为你看家护院
  10. 微信开发众筹项目视频教学小程序+java后端开发+mysql数据库