代码语言:javascript复制
public List<Types> buildTree(List<Types> list) {
//父级(总的)
List<Types> typesList = new ArrayList<>();
list.forEach(t -> {
if (t.getParentId() == null || t.getParentId() == 0) {
typesList.add(nodeTree(t, list));
}
});
return typesList;
}
//types 父级的对象
public Types nodeTree(Types types, List<Types> list) {
List<Types> typesList = new ArrayList<>();
list.forEach(t -> {
if (t.getParentId() != null && types.getId().equals(t.getParentId())) {
typesList.add(nodeTree(t, list));
}
});
types.setChildren(typesList);
return types;
}