Java构建树 工具类

使用hutool工具 

使用场景:菜单构建、目录构建

1.添加maven依赖

        <dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.8.26</version>
		</dependency>

2.测试

        a.默认方式

        b.自定义字段,适配不同的数据模型


public class TreeTest {
    public static void main(String[] args) {
       testTree();
        customTree();
    }

    public static void testTree(){
        TreeNode<Integer> tree = new TreeNode<>(1,0,"最小值",1);
        TreeNode<Integer> tree2 = new TreeNode<>(2,1,"最小值",1);
        TreeNode<Integer> tree3 = new TreeNode<>(3,2,"最小值",1);
        TreeNode<Integer> tree4 = new TreeNode<>(4,1,"最小值",1);
        TreeNode<Integer> tree5 = new TreeNode<>(5,2,"最小值",1);
        TreeNode<Integer> tree6 = new TreeNode<>(6,4,"最小值",1);
        TreeNode<Integer> tree7 = new TreeNode<>(7,6,"最小值",1);
        TreeNode<Integer> tree8 = new TreeNode<>(8,3,"最小值",1);
        List<TreeNode<Integer>> treeNodeList = new ArrayList<>();
        treeNodeList.add(tree);
        treeNodeList.add(tree2);
        treeNodeList.add(tree3);
        treeNodeList.add(tree4);
        treeNodeList.add(tree5);
        treeNodeList.add(tree6);
        treeNodeList.add(tree7);
        treeNodeList.add(tree8);

        List<Tree<Integer>> treeList = TreeUtil.build(treeNodeList,0);
        System.out.println(JSON.toJSONString(treeList));
    }

    public static void customTree(){
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("id");                              //默认id,可以不设置
        config.setParentIdKey("pid");                       //父id
        config.setNameKey("dataCategoryName");              //分类名称
        config.setDeep(10);                                  //最大递归深度
        config.setChildrenKey("childrenList");              //孩子节点
        config.setWeightKey("count");                        //排序字段

        List<AppleBranch> dataList = buildAppleBranchList();
        List<Tree<Integer>> treeList = TreeUtil.build(dataList, 0, config, ((object, treeNode) -> {
            treeNode.putExtra("id", object.getId());
            treeNode.putExtra("pid", object.getPid());
            treeNode.putExtra("name", object.getName());
            treeNode.putExtra("length", object.getLength());
            treeNode.putExtra("count", object.getCount());
            //扩展属性...
        }));
        System.out.println(JSON.toJSONString(treeList));
    }



    public static List<AppleBranch> buildAppleBranchList(){
        List<AppleBranch> list = new ArrayList<>();
        AppleBranch appleBranch = AppleBranch.builder().id(1).pid(0).name("主干").length(6).count(100).build();
        AppleBranch appleBranch2 = AppleBranch.builder().id(2).pid(1).name("次干").length(5).count(90).build();
        AppleBranch appleBranch3 = AppleBranch.builder().id(3).pid(1).name("次干").length(5).count(90).build();
        AppleBranch appleBranch4 = AppleBranch.builder().id(4).pid(2).name("分支").length(4).count(80).build();
        AppleBranch appleBranch5 = AppleBranch.builder().id(5).pid(2).name("分支").length(4).count(80).build();
        AppleBranch appleBranch6 = AppleBranch.builder().id(6).pid(2).name("分支").length(4).count(80).build();
        AppleBranch appleBranch7 = AppleBranch.builder().id(7).pid(3).name("分支").length(4).count(80).build();
        AppleBranch appleBranch8 = AppleBranch.builder().id(8).pid(7).name("枝条").length(3).count(50).build();
        AppleBranch appleBranch9 = AppleBranch.builder().id(9).pid(8).name("枝条").length(3).count(50).build();
        AppleBranch appleBranch10 = AppleBranch.builder().id(10).pid(8).name("枝条").length(3).count(50).build();
        AppleBranch appleBranch11 = AppleBranch.builder().id(11).pid(10).name("枝条").length(3).count(50).build();
        AppleBranch appleBranch12 = AppleBranch.builder().id(12).pid(10).name("枝条").length(3).count(50).build();
        list.add(appleBranch);
        list.add(appleBranch2);
        list.add(appleBranch3);
        list.add(appleBranch4);
        list.add(appleBranch5);
        list.add(appleBranch6);
        list.add(appleBranch7);
        list.add(appleBranch8);
        list.add(appleBranch9);
        list.add(appleBranch10);
        list.add(appleBranch11);
        list.add(appleBranch12);
        return list;
    }
}

@Data
@Builder
public class AppleBranch {
    private Integer id;
    private Integer pid;
    private Integer length;
    private String name;
    private Integer count;
    public AppleBranch(Integer id,Integer pid,Integer length,String name,Integer count){
        this.id=id;
        this.pid=pid;
        this.length=length;
        this.name=name;
        this.count=count;
    }
}

默认树结构结果预览:

[
  {
    "id":1,
    "parentId":0,
    "weight":1,
    "name":"最小值",
    "children":[
      {
        "id":2,
        "parentId":1,
        "weight":1,
        "name":"最小值",
        "children":[
          {
            "id":3,
            "parentId":2,
            "weight":1,
            "name":"最小值",
            "children":[
              {
                "id":8,
                "parentId":3,
                "weight":1,
                "name":"最小值"
              }
            ]
          },
          {
            "id":5,
            "parentId":2,
            "weight":1,
            "name":"最小值"
          }
        ]
      },
      {
        "id":4,
        "parentId":1,
        "weight":1,
        "name":"最小值",
        "children":[
          {
            "id":6,
            "parentId":4,
            "weight":1,
            "name":"最小值",
            "children":[
              {
                "id":7,
                "parentId":6,
                "weight":1,
                "name":"最小值"
              }
            ]
          }
        ]
      }
    ]
  }
]

自定义树结构结果预览:

[
  {
    "id":1,
    "pid":0,
    "name":"主干",
    "length":6,
    "count":100,
    "childrenList":[
      {
        "id":2,
        "pid":1,
        "name":"次干",
        "length":5,
        "count":90,
        "childrenList":[
          {
            "id":4,
            "pid":2,
            "name":"分支",
            "length":4,
            "count":80
          },
          {
            "id":5,
            "pid":2,
            "name":"分支",
            "length":4,
            "count":80
          },
          {
            "id":6,
            "pid":2,
            "name":"分支",
            "length":4,
            "count":80
          }
        ]
      },
      {
        "id":3,
        "pid":1,
        "name":"次干",
        "length":5,
        "count":90,
        "childrenList":[
          {
            "id":7,
            "pid":3,
            "name":"分支",
            "length":4,
            "count":80,
            "childrenList":[
              {
                "id":8,
                "pid":7,
                "name":"枝条",
                "length":3,
                "count":50,
                "childrenList":[
                  {
                    "id":9,
                    "pid":8,
                    "name":"枝条",
                    "length":3,
                    "count":50
                  },
                  {
                    "id":10,
                    "pid":8,
                    "name":"枝条",
                    "length":3,
                    "count":50,
                    "childrenList":[
                      {
                        "id":11,
                        "pid":10,
                        "name":"枝条",
                        "length":3,
                        "count":50
                      },
                      {
                        "id":12,
                        "pid":10,
                        "name":"枝条",
                        "length":3,
                        "count":50
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值