線性的數據結構改爲樹形結構 java工具類

工具類html

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>接口便可url

 

返回數據展現spa

{
    "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
        }]
    }]
}
相關文章
相關標籤/搜索