zoukankan      html  css  js  c++  java
  • 线性的数据结构改成树形结构 java工具类

    工具类

    public class TreeUtil{
        /**
         * 解析树形数据
         * @param topId
         * @param entityList
         * @return
         * @author lidong
         * @date 2018年8月16日
         */
        public static <E extends TreeEntity<E>> List<E> getTreeList(Integer topId, List<E> entityList) {
            List<E> 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  <E extends TreeEntity<E>>  List<E> getSubList(Integer id, List<E> entityList) {
            List<E> 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  <E extends TreeEntity<E>>  List<Integer> getTreeIdList(Integer currentId, List<E> entityList) {
            List<Integer> ids=new ArrayList<Integer>();
            List<E> 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 <E extends TreeEntity<E>>  List<E> getIdByPidList(Integer currentId, List<E> entityList) {
            List<E> list = new ArrayList<E>();
            for (E entity : entityList) {
                if(currentId.equals(entity.getParentId())){
                    list.add(entity);
                }
            }
            
            List<E> tempList = new ArrayList<E>();
            for(E entity : list ) {
                List<E> 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<E> {
        public Integer getId();
        public Integer getParentId();
        public void setChildren(List<E> 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
            }]
        }]
    }
  • 相关阅读:
    深入理解yield from语法
    数据库事务并发问题,锁机制和对应的4种隔离级别
    同源策略与CORS跨域请求
    Restful 4 -- 认证组件、权限组件、频率组件、url注册器、响应器、分页器
    Restful 3 -- 序列化组件(GET/PUT/DELETE接口设计)、视图优化组件
    Restful 2 --DRF解析器,序列化组件使用(GET/POST接口设计)
    Restful 1 -- REST、DRF(View源码解读、APIView源码解读)及框架实现
    Vue(7)- vue-cookies、极验滑动验证geetest、vue-router的导航守卫
    Vue --6 router进阶、单页面应用(SPA)带来的问题
    Vue 5 -- axios、vuex
  • 原文地址:https://www.cnblogs.com/G-yong/p/9828825.html
Copyright © 2011-2022 走看看