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