先看效果
工具类代码: public static List<CmsCategoryTree> buildDeptTree(List<CmsCategoryTree> nodes) { if (nodes == null) { return null; } List<CmsCategoryTree> result = new ArrayList<>(); nodes.forEach(children -> { String pid = children.getParentId(); if (pid == null || "0".equals(pid)) { result.add(children); return; } for (CmsCategoryTree n : nodes) { String id = n.getId(); if (id != null && id.equals(pid)) { if (n.getChildren() == null) n.initChildren(); n.getChildren().add(children); children.setHasParent(true); n.setHasChild(true); return; } } }); return result; }
CmsCategoryTree 代码:
@Data
public class CmsCategoryTree {
/**id*/
private String id;
/**栏目模块*/
private String module;
/**栏目名称*/
private String name;
/**父级编号*/
private String parentId;
/**备注信息*/
private String remarks;
private List<CmsCategoryTree> children;
private boolean hasParent = false;
private boolean hasChild = false;
public void initChildren(){
this.children = new ArrayList<>();
}
}