将一个平级的集合组装为一个有层级的集合
private List<Department> getDeptTree(List<Department> currEntDepts, Department department{
List<Department> children = new ArrayList<>();
for (Department dept : currEntDepts) {
if (ObjectUtil.equals(dept.getParentId(), department.getId())) {
children.add(dept);
dept.setChildren(getDeptTree(currEntDepts, dept));
}
}
department.setChildren(children);
return children;
}
获取有层级集合的某个字段数据
public static void getChildrens(List<Department> list, List<Integer> ids) {
if (list != null) {
for (Department sysDept : list) {
ids.add(sysDept.getId());
List<Department> children = sysDept.getChildren();
getChildrens(children, ids);
}
}
}
实体类
@Data
@EqualsAndHashCode(callSuper = false)
public class Department {
private Integer id;
/** 父级id*/
private Integer parentId;
private List<Department> children;
}