java 遍历组装树形

/**
 * @title: GroupNodeTree
 * @Author: 相思子
 * @Date: 2021/8/10 12:04
 * @Description:组装树形
 */
@Component
public class GroupNodeTree {

    private List<SummarySchemeGroupNode> groupNodeList;

    public GroupNodeTree(List<SummarySchemeGroupNode> groupNodeList) {
        this.groupNodeList=groupNodeList;
    }

    //建立树形结构
    public List<SummarySchemeGroupNode> buildTree(){
        List<SummarySchemeGroupNode> treeGroups = new ArrayList<>();
        for(SummarySchemeGroupNode groupNode : getRootNode()) {
            groupNode=buildChildTree(groupNode);
            treeGroups.add(groupNode);
        }
        return treeGroups;
    }

    //递归,建立子树形结构
    private SummarySchemeGroupNode buildChildTree(SummarySchemeGroupNode pNode){
        List<SummarySchemeGroupNode> chilNodes = new ArrayList<>();
        for(SummarySchemeGroupNode groupNode : groupNodeList) {
            if(groupNode.getParentId() != null && groupNode.getParentId().equals(pNode.getKey())) {
                chilNodes.add(buildChildTree(groupNode));
            }
        }
        pNode.setChildren(chilNodes);
        return pNode;
    }

    //获取根节点
    private List<SummarySchemeGroupNode> getRootNode() {
        List<SummarySchemeGroupNode> rootGroupLists = groupNodeList.stream().filter(node -> StringUtils.isEmpty(node.getParentId())).sorted(Comparator.comparing(SummarySchemeGroupNode::getOrder)).collect(Collectors.toList());
        return rootGroupLists;
    }

    /**
     * 获取某个父节点下面的所有子节点
     * @param nodeList 所有节点
     * @param pid 父级节点
     * @param set 汇总集合
     * @return List<String>
     */
    public List<String> treeChildAndSelfList(List<SummarySchemeGroupNode> nodeList, String pid, Set<String> set) {
        for (SummarySchemeGroupNode node : nodeList) {
            //遍历出父id等于参数的id,add进子节点集合
            if (node.getParentId() !=null && node.getParentId().equals(pid)) {
                //递归遍历下一级
                treeChildAndSelfList(nodeList, node.getKey(), set);
                set.add(node.getKey());
            }
        }
        return new ArrayList<>(set);
    }
}

@ApiModel(description = "汇总方案分组节点")
public class SummarySchemeGroupNode {

    /**
     * 节点id
     */
    @ApiModelProperty(value = "节点id")
    private String key;
    /**
     * 标题
     */
    @ApiModelProperty(value = "标题")
    private String title;
    /**
     * 排序
     */
    @ApiModelProperty(value = "排序")
    private String order;
    /**
     * 父节点
     */
    @ApiModelProperty(value = "父节点id")
    private String parentId;
    /**
     * 子节点
     */
    @ApiModelProperty(value = "子节点")
    private List<SummarySchemeGroupNode> children = new ArrayList<>();
    /**
     * 是否叶子结点
     */
    @ApiModelProperty(value = "是否叶子结点")
    private boolean isLeaf;

    /**
     * 任务key
     */
    @ApiModelProperty(value = "taskKey")
    private String taskKey;
}
@GetMapping("/query/tree/schemes")
    @ApiOperation(value = "查询汇总方案树形")
    public List<SummarySchemeGroupNode> querySchemesTree() throws Exception {
        LogHelper.info("数据汇总模块","查询汇总方案树形","查询汇总方案树形");
        List<SummarySchemeGroupNode> tree = new ArrayList<>();
        try {
            List<SummarySchemeGroupNode> summarySchemeGroupNodes = groupService.queryAllGroupNode();
            List<SummarySchemeGroupNode> schemeGroupNodes = schemeService.queryAllScheme();
            if(schemeGroupNodes != null){
                summarySchemeGroupNodes.addAll(schemeGroupNodes);
            }
            if (summarySchemeGroupNodes != null) {
                GroupNodeTree groupNodeTree =new GroupNodeTree(summarySchemeGroupNodes);
                tree=groupNodeTree.buildTree();
            }
            return tree;
        } catch (Exception e) {
            logger.error("初始化汇总方案树形失败",e);
            throw new JQException(SummarySchemeErrorEnum.SUMMARYSCHEME_EXCEPTION_003,"初始化汇总方案树形失败");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值