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

Java stream处理递归

更新完整代码

private void queryLevelCategory(List<KnowledgeCategory> categoryList){
   
		// 可以是顶级thetopuuid0000000000000000000000 或者某个分类查子集
		List<KnowledgeCategory> knowledgeCategories = this.deepCategory("thetopuuid0000000000000000000000", categoryList);
		String s = JsonUtil.toJson(knowledgeCategories);
		System.out.println(s);
	}

	private List<KnowledgeCategory> deepCategory(String uuid,List<KnowledgeCategory> categoryList){
   
		List<KnowledgeCategory>	result = new ArrayList<>();
		categoryList.forEach(x->{
   
			if (uuid.equals(x.getParentUuid())) {
   
				List<KnowledgeCategory> childRenList = this.deepCategory(x.getUuid(), categoryList);
				x.setChildRen(childRenList);
				result.add(x);
			}
		});
		return result;
	}

文件上传中...

响应json数据
[
  {
   
    "uuid": "category000000000000000000000009",
    "categoryName": "知识管理默认一级分类(2)",
    "parentUuid": "thetopuuid0000000000000000000000",
    "describeInfo": "知识管理默认一级分类",
    "lever": 1,
    "childRen": [
      {
   
        "uuid": "category000000000000000000000010",
        "categoryName": "知识管理默认二级分类(1)",
        "parentUuid": "category000000000000000000000009",
        "describeInfo": "知识管理默认二级分类",
        "lever": 2,
        "childRen": []
      }
    ]
  },
  {
   
    "uuid": "z4440a5127f0492ab3eaeb1a4a98406f",
    "categoryName": "1233(0)",
    "parentUuid": "thetopuuid0000000000000000000000",
    "describeInfo": "",
    "lever": 1,
    "childRen": [
      {
   
        "uuid": "d5b4dc296a5e47c8bb546fe1ac3a5446",
        "categoryName": "3无123213(0)",
        "parentUuid": "z4440a5127f0492ab3eaeb1a4a98406f",
        "describeInfo": "",
        "lever": 2,
        "childRen": [
          {
   
            "uuid": "i7f477bfbf3f44a797efeae830212320",
            "categoryName": "tttt(0)",
            "parentUuid": "d5b4dc296a5e47c8bb546fe1ac3a5446"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值