1.示例
//递归树形
private List<DeptVO> getDeptTree(List<DeptVO> childList, List<DeptVO> parentList) {
for (DeptVO p : parentList) {
List<DeptVO> twoLevelMenuTree = childList.stream()
.filter(s -> Objects.equals(s.getUpDeptId(), p.getDeptId()))
.collect(Collectors.toList());
if (twoLevelMenuTree.size() > 0) {
p.setChildren(twoLevelMenuTree);
getDeptTree(childList, twoLevelMenuTree);
}
}
return parentList;
}
2.递归参数解释
以上childList为所有数据的集合,parentList为最高节点的数据,这里是根据子节点的上级部门id等于父节点部门id做联系形成树形结构。
3.反向递归(取到子节点的所有子节点)
public void getChildrenByTree(DeptVO result, List<DeptVO> children, String deptId) {
if (result == null) {
return;
}
if (result.getDeptId().equals(deptId)) {
if (result.getChildren() != null) {
children.addAll(result.getChildren());
return;
}
}
if (result.getChildren() != null) {
for (DeptVO child : result.getChildren()) {
getChildrenByTree(child, children, deptId);
}
}
}
4.反向递归参数解释
result是顶级父节点的数据(包含所有子节点),children是一个用来接收子节点数据的集合,使用时创建一个空的list传进方法即可,deptId即为需要查找所有子节点的父节点的id。