如何快速、且易懂編寫Java遞迴生成樹形選單結構

zhaozhangxiao發表於2021-06-18
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;
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章