JAVA 遞歸獲取組織樹


1、

/*建立樹形結構*/
public List<Department> builTree(){
List<Department> treeMenus =new ArrayList<Department>();
for(Department menuNode : getRootNode()) {
menuNode=buildChilTree(menuNode);
treeMenus.add(menuNode);
}
return treeMenus;
}

/*遞歸,建立子樹形結構*/
private Department buildChilTree(Department pNode){
List<Department> chilMenus =new ArrayList<Department>();
for(Department menuNode : menuList) {
if(menuNode.getParentId().equals(pNode.getId())) {
chilMenus.add(buildChilTree(menuNode));
}
}
pNode.setChildren(chilMenus);
return pNode;
}

/*獲取根節點*/
private List<Department> getRootNode() {
List<Department> rootMenuLists =new ArrayList<Department>();
for(Department menuNode : menuList) {
if(menuNode.getParentId().equals("1")) {
rootMenuLists.add(menuNode);
}
}
return rootMenuLists;
}
builTree();

2、

/*建立全部樹形結構*/
List<FuncConfig> buildChilTree(String parentId){
List<FuncConfig> list = funcConfigMapper.getFuncParentId(parentId);
for(FuncConfig funcConfigVo : list) {
String id = funcConfigVo.getId();
List<FuncConfig> child = buildChilTree(id);
if(!child.isEmpty()) {
funcConfigVo.setChildren(child);
}
}
return list;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM