• Stream流的使用详解(持续更新)


    1. 对比两List集合数据某些字段一样的情况下取值:

    一般简单方式我们会使用双重for循环来处理判断数据取值(如下代码所示),但是数据量越大的情况下代码效率则越低,并且现在很多公司都会限制for循环层数所以更推荐stream将List转换为Map通过key取值

    普通方式:

    1. for (DashboardsDO aDo : dashboardsDOS) {
    2. for (Target target : targetList) {
    3. if (aDo.getStoreCode().equals(target.getStoreCode())) {
    4. 代码逻辑等等
    5. }
    6. }
    7. }

    stream:

    1. List list1= new ArrayList<>();
    2. List list2= new ArrayList<>();
    3. // 假设以上两个list都有数据我们通过学生姓名name与学号num进行取值
    4. Map studentMap=list2.stream()
    5. .collect(Collectors.toMap(studentMSG-> studentMSG.getName() + "_" + studentMSG.getNum(), Function.identity()));
    6. // 此时我们将name与num作为map的key,值为当前实体类
    7. list1 = list1.stream()
    8. .map(student-> {
    9. // 将list1的name与num作为key
    10. String key = student.getName() + "_" + student.getNum();
    11. // 通过key取值
    12. StudentMSG studentMSG = studentMap.get(key);
    13. // 你的业务逻辑
    14. return student;
    15. }).collect(Collectors.toList());

    2. Stream流过滤出偶数并求和

    1. List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    2. // 使用Stream流过滤出偶数并求和
    3. int sumOfEvenNumbers = numbers.stream()
    4. .filter(n -> n % 2 == 0)
    5. .mapToInt(Integer::intValue)
    6. .sum();
    7. System.out.println("Sum of even numbers: " + sumOfEvenNumbers);

    3. Stream流将数字转换为字符串并连接成一个字符串

    1. List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    2. // 使用Stream流将数字转换为字符串并连接成一个字符串
    3. String joinedNumbers = numbers.stream()
    4. .map(String::valueOf)
    5. .collect(Collectors.joining(", "));
    6. System.out.println("Joined numbers: " + joinedNumbers);

    4.  Stream根据集合某字段进行排序

    1. List people = new ArrayList<>();
    2. people.add(new Person("张三", 30));
    3. people.add(new Person("李四", 25));
    4. people.add(new Person("王五", 35));
    5. // 根据age字段排序
    6. List sortedPeople = people.stream()
    7. .sorted(Comparator.comparingInt(Person::getAge))
    8. .collect(Collectors.toList());
    9. sortedPeople.forEach(person -> System.out.println(person.getName() + ": " + person.getAge()));

    5. Stream根据某字段过滤

    1. List people = new ArrayList<>();
    2. people.add(new Person("张三", 20));
    3. people.add(new Person("李四", 15));
    4. people.add(new Person("王五", 25));
    5. // 过滤年龄大于18岁的人
    6. List adults = people.stream()
    7. .filter(person -> person.getAge() >= 18)
    8. .collect(Collectors.toList());
    9. adults.forEach(person -> System.out.println(person.getName() + ": " + person.getAge()));

    6. Stream去重

    1. List numbers = Arrays.asList(1, 2, 3, 4, 5, 1, 2, 3);
    2. List distinctNumbers = numbers.stream().distinct().collect(Collectors.toList());

     7. Stream两个list根据某相同字段去重

    1. List list1 = new ArrayList<>();
    2. list1.add(new MyClass("A"));
    3. list1.add(new MyClass("B"));
    4. list1.add(new MyClass("C"));
    5. List list2 = new ArrayList<>();
    6. list2.add(new MyClass("B"));
    7. list2.add(new MyClass("C"));
    8. list2.add(new MyClass("D"));
    9. // 根据field字段去重
    10. List result = Stream.concat(list1.stream(), list2.stream())
    11. .distinct()
    12. .collect(Collectors.toList());
    13. for (MyClass item : result) {
    14. System.out.println(item.getField());
    15. }

    8.  映射函数

    1. List strings = Arrays.asList("apple", "banana", "cherry");
    2. List lengths = strings.stream()
    3. .map(String::length)
    4. .collect(Collectors.toList());
    5. System.out.println(lengths); // 输出 [5, 6, 6]

    9. 对元素进行排序

    1. List strings = Arrays.asList("apple", "banana", "cherry", "date");
    2. List sortedStrings = strings.stream()
    3. .sorted((s1, s2) -> Integer.compare(s1.length(), s2.length()))
    4. .collect(Collectors.toList());
    5. System.out.println(sortedStrings); // 输出 [date, apple, cherry, banana]

  • 相关阅读:
    分享VR眼镜加密播放器OEM方案
    快速生成力扣链表题的链表,实现快速调试
    随手记录第四话 -- elasticsearch基于docker安装以及SpringBoot集成使用
    MySQL MHA信息的收集【Filebeat+logstash+MySQL】
    node多版本控制
    elasticsearch 7.X全部版本的新特性与重大变化
    信号量 线程通信- Linux系统编程-(pthread)
    c语言中文件的定义
    8–9月,​Sui Move智能合约工作坊将在台北+线上举行
    LC2332H芯片的讲解
  • 原文地址:https://blog.csdn.net/FanZaiYo/article/details/133384547