• 【Stream】简易笔记


    一、常用函数

    map

    • 处理流元素对象,然后会返回处理完的对象
    // bean的属性值复制到目标bean中
    .map(x -> {
         SysUserAndUserServiceVO vo = new SysUserAndUserServiceVO();
         BeanUtils.copyProperties(x, vo);
         return vo;
    })
    
    // 获取Bean里面的属性值
    .map(x -> x.name) 
    
    String experimenterNames = sysUsers.stream().map(SysUser::getNickName).collect(Collectors.joining("、"));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    peek

    .map(x -> {
    	x.setChildren(null);
        return x;
    })
    
    
    // 等价于
    .peek((x) -> x.setChildren(null))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    collect

    • 流转换成集合和聚合元素
    // 将流转化为List
    .collect(Collectors.toList());
    
    • 1
    • 2

    forEach

    • 对流进行遍历操作
    // 对流进行遍历输出
    .forEach(x -> {
    	System.out.println(x)
    });
    
    • 1
    • 2
    • 3
    • 4

    filter

    // 过滤符合条件的流
    .filter(x -> x.getParentId() == 0)
    
    • 1
    • 2

    Reduce

    public class Test {
        public static void main(String[] args) {
            List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);
            // 求和方式1
            Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
            // 求和方式2
            Optional<Integer> sum2 = list.stream().reduce(Integer::sum);
            // 求和方式3
            Integer sum3 = list.stream().reduce(0, Integer::sum);
            // 求乘积
            Optional<Integer> product = list.stream().reduce((x, y) -> x * y);
    
            // 求最大值方式1
            Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y);
            // 求最大值写法2
            Integer max2 = list.stream().reduce(1, Integer::max);
    
            System.out.println("list求和:" + sum.get() + "," + sum2.get() + "," + sum3);
            System.out.println("list求积:" + product.get());
            System.out.println("list求和:" + max.get() + "," + max2);
    
            /**
             * @Result:
             * list求和:29,29,29
             * list求积:2112
             * list求和:11,11
             * 
             */
        }
    }
    
    public class MsAssessItem {
        private Integer itemNconfig;
    
        public Integer getItemNconfig() {
            return itemNconfig;
        }
    
        public void setItemNconfig(Integer itemNconfig) {
            this.itemNconfig = itemNconfig;
        }
    
        public static void main(String[] args) {
    
            List<MsAssessItem> list = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                MsAssessItem msAssessItem = new MsAssessItem();
                msAssessItem.setItemNconfig(5);
                list.add(msAssessItem);
            }
            // 0 + 5 + 5 + 5 + 5 + 5
            int sum = list.stream().map(MsAssessItem::getItemNconfig).reduce(0, Integer::sum, Integer::sum);
            System.out.println(sum);
    
            // 5 + 5 + 5 + 5 + 5 + 5
            System.out.println(list.stream().map(MsAssessItem::getItemNconfig).reduce(5, Integer::sum, Integer::sum));
    
     		List<MsAssessItem> list1 = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                MsAssessItem msAssessItem = new MsAssessItem();
    
                msAssessItem.setItemNconfig(i % 2 == 0 ? null : 5);
                list1.add(msAssessItem);
            }
            // 0 + null + null  + 5 + null  + 5
            int sum = list1.stream().filter(x -> x.getItemNconfig() != null).map(MsAssessItem::getItemNconfig).reduce(0, Integer::sum, Integer::sum);
            System.out.println(sum);
            // 5 + null  + null  + 5 + null  + 5
            System.out.println(list1.stream().filter(x -> x.getItemNconfig() != null).map(MsAssessItem::getItemNconfig).reduce(5, Integer::sum, Integer::sum));
        
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    二、实战

    1. 满足某一条件的组装

    Q:listA里面包含了所有用户集合,listB里面包含了用户所拥有的主机数量集合。a.userId =b.userId
    listA:
    表A:
    selectUserList()
    在这里插入图片描述
    listB:
    表B:
    在这里插入图片描述
    统计用户服务列表里面业务用户的服务数量:selectUserServiceNum()
    求listC
    (有没有大佬可以用sql语句来实现结果listC的结果)
    在这里插入图片描述

    stream流

        public TableDataInfo listA() {
            // 当前业务客户listA
            List<SysUserAndUserServiceVO> list = userService.selectUserList(user).stream()
                    .map(x -> {
                        SysUserAndUserServiceVO vo = new SysUserAndUserServiceVO();
                        BeanUtils.copyProperties(x, vo);
                        return vo;
                    })
                    .collect(Collectors.toList());
            // 统计用户服务列表里面业务用户的服务数量:listB
            List<SysUserAndUserServiceVO> listB = userService.selectUserServiceNum(sysUserAndUserServiceVO);
            // 当前业务客户list 与 服务数量的匹配
            listA.forEach(
                    x -> {
                        List<SysUserAndUserServiceVO> collect = listB.stream()
                        		// 当 a.userId =b.userId 的时候
                                .filter(userServiceNum -> userServiceNum.getUserId().equals(x.getUserId()))
                                .collect(Collectors.toList());
                        if (CollectionUtils.isEmpty(collect)){
                            x.setServiceNum(0L);
                        }else {
                            x.setServiceNum(collect.get(0).getServiceNum());
                        }
                    }
            );
            return getDataTable(list);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2. 树级集合的组装

    数据库:暂定树的高度不超过3.
    在这里插入图片描述

    效果图:
    在这里插入图片描述
    用for循环

        public List<AiBusinessTypeVO> selectAiBusinessTypeTree(AiBusinessType aiBusinessType) {
            // 查询所有的数据
            aiBusinessType.setParentId(0L);
            aiBusinessType.setDelFlag(DelFlag.OK.getCode());
            
            List<AiBusinessTypeVO> oneList = aiBusinessTypeService.selectAiBusinessTypeListVO(aiBusinessType);
            for (AiBusinessTypeVO businessType : oneList) {
                AiBusinessType tempBusinessType = new AiBusinessType();
                List<AiBusinessTypeVO> twoList = aiBusinessTypeService.selectAiBusinessTypeListVO(tempBusinessType);
                businessType.setAiBusinessTypes(twoList);
                for (AiBusinessTypeVO type : twoList) {
                    tempBusinessType = new AiBusinessType();
                    List<AiBusinessTypeVO> threeList = aiBusinessTypeService.selectAiBusinessTypeListVO(tempBusinessType);
                    type.setAiBusinessTypes(threeList);
                }
            }
            return getDataTable(oneList);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    stream流

    查询树级结构的代码

        public List<AiBusinessTypeVO> selectAiBusinessTypeTree(AiBusinessType aiBusinessType) {
            // 查询出所有的数据集合allList 
            List<AiBusinessTypeVO> allList = aiBusinessTypeMapper.selectAiBusinessTypeListVO(aiBusinessType);
            // 新建一个最终返回的集合 list 
            List<AiBusinessTypeVO> list = new ArrayList<>();
            allList.stream()
            		// 返回第一层的集合
                    .filter(x -> x.getParentId() == 0)
                    // 遍历第一层集合
                    .forEach(x -> {
                    	// 把遍历出来的第一层:x 放到最终结果集合:list 里面
                        list.add(x);
                        // 遍历的第一层:x, 所有得数据集合
                        getAiBusinessTypeChildren(x, allList);
                    });
            return list;
        }
        
        /**
         * 获取当前层数的子结构数据
         *
         * @param aiBusinessType 第N层数据
         * @param allList 所有的数据合集
         */
        private void getAiBusinessTypeChildren(AiBusinessTypeVO aiBusinessType, List<AiBusinessTypeVO> allList) {
            // 新建空的子数据集合:children 
            List<AiBusinessTypeVO> children = new ArrayList<>();
            // 设置第 N 层的子数据:children 
            aiBusinessType.setChildren(children);
            // 遍历所有的数据集合
            allList.stream()
            		// 过滤出第 N+1 层数据
                    .filter(x -> x.getParentId().equals(aiBusinessType.getId()))
                    // 遍历第 N+1 层数据
                    .forEach(x -> {
                        //空的子数据集合:children,存放第 N+1 层数据
                        children.add(x);
                        // 继续获取 N+1 层子数据结构
                        getAiBusinessTypeChildren(x,allList);
                    });
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    返回的 List

    @Data
    public class AiBusinessTypeVO {
        /**
         * id
         */
        private Long id;
        
        /**
         * 场景分类名称
         */
        private String businessTypeName;
        
        /**
         * 子级数据
         */
        private List<AiBusinessTypeVO> children;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    三、参考链接

    Stream流常用方法与应用案例大全

    随笔

     Set<String> strings = list.stream().map(StorehouseGermplasmRef::getGermplasmStorehouseCode).collect(Collectors.toSet());
    String result = String.join(", ", strings);
    
    • 1
    • 2
  • 相关阅读:
    ts3.接口和对象类型
    Triple-shapelet Networks for Time SeriesClassification(ICDM2020)
    旅游卡免费旅游的使用条件有哪些?
    传输层中的TCP和UPD协议
    【R语言数据科学】(十七):常见机器学习算法(附代码实现)
    ARM虚拟机安装OMV
    vue3修改默认eslint配置,格式化代码
    PAL/NTSC/1080I和interlaced scan(隔行扫描)
    开发者也能“搞艺术”?聚合数据联合科大讯飞助力开发者创新不断
    48_ue4进阶末日生存游戏开发[本地存储游戏数据]
  • 原文地址:https://blog.csdn.net/qq_43718048/article/details/126646435