java stream 常用-项目中

    Map<String, Department> departMap = departList.stream().collect(Collectors.toMap(t -> t.getUuid(), t -> t, (n, o) -> o));
    ```

这段代码可以通过递归来优化,在itsm.setChildrenList(childrenList)之前,调用一个递归函数,将childrenList中的每个元素都处理一遍,为其添加子集。以下是示例代码:

for (Map.Entry<String, List<ItsmTypeReportResultVo>> entry : itsmTypeMapList.entrySet()) {
    String type = entry.getKey();
    List<ItsmTypeReportResultVo> value = entry.getValue();
    ItsmTypeReportWorkFlowListDataTempVo itsm = new ItsmTypeReportWorkFlowListDataTempVo();
    itsm.setItsmType(type);
    Map<String, Integer> departList = getStringIntegerMap(departListHead, value);
    itsm.setDepartList(departList);
    Map<String, List<ItsmTypeReportResultVo>> formTypeMap = value.stream().collect(Collectors.groupingBy(ItsmTypeReportResultVo::getFormName));
    if (formTypeMap == null) {
        continue;
    }
    List<ItsmTypeReportWorkFlowListDataTempVo> childrenList = new ArrayList<>();
    for (Map.Entry<String, List<ItsmTypeReportResultVo>> formEntry : formTypeMap.entrySet()) {
        String childType = formEntry.getKey();
        List<ItsmTypeReportResultVo> childValue = formEntry.getValue();
        ItsmTypeReportWorkFlowListDataTempVo childItsm = new ItsmTypeReportWorkFlowListDataTempVo();
        childItsm.setFormType(childType);
        Map<String, Integer> cccc = getStringIntegerMap(departListHead, childValue);
        childItsm.setDepartList(cccc);
        childrenList.add(childItsm);
    }
    recursivelyAddChildren(itsm, childrenList);
    w.add(itsm);
}

private void recursivelyAddChildren(ItsmTypeReportWorkFlowListDataTempVo parent, List<ItsmTypeReportWorkFlowListDataTempVo> children) {
    for (ItsmTypeReportWorkFlowListDataTempVo child : children) {
        Map<String, List<ItsmTypeReportResultVo>> childFormTypeMap = child.getValue().stream().collect(Collectors.groupingBy(ItsmTypeReportResultVo::getFormName));
        if (childFormTypeMap == null) {
            continue;
        }
        List<ItsmTypeReportWorkFlowListDataTempVo> subChildrenList = new ArrayList<>();
        for (Map.Entry<String, List<ItsmTypeReportResultVo>> childFormEntry : childFormTypeMap.entrySet()) {
            String childFormType = childFormEntry.getKey();
            List<ItsmTypeReportResultVo> childFormValue = childFormEntry.getValue();
            ItsmTypeReportWorkFlowListDataTempVo childItsm = new ItsmTypeReportWorkFlowListDataTempVo();
            childItsm.setFormType(childFormType);
            Map<String, Integer> childDepartList = getStringIntegerMap(departListHead, childFormValue);
            childItsm.setDepartList(childDepartList);
            subChildrenList.add(childItsm);
        }
        child.setChildrenList(subChildrenList);
        if (!subChildrenList.isEmpty()) {
            recursivelyAddChildren(child, subChildrenList);
        }
    }
}


使用递归优化,可以使代码结构更加清晰,易于维护。
本作品采用《CC 协议》,转载必须注明作者和本文链接
MissYou123
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!