List转Tree,允许多棵树同时存在

结果展示

   {
            "id": "1564",
            "parentId": "0",
            "name": "tw",
            "sort": 0
        },
        {
            "id": "1550",
            "parentId": "1547",
            "name": "te",
            "sort": 0,
            "children": [
                {
                    "id": "1615",
                    "parentId": "1550",
                    "name": "qwa1",
                    "sort": 0
                }
            ]
        },
        {
            "id": "1548",
            "parentId": "0",
            "name": "terr",
            "sort": 0,
            "children": [
                {
                    "id": "1549",
                    "parentId": "1548",
                    "name": "ter",
                    "sort": 0
                }
            ]
        }

原始数据

在这里插入图片描述

代码

  • VO
@Data
public class Info implements Serializable {
    
    private Integer id;

    /** 公司id */
    private Integer companyId;
    
    /** 父节点id */
    private Integer parentId;
    
    /** 排序 */
    private Integer sort;

    /** 创建人 */
    private Integer createUserId;
    
    @Transient
    private List<Info> children;
}
  • Utils

    /**
     * 树结构遍历
     *
     * @param treeList
     * @return
     */
    public static List<Tree<String>> buildTree(List<Info> treeList) {
        List<Info> treeNodes = treeList;
        List<Info> trees = new ArrayList<Info>();
        if (!CollectionUtils.isEmpty(treeNodes)) {
            for (int i = 0; i < treeNodes.size(); i++) {
                Info boi = getChildren(treeNodes.get(i), treeList);
                if (boi != null) {
                    //不空是顶级节点
                    trees.add(boi);
                }
            }
        }
        trees.sort(Comparator.comparing(Info::getSort));
        // 封装 数据
        return packCaseObjectIno( trees, new LinkedList<>() );
    }

    /**
     * 判断此节点是否为顶级节点
     *
     * @param treeNode
     * @param treeNodes
     * @return
     */
    public static Info getChildren(Info treeNode, List<Info> treeNodes) {
        boolean isParent = true;
        if (treeNode.getParentId() != 0) {
            for (Info it : treeNodes) {
                if (treeNode.getParentId().equals(it.getId())) {
                    isParent = false;
                    break;
                }
            }
        }
        if (isParent) {
            return findChildren(treeNode, treeNodes);
        }
        return null;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static Info findChildren(Info treeNode, List<Info> treeNodes) {
        treeNode.setChildren(new ArrayList<Info>());
        for (Info it : treeNodes) {
            if (treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<Info>());
                }
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        if (treeNode.getChildren() != null && treeNode.getChildren().size()>0) {
            treeNode.getChildren().sort(Comparator.comparing(Info::getSort));
        }
        return treeNode;
    }


    public static List<Tree<String>> packCaseObjectIno(List<Info> list,List<Tree<String>> tree ){
        for (Info info:list) {
            List<Tree<String>> tr = new LinkedList<>();
            Tree<String> tre = new Tree<>();
            tre.setId(info.getId().toString());
            tre.setParentId(info.getParentId().toString());
            tre.put("sort", info.getSort());
            tre.put("name", info.getName());
            tre.put("createUserId", info.getCreateUserId());
            if(CollectionUtil.isNotEmpty(info.getChildren())){
                tre.setChildren(packCaseObjectIno(info.getChildren(),tr));
            }
            tree.add(tre);
        }
        return tree;
    }

原计划是按照 cn.hutool.core.lang.tree.TreeUtil;的方式封装一个,试了一下没完成,暂先实现功能,待后优化。
-----2022/08/04

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值