java8中过滤、获取指定属性转换为集合、字符串拼接、移除某个对象的使用

@Override
    public List<TaskApproveVo> taskApprove(TaskApproveDTO taskApproveDTO) {
        List<TaskApproveVo> voList = approveLogMapper.getTaskApprove(taskApproveDTO);
        // 将名称与部门名称拼接
        voList.forEach(taskApproveVo -> {
            String userName = taskApproveVo.getUserName();
            String orgName = taskApproveVo.getOrgName();
            taskApproveVo.setUserNameAndOrgName(userName + "-" + orgName);
        });

        // 如果当前节点有多个代办的话  将同一级别的代办展示为一条 并将名字拼接
        List<TaskApproveVo> list = voList.stream().filter(a -> a.getStatus() == 0).collect(Collectors.toList()); // 过滤 获取同一节点所有代办
        if (list.size() > 1){
            List<String> newList = list.stream().map(TaskApproveVo::getUserNameAndOrgName).collect(Collectors.toList()); // 获取指定属性转换为集合 获取所有的userNameAndOrgName 转化为集合
            String names = newList.stream().collect(Collectors.joining(",")); // 逗号拼接字符串

            // 将原始集合中的代办剔除 创建一个新的代办对象
            voList.removeIf(vo -> vo.getStatus() == 0);

            TaskApproveVo taskApproveVo = new TaskApproveVo();
            taskApproveVo.setStatus(0);
            taskApproveVo.setBillId(voList.get(0).getBillId());
            taskApproveVo.setInstanceId(voList.get(0).getInstanceId());
            taskApproveVo.setUserNameAndOrgName(names);
            voList.add(taskApproveVo);
        }

        return voList;
    }

过滤

List<TaskApproveVo> list = voList.stream().filter(a -> a.getStatus() == 0).collect(Collectors.toList());

获取指定属性转换为集合

List<String> newList = list.stream().map(TaskApproveVo::getUserNameAndOrgName).collect(Collectors.toList());

逗号拼接字符串

String names = newList.stream().collect(Collectors.joining(","));

根据条件移除集合中的某个对象

voList.removeIf(vo -> vo.getStatus() == 0);

通过某个字段对集合中的数据进行去重

List<WorkflowCirculation> circulationList = workflowCirculationMapper.queryListByPressesKeyAndBindVersion("Process_1646718515566", "version1");

List<WorkflowCirculation> list = circulationList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
       new TreeSet<>(Comparator.comparing(WorkflowCirculation::getName))
), ArrayList::new));

你可能感兴趣的:(java,list,spring,boot,stream)