java工具封装树形对象,常用于菜单 json树逆向生成list集合

package com.jiuding.adminuser.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jiuding.adminuser.vo.TPermissionVo;

import java.util.List;
import java.util.Map;

public class TreeToolUtils {
    /**
        TPermissionVo 是我的javaBen,请换成自己的那个树节点对象
        对象中的属性主要需要的是自己的主键:
        private String id;
        //父级的主键
        private String parentId;
        //装子集的list,因为表示自关联所以list对象也是自己
        private List<TPermissionVo> list;
        
    */
//根节点
private List<TPermissionVo> rootList; //根节点对象集合存放到这里 如:表中所有父节点都是0的    
    
    private List<TPermissionVo> bodyList; //其他节点存放到这里,可以包含根节点
    
    
    public TreeToolUtils(List<TPermissionVo> rootList, List<TPermissionVo> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    public TreeToolUtils() {
    }

    public List<TPermissionVo> getTree(){   //调用的方法入口
        if(bodyList != null && !bodyList.isEmpty()){
            //声明一个map,用来过滤已操作过的数据
            Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree,map));
            return rootList;
        }
        return null;
    }

    public void getChild(TPermissionVo TPermissionVo,Map<String,String> map){
        List<TPermissionVo> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c ->c.getParentId().equals(TPermissionVo.getId()))
                .forEach(c ->{
                    map.put(c.getId(),c.getParentId());
                    getChild(c,map);
                    childList.add(c);
                });
        TPermissionVo.setList(childList);

    }
}
//使用: 创建一个对象
//list是装所有父节点的
//list2是装所有子节点的
        TreeToolUtils utils =  new TreeToolUtils(list,list2);
        List<TPermissionVo> result =  utils.getTree();
//result 就是返回的 树集合

[
            {
                "id": "0d311a16aa71461dadfc62f0d27682be",
                "createBy": "10296c662e8a4628a6b5ac65a1609ba9",
                "createTime": "2019-10-08 17:11:32",
                "delFlag": null,
                "updateBy": null,
                "updateTime": null,
                "name": "关系分析",
                "parentId": "0",
                "sortOrder": 1,
                "module": 1,
                "style": "",
                "icon": "",
                "path": "",
                "status": 0,
                "url": "guanxifenxi",
                "list": [
                    {
                        "id": "c8e4be1cae654de6bd6ceb1225d6fcc0",
                        "createBy": "xxx",
                        "createTime": "2019-10-18 15:30:33",
                        "delFlag": null,
                        "updateBy": null,
                        "updateTime": null,
                        "name": "我是关系分析二级级菜单",
                        "parentId": "0d311a16aa71461dadfc62f0d27682be",
                        "sortOrder": 9999,
                        "module": 1,
                        "style": "",
                        "icon": "",
                        "path": "",
                        "status": 0,
                        "url": "/queryDepartmentsssss",
                        "list": [
                            {
                                "id": "49abcca609a6444898adbb5746ab27bf",
                                "createBy": "xxx",
                                "createTime": "2019-10-18 15:30:51",
                                "delFlag": null,
                                "updateBy": null,
                                "updateTime": null,
                                "name": "我是关系分析三级级菜单",
                                "parentId": "c8e4be1cae654de6bd6ceb1225d6fcc0",
                                "sortOrder": 9999,
                                "module": 1,
                                "style": "",
                                "icon": "",
                                "path": "",
                                "status": 0,
                                "url": "/queryDepartm",
                                "list": [
                                    {
                                        "id": "9287f6a6b15b490f9126e77563050269",
                                        "createBy": "xxx",
                                        "createTime": "2019-10-18 15:42:49",
                                        "delFlag": null,
                                        "updateBy": null,
                                        "updateTime": null,
                                        "name": "我是关系分析四级级菜单",
                                        "parentId": "49abcca609a6444898adbb5746ab27bf",
                                        "sortOrder": 9999,
                                        "module": 1,
                                        "style": "",
                                        "icon": "",
                                        "path": "",
                                        "status": 0,
                                        "url": "/queryDepartm4",
                                        "list": [],
                                        "bool": false
                                    }
                                ],
                                "bool": false
                            },
                            {
                                "id": "cd25ab0d7dc9460d8c761b3c3176169b",
                                "createBy": "xxx",
                                "createTime": "2019-10-18 15:31:12",
                                "delFlag": null,
                                "updateBy": null,
                                "updateTime": null,
                                "name": "我是关系分析三级级菜单2",
                                "parentId": "c8e4be1cae654de6bd6ceb1225d6fcc0",
                                "sortOrder": 9999,
                                "module": 1,
                                "style": "",
                                "icon": "",
                                "path": "",
                                "status": 0,
                                "url": "/queryDepartm2",
                                "list": [],
                                "bool": false
                            }
                        ],
                        "bool": false
                    }
                ],
                "bool": true
            },
            {
                "id": "20f9d7d9ca9b40ecb585dca8446d7417",
                "createBy": "10296c662e8a4628a6b5ac65a1609ba9",
                "createTime": "2019-10-08 17:12:22",
                "delFlag": null,
                "updateBy": null,
                "updateTime": null,
                "name": "联案分析",
                "parentId": "0",
                "sortOrder": 3,
                "module": 1,
                "style": "",
                "icon": "",
                "path": "",
                "status": 0,
                "url": "textr",
                "list": [],
                "bool": true
            }]

json树逆向生成list集合

//    装子节点集合
    private static List<JSONObject> childMenu=new ArrayList<JSONObject>();
    private List<JSONObject> adasd(JSONObject object,String pid){
        if (!object.isEmpty()){
            JSONObject data = object.getJSONObject("data");
            data.put("pid",pid);
            String id = data.getString("id");
            childMenu.add(data);
//            System.err.println("data的数据:"+data);
            JSONArray children = object.getJSONArray("children");
//            System.err.println("节点的数据:"+children);
            if (!children.isEmpty()&&children.size()>0){
                for (Object child : children){
                    adasd((JSONObject)child,id);
                }
            }
        }
        return childMenu;
    }

数据:

{
	"root": {
		"data": {
			"id": "c1lg9acr6wo0",
			"created": 1585295229212,
			"text": "中心主题"
		},
		"children": [{
			"data": {
				"id": "c1lh5vxprfk0",
				"created": 1585297783845,
				"text": "分支1主题"
			},
			"children": [{
				"data": {
					"id": "c1lh621gnuw0",
					"created": 1585297797132,
					"text": "分支1的分支1主题"
				},
				"children": [{
					"data": {
						"id": "c1lh66h48w00",
						"created": 1585297806786,
						"text": "分支1的分支1的分支1主题"
					},
					"children": []
				}]
			}, {
				"data": {
					"id": "c1lh6fzwdm80",
					"created": 1585297827513,
					"text": "分支1的分支2主题"
				},
				"children": []
			}]
		}, {
			"data": {
				"id": "c1lh6l0hzh40",
				"created": 1585297838433,
				"text": "分支3主题"
			},
			"children": [{
				"data": {
					"id": "c1lh712oe800",
					"created": 1585297873393,
					"text": "分支3的分支主题",
					"layout_right_offset": {
						"x": 38,
						"y": 22
					}
				},
				"children": []
			}]
		}, {
			"data": {
				"id": "c1lh6tu6qcg0",
				"created": 1585297857642,
				"text": "分支2主题"
			},
			"children": []
		}]
	},
	"template": "default",
	"theme": "fresh-blue",
	"version": "1.4.43"
}

 

使用 obj为上面的json字符串
       childMenu.clear();
        System.err.println(obj);
        List<JSONObject> adasd=new ArrayList<>();
        if (obj!=null){
            JSONObject root = obj.getJSONObject("root");
            //处理数据
             adasd = adasd(root,"root");
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值