工具类
public class TreeUtil{ /** * 解析树形数据 * @param topId * @param entityList * @return * @author lidong * @date 2018年8月16日 */ public staticextends TreeEntity > List getTreeList(Integer topId, List entityList) { List resultList=new ArrayList<>(); //获取顶层元素集合 Integer parentId; for (E entity : entityList) { parentId=entity.getParentId(); if(parentId==null||topId.equals(parentId)){ resultList.add(entity); } } //获取每个顶层元素的子数据集合 for (E entity : resultList) { entity.setChildren(getSubList(entity.getId(),entityList)); } return resultList; } /** * 获取子数据集合 * @param id * @param entityList * @return * @author jianda * @date 2017年5月29日 */ private static extends TreeEntity > List getSubList(Integer id, List entityList) { List childList=new ArrayList<>(); Integer parentId; //子集的直接子对象 for (E entity : entityList) { parentId=entity.getParentId(); if(id.equals(parentId)){ childList.add(entity); } } //子集的间接子对象 for (E entity : childList) { entity.setChildren(getSubList(entity.getId(), entityList)); } //递归退出条件 if(childList.size()==0){ return null; } return childList; } public static extends TreeEntity > List getTreeIdList(Integer currentId, List entityList) { List ids=new ArrayList (); List idByPidList = getIdByPidList(currentId,entityList); if (idByPidList!=null && idByPidList.size()>0) { for (E entity : idByPidList) { ids.add(entity.getId()); } } ids.add(currentId); ids=new ArrayList<>(new LinkedHashSet<>(ids)); return ids; } public static synchronized extends TreeEntity > List getIdByPidList(Integer currentId, List entityList) { List list = new ArrayList (); for (E entity : entityList) { if(currentId.equals(entity.getParentId())){ list.add(entity); } } List tempList = new ArrayList (); for(E entity : list ) { List idByPidList = getIdByPidList(entity.getId(),entityList); if(idByPidList!=null && idByPidList.size()>0) { tempList.addAll(idByPidList); } } list.addAll(tempList); tempList.clear(); if(list.size()==0){ return null; } return list; } }
TreeEntity<E>
public interface TreeEntity{ public Integer getId(); public Integer getParentId(); public void setChildren(List children); }
Entity
实现TreeEntity<E>接口即可
返回数据展示
{
"status": "S",
"message": null,
"value": [{
"id": 8,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "系统管理",
"table": null,
"parentId": 0,
"type": "整体模块",
"icon": "",
"url": "",
"sort": 1,
"parentName": null,
"children": [{
"id": 12,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "用户管理",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/user/index.html",
"sort": 1,
"parentName": null,
"children": null
}, {
"id": 13,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "角色管理",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/role/index.html",
"sort": 2,
"parentName": null,
"children": null
}, {
"id": 14,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "数据字典",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/dictionary/index.html",
"sort": 3,
"parentName": null,
"children": null
}, {
"id": 15,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "菜单管理",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/menu/index.html",
"sort": 4,
"parentName": null,
"children": null
}, {
"id": 16,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "部门管理",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/dept/index.html",
"sort": 5,
"parentName": null,
"children": null
}, {
"id": 17,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "地区管理",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/area/index.html",
"sort": 6,
"parentName": null,
"children": null
}, {
"id": 19,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "代码生成",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/generator/list.html",
"sort": 8,
"parentName": null,
"children": null
}, {
"id": 24,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "日志管理",
"table": null,
"parentId": 8,
"type": "子模块",
"icon": "",
"url": "/views/system/log/index.html",
"sort": 7,
"parentName": null,
"children": null
}]
}, {
"id": 9,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "测评管理",
"table": null,
"parentId": 0,
"type": "整体模块",
"icon": "icon-computer",
"url": "",
"sort": 1,
"parentName": null,
"children": [{
"id": 20,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "在线测评",
"table": null,
"parentId": 9,
"type": "子模块",
"icon": "",
"url": "/views/ceping/index.html",
"sort": 1,
"parentName": null,
"children": null
}, {
"id": 21,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "语音读取",
"table": null,
"parentId": 9,
"type": "子模块",
"icon": "",
"url": "/views/sound/index.html",
"sort": 2,
"parentName": null,
"children": null
}]
}, {
"id": 11,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "帮助中心",
"table": null,
"parentId": 0,
"type": "整体模块",
"icon": "icon-computer",
"url": "",
"sort": 1,
"parentName": null,
"children": [{
"id": 22,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "问题管理",
"table": null,
"parentId": 11,
"type": "子模块",
"icon": "",
"url": "/views/question/question/question.html",
"sort": 1,
"parentName": null,
"children": null
}, {
"id": 23,
"createTime": null,
"createUser": null,
"updateUser": null,
"updateTime": null,
"name": "账号问题",
"table": null,
"parentId": 11,
"type": "子模块",
"icon": "",
"url": "/helpcenter/list",
"sort": 2,
"parentName": null,
"children": null
}]
}]
}