java递归组装树结构

1:代码

/**
	 *
	 * @param treeList 排序好的树结构
	 * @param metaList	总数据
	 * @param temp	临时对象
	 */
	private void getTreeModelList(List<TreeModel> treeList,List<SysPermission> metaList,TreeModel temp) {
		for (SysPermission permission : metaList) {
			//获取父id
			String tempPid = permission.getParentId();
			//创建树结构对象
			TreeModel tree = new TreeModel(permission.getId(), tempPid, permission.getName(),permission.getRuleFlag(), permission.isLeaf());
			// 临时对象为空,父id为空,拿到一级菜单
			if(temp==null && oConvertUtils.isEmpty(tempPid)) {
				treeList.add(tree);
				//判断是否有子集,如果有则进行循环添加子集集合
				if(!tree.getIsLeaf()) {
					//这里有用就的是临时对象,给临时对象添加子集集合
					getTreeModelList(treeList, metaList, tree);
				}
			}else if(temp!=null && tempPid!=null && tempPid.equals(temp.getKey())){
				//这里首先获取临时对象,然后给临时对象添加子集集合
				temp.getChildren().add(tree);
				//判断当前对象是否有子集,如若有则进行循环添加子集集合
				if(!tree.getIsLeaf()) {
					//这里就是使用临时对象,给临时对象添加子集集合
					getTreeModelList(treeList, metaList, tree);
				}
			}
			
		}
	}

2:数据结构

[
  {
    "key": "9502685863ab87f0ad1134142788a385",
    "title": null,
    "slotTitle": "首页",
    "isLeaf": true,
    "icon": null,
    "ruleFlag": 0,
    "scopedSlots": {
      "title": "hasDatarule"
    },
    "children": null,
    "parentId": "",
    "label": null,
    "value": "9502685863ab87f0ad1134142788a385"
  },
  {
    "key": "d7d6e2e4e2934f2c9385a623fd98c6f3",
    "title": null,
    "slotTitle": "系统管理",
    "isLeaf": false,
    "icon": null,
    "ruleFlag": 0,
    "scopedSlots": {
      "title": "hasDatarule"
    },
    "children": [
      {
        "key": "3f915b2769fc80648e92d04e84ca059d",
        "title": null,
        "slotTitle": "用户管理",
        "isLeaf": false,
        "icon": null,
        "ruleFlag": 0,
        "scopedSlots": {
          "title": "hasDatarule"
        },
        "children": [
          {
            "key": "1260929666434318338",
            "title": null,
            "slotTitle": "用户编辑",
            "isLeaf": true,
            "icon": null,
            "ruleFlag": 0,
            "scopedSlots": {
              "title": "hasDatarule"
            },
            "children": null,
            "parentId": "3f915b2769fc80648e92d04e84ca059d",
            "label": null,
            "value": "1260929666434318338"
          }
        ],
        "parentId": "d7d6e2e4e2934f2c9385a623fd98c6f3",
        "label": null,
        "value": "3f915b2769fc80648e92d04e84ca059d"
      }
    ],
    "parentId": "",
    "label": null,
    "value": "d7d6e2e4e2934f2c9385a623fd98c6f3"
  }
]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值