JAVA 递归获取组织树

Posted on 2020-12-15 14:46  jiaoqing。  阅读(1834)  评论(0)    收藏  举报

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;
}

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3