递归设置树节点

it2025-07-25  12

使用递归的方法设置节点 //所有的节点 private List<CatalogNode> buildChildrenList(List<CatalogNode> catalogList) { List<CatalogNode> nodeList = catalogList.stream().filter(catalogNode -> StringUtils.isBlank(catalogNode.getPid())) .map(catalogNode -> { catalogNode.setChildren(getChildren(catalogNode, catalogList)); return catalogNode; }) .collect(Collectors.toList()); return nodeList; } private List<CatalogNode> getChildren(CatalogNode root, List<CatalogNode> all) { List<CatalogNode> children = all.stream().filter(catalogNode -> { if(StringUtils.isNotBlank(catalogNode.getPid())) { catalogNode.setAttributes(new Attribute("seq", catalogNode.getCatalogSeq())); return catalogNode.getPid().equals(root.getId()); } return false; } ).map((menu) -> { menu.setChildren(getChildren(menu, all)); return menu; }).collect(Collectors.toList()); return children; }
最新回复(0)