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");
}