java 生成树形结构_Java后台生成树状结构工具类

importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;public classTreeUtil {//private static String PID = "000000";

/*** 将平行的树,转化为一颗有层级关系的树

*@paramlist

*@parampId

*@return

*/

public static List getTreeList(Listlist,String pId){if (list == null){return null;

}//获取所有头节点

List rootNode = new ArrayList<>();for(TreeData treeData: list ){if(treeData.getId().equals(pId)){

rootNode.add(treeData);

}

}//头节点不存在的情况

if (rootNode.size() == 0){returngetChild(pId,list);

}//头节点存在的情况

for(TreeData treeData : rootNode){

String id=treeData.getId();

treeData.setChild(getChild(id,list));

}returnrootNode;

}private static List getChild(String id,Listlist){//找到id节点子节点

List childList = new ArrayList<>();for(TreeData treeData: list){if(treeData.getpId().equals(id)){

childList.add(treeData);

}

}//给子节点设置子节点

for(TreeData treeData : childList){

id=treeData.getId();//递归

treeData.setChild(getChild(id,list));

}if (childList.size() == 0){return null;

}returnchildList;

}/*** 获取指定层级的树

*@paramtreeList

*@paramlevel

*@return

*/

public static List getTreeNodeOnLevel(List treeList,intlevel){if (treeList == null && treeList.size() == 0){return new ArrayList<>();

}

ArrayList treeListOnLevel = new ArrayList<>();//调用递归方法

treeList = TreeUtil.recursionQueryTree(treeList, treeListOnLevel, 0,level);returntreeList;

}/*** 递归查所有指定层级的树节点

*@paramtreeList

*@paramtreeListOnLevel

*@paramcurrentLevel

*@paramlevel

*@return

*/

private static List recursionQueryTree(List treeList,List treeListOnLevel,int currentLevel,intlevel){

currentLevel++;//递归循环查询指定层级树

for(TreeData treeData : treeList){if (currentLevel ==level){

treeListOnLevel.add(treeData);continue;

}//递归

if (treeData.getChild() == null || treeData.getChild().size() == 0){continue;

}

recursionQueryTree(treeData.getChild(),treeListOnLevel,currentLevel,level);

}returntreeListOnLevel;

}/*** 统计一颗树的深度

*@paramtreeList

*@return

*/

public static int getTreeLevel(ListtreeList){if (treeList == null || treeList.size() == 0){return 0;

}//调用递归统计方法

return TreeUtil.recursionCount(treeList,0,0);

}private static int recursionCount(List treeList,int currentlevel,intmaxlevel){

currentlevel++;//当当前层级大于目前最大层级时覆盖

if (currentlevel >maxlevel){

maxlevel=currentlevel;

}//递归遍历节点

for(TreeData treeData : treeList){//如果子节点为空不进行递归

if (treeData.getChild() == null || treeData.getChild().size() == 0){continue;

}//递归

maxlevel =recursionCount(treeData.getChild(), currentlevel, maxlevel);

}returnmaxlevel;

}/*** 获取指定id在此树中的层级位置

*

*@paramlist 要遍历的树

*@paramid 要寻找的节点Id

*@paramrankCount 层级统计器,传入0即可

*@return

*/

public static Map getNode(Listlist,String id,Integer rankCount){

TreeData node= null;//计数器加一

rankCount++;

Map map = new HashMap<>();

map.put("node",node);

map.put("rankCount",rankCount);//如果传入的树为0或返回层级0

if (list == null || list.size() == 0){

map.put("rankCount",0);returnmap;

}//遍历树

for(TreeData treeData : list){if(treeData.getId().equals(id)){

map.put("node",treeData);returnmap;

}if (treeData.getChild() == null || treeData.getChild().size() == 0){continue;

}//递归

Map map1 =getNode(treeData.getChild(),id,rankCount);if (map1.get("node") != null){returnmap1;

}

}//如果传入的节点是不存在的,返回层级0

map.put("rankCount",0);returnmap;

}

}

你可能感兴趣的:(java,生成树形结构)