public class TreeUtil {
public static List<DepartmentTreeReturnVm> toTree(List<DepartmentEntity> datas) {
//得到父节点
return datas.stream()
.filter(m -> !StringUtils.hasLength(m.getParentId()))
.map(m -> {
//可以根据需求赋予参数
DepartmentTreeReturnVm vo = new DepartmentTreeReturnVm(m.getId(), m.getName());
vo.setChildren(buildSubNodes(m, datas));
return vo;
}).collect(Collectors.toList());
}
private static List<DepartmentTreeReturnVm> buildSubNodes(DepartmentEntity root, List<DepartmentEntity> list) {
return list.stream()
.filter(m -> root.getId().equals(m.getParentId()))
.map(m -> {
//可以根据需求赋予参数
DepartmentTreeReturnVm vo = new DepartmentTreeReturnVm(m.getId(), m.getName());
vo.setChildren(buildSubNodes(m, list));
return vo;
}).collect(Collectors.toList());
}
}