• JDK新特性-Stream流


    Stream流是用来操作集合或者数组中的数据的,Stream流提供了一种更加强大的,更加简单的方式来操作集合或者数组中的数据,代码更加简洁,可读性更好。下面是一个简单的例子:

    1. public class S1 {
    2. public static void main(String[] args) {
    3. List names = new ArrayList<>();
    4. Collections.addAll(names, "张三丰", "张无忌", "周芷若", "赵敏", "张强");
    5. System.out.println(names);
    6. // 找出姓张的,且名字是三个字的存入到新集合中去
    7. System.out.println(getZ3(names));
    8. // 使用Stream流实现上述需求
    9. List list = names.stream()
    10. .filter(s -> s.startsWith("张"))
    11. .filter(s -> s.length()==3)
    12. .collect(Collectors.toList());
    13. System.out.println(list);
    14. }
    15. static List getZ3(List names){
    16. List newNames = new ArrayList<>();
    17. // 遍历集合,然后匹配姓张且为三个字的将其添加到新的集合中去
    18. for (String name : names) {
    19. if (name.length()==3 && name.startsWith("张")){
    20. newNames.add(name);
    21. }
    22. }
    23. return newNames;
    24. }
    25. }

    关于Stream流操作数据的描述:Stream流就像是一条流水线,能够跟数据建立连接,然后调用流水线上的各种方法对数据进行处理、计算等,最后获取处理的结果,遍历、统计、收集到一个新集合中返回。

    Stream流常见的中间方法:

    运用不同中间方法的例子:

    1. // 需求1:对包含分数的集合,找出大于60的,并进行升序排序后输出
    2. List list1 = new ArrayList<>();
    3. Collections.addAll(list1, 59.0, 80.0, 80.1, 85.2, 79.0, 60.0, 90.0, 86.9);
    4. list1.stream().filter(s -> s >= 60).sorted().forEach(s -> System.out.println(s));
    5. // 需求2:找出年龄大于等于23,且年龄小于等于30岁的学生,并按照年龄降序输出
    6. List students = new ArrayList<>();
    7. Student s1 = new Student("张三", 31, 172.1);
    8. Student s2 = new Student("李四", 27, 180.2);
    9. Student s3 = new Student("王五", 23, 179.0);
    10. Student s4 = new Student("赵六", 30, 171.0);
    11. Student s5 = new Student("赵六", 33, 178.0);
    12. Collections.addAll(students, s1, s2, s3, s4, s5);
    13. students.stream().filter(student -> student.age>=23 && student.age<=30).sorted(((o1, o2) -> o2.age - o1.age))
    14. .forEach(student -> System.out.println(student));
    15. // 需求3:找出身高前三名的学生输出
    16. students.stream().sorted(((o1, o2) -> Double.compare(o2.height, o1.height))).limit(3)
    17. .forEach(student -> System.out.println(student));
    18. // 需求4:取出身高最矮的两位同学的信息
    19. System.out.println("最矮的两位同学:");
    20. students.stream().sorted((o1, o2) -> Double.compare(o1.height, o2.height)).limit(2)
    21. .forEach(student -> System.out.println(student));
    22. // 需求5:找出身高超过168的学生的名字,要求去除重复的名字,再输出
    23. students.stream().filter(student -> student.height>168).map(student -> student.name).distinct()
    24. .forEach(s -> System.out.println(s));

    Stream流常见的终结方法:终结方法是指这些方法调用之后就不会继续返回Stream流了

    运用stream流终结方法的例子 

    1. List students = new ArrayList<>();
    2. Student s1 = new Student("张三", 31, 162.1);
    3. Student s2 = new Student("李四", 27, 180.2);
    4. Student s3 = new Student("王五", 23, 179.0);
    5. Student s4 = new Student("赵六", 30, 171.0);
    6. Collections.addAll(students, s1, s2, s3, s4);
    7. // 需求1:计算出身高超过168的学生一共有几人
    8. long count = students.stream().filter(student -> student.height > 168).count();
    9. System.out.println(count);
    10. // 需求2:找出身高最高的学生对象
    11. Student s = students.stream().max(((o1, o2) -> Double.compare(o1.height, o2.height))).get();
    12. System.out.println(s);
    13. // 需求3:找出身高最矮的学生对象
    14. Student ss = students.stream().min(((o1, o2) -> Double.compare(o1.height, o2.height))).get();
    15. System.out.println(ss);
    16. // 需求4:找出身高超过170的学生对象,并放到一个新集合中去返回
    17. List students1 = students.stream().filter(student -> student.height > 170).collect(Collectors.toList());
    18. System.out.println(students1);
    19. // 需求5:找出身高超过170的学生对象,并将其名字和身高放入到一个Map集合中返回
    20. Map stringDoubleMap = students.stream().filter(student -> student.height > 170).collect(Collectors.toMap(a -> a.name, a -> a.height));
    21. System.out.println(stringDoubleMap);
    22. // 将学生对象收集到数组中去
    23. Student[] students2 = students.stream().toArray(len -> new Student[len]);
    24. System.out.println(Arrays.toString(students2));

     

  • 相关阅读:
    UCF(暑期团队赛二)
    从零实现的浏览器Web脚本
    传统 API 管理与测试过程正面临严峻的挑战
    Updating FreeBSD repository catalogue...
    细粒度IP定位参文2(Corr-SLG):A street-level IP geolocation method (2021年)
    vue03
    Debezium系列之:详细整理总结Kafka Connect Configs
    弘辽科技:做好产品标题,让链接免费流量快速爆起来
    java毕业设计论文题目SSM框架车库停车计费系统|停车场
    【SpringBoot】| Spring + SpringMVC + SpringBoot 常用注解总结
  • 原文地址:https://blog.csdn.net/weixin_57244464/article/details/133040272