如何快速、且易懂编写Java递归生成树形菜单结构

Java
346
0
0
2022-04-27
pojo:
@Data
public class CategoryTreeVO {
  private Integer id;
  private String name;
  private Integer type;
  private Integer fatherId;
  private List<CategoryTreeVO> nodeCategoryList;
}
@Override
public List<CategoryTreeVO> treeList() {
  List<CategoryTreeVO> treeList = categoryMapperCustom.treeList();
   方式2 
  List<CategoryTreeVO> categoryTreeVOS = buildTree100(treeList, 0);
  return categoryTreeVOS;
  }

 public List<CategoryTreeVO> buildTree100(List<CategoryTreeVO> list, Integer pid) {
        List<CategoryTreeVO> trees = new ArrayList<CategoryTreeVO>();
        for (CategoryTreeVO categoryTree : list
        ) {
            if (categoryTree.getFatherId().equals(pid)) {
                List<CategoryTreeVO> nodeList = buildTree100(list, categoryTree.getId());
                categoryTree.setNodeCategoryList(nodeList);
                trees.add(categoryTree);
            }
        }
        return trees;
    }