java数组转树形结构递归实现

文章提供了一段Java代码,用于将包含部门信息的数组列表(如资源概览,资源目录等)根据父编码superCode递归转换成树形结构。该过程首先找到顶层元素,然后获取其子数据集合,通过递归方式构建出具有层级关系的部门树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.数组列表

{
    "data": [
        {
            "deptName": "资源概览",
            "contain": "0",
            "deptLevel": "1",
            "deptId": "5862",
            "superCode": null,
            "parentDeptName": null,
            "deptCode": "KS419"
        },
        {
            "deptName": "资源目录",
            "contain": "0",
            "deptLevel": "2",
            "deptId": "5861",
            "superCode": "KS419",
            "parentDeptName": "资源概览",
            "deptCode": "KS418"
        },
        {
            "deptName": "资源大屏",
            "contain": "0",
            "deptLevel": "2",
            "deptId": "5862",
            "superCode": "KS419",
            "parentDeptName": "资源概览",
            "deptCode": "KS417"
        },
        {
            "deptName": "监控大屏",
            "contain": "0",
            "deptLevel": "1",
            "deptId": "5864",
            "superCode": null,
            "parentDeptName": null,
            "deptCode": "KS416"
        }
    ]
}

2.根据父编码superCode递归转树形结构

    public List<DeptInfoVo> getRoleMenuTree(List<DeptInfoVo> deptList) {
        List<DeptInfoVo> resultList = new ArrayList<>();
        //获取顶层元素集合
        for (DeptInfoVo permission : deptList) {
            String parentId = permission.getSuperCode();
            //顶层元素的id为null或者为0
            if (StringUtils.isBlank(parentId)) {
                DeptInfoVo superDept=new DeptInfoVo();
                superDept.setDeptId(permission.getDeptId());
                superDept.setDeptCode(permission.getDeptCode());
                superDept.setDeptName(permission.getDeptName());
                superDept.setDeptLevel(permission.getDeptLevel());
                superDept.setSuperCode(parentId);
                resultList.add(superDept);
            }
        }
        //获取每个顶层元素的子数据集合
        for (DeptInfoVo entity : resultList) {
            entity.setChild(getPowerSubList(entity.getDeptCode(), deptList));
        }
        return resultList;
    }

    /**
     * 获取子数据集合
     *
     * @param code
     * @param deptList
     * @return
     */
    private List<DeptInfoVo> getPowerSubList(String code, List<DeptInfoVo> deptList) {
        List<DeptInfoVo> childList = new ArrayList<>();
        //子集的直接子对象
        for (DeptInfoVo entity : deptList) {
            String parentId = entity.getSuperCode();
            if (code.equals(parentId)) {
                DeptInfoVo vo = new DeptInfoVo();
                vo.setDeptId(entity.getDeptId());
                vo.setDeptCode(entity.getDeptCode());
                vo.setDeptName(entity.getDeptName());
                vo.setDeptLevel(entity.getDeptLevel());
                vo.setSuperCode(parentId);
            }
        }
        //子集的间接子对象
        for (DeptInfoVo entity : childList) {
            entity.setChild(getPowerSubList(entity.getDeptCode(), deptList));
        }
        //递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

3.返回结果

{
	"data": [{
		"deptName": "资源概览",
		"contain": "0",
		"deptLevel": "1",
		"deptId": "5862",
		"superCode": null,
		"parentDeptName": null,
		"deptCode": "KS419",
		"child": [{
				"deptName": "资源目录",
				"contain": "0",
				"deptLevel": "2",
				"deptId": "5861",
				"superCode": "KS419",
				"parentDeptName": "资源概览",
				"deptCode": "KS418"
			},
			{
				"deptName": "资源大屏",
				"contain": "0",
				"deptLevel": "2",
				"deptId": "5862",
				"superCode": "KS419",
				"parentDeptName": "资源概览",
				"deptCode": "KS417"
			}
		]
	}, {
		"deptName": "患者视图",
		"contain": "0",
		"deptLevel": "1",
		"deptId": "5864",
		"superCode": null,
		"parentDeptName": null,
		"deptCode": "KS416"
	}]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值