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

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"
	}]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过递归实现将一个普通的数组转换成树形结构。具体实现步骤如下: 1. 创建一个空的树形结构对象,用于存储转换后的结果。 2. 遍历数组,对于每一个元素,判断它是否为根节点,即它的父节点是否为 null 或 undefined。如果是根节点,则将其作为树形结构的根节点,否则将其作为某个节点的子节点。 3. 如果当前元素不是根节点,则需要遍历树形结构,找到它应该作为子节点的节点。可以通过递归实现这一步骤,从根节点开始遍历整个树形结构,找到第一个满足条件的节点,将当前元素作为它的子节点。 4. 重复步骤 2 和 3,直到数组中的所有元素都被处理完毕。 以下是一个示例代码: ```javascript function arrayToTree(arr, parentId) { const tree = []; for (let i = 0; i < arr.length; i++) { const node = arr[i]; if (node.parentId === parentId) { const children = arrayToTree(arr, node.id); if (children.length > 0) { node.children = children; } tree.push(node); } } return tree; } ``` 在上面的代码中,`arr` 是需要转换的数组,`parentId` 是当前节点的父节点 ID。在遍历数组时,判断当前节点的 `parentId` 是否等于 `parentId`,如果是,则将其加入到树形结构中,并递归处理它的子节点。如果不是,则跳过该节点。 需要注意的是,上面的代码并没有对数组进行排序,如果需要按照某种顺序构建树形结构,需要对数组进行排序。另外,上面的代码只是一个简单的示例,实际应用中可能需要进行一些优化,例如使用哈希表存储节点,避免重复遍历等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值