Hutool构建树结构

  1. 引入依赖
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>${hutool.version}</version>
</dependency>
  1. 测试
@SpringBootTest
public class HutoolTest {

    private List<Menu> menuList = new ArrayList<>();

    @BeforeEach
    public void init() {
        menuList.add(new Menu(1L, 0L, "一级菜单", 1, "first", LocalDateTime.now()));
        menuList.add(new Menu(2L, 1L, "二级菜单", 1, "second", LocalDateTime.now()));
        menuList.add(new Menu(3L, 2L, "三级菜单", 1, "first", LocalDateTime.now()));
        menuList.add(new Menu(4L, 0L, "一级菜单-1", 2, "first-1", LocalDateTime.now()));
    }

    /**
     * weight越小越靠上
     */
    @Test
    public void testBuildTree() {
    	// TreeNodeConfig用于设置构建为树后的字段名
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("id");
        config.setParentIdKey("pid");
        config.setChildrenKey("children");
        config.setNameKey("name");
        config.setWeightKey("weight");
        List<Tree<Long>> treeList = TreeUtil.build(menuList, 0L, config, new NodeParser<Menu, Long>() {
            @Override
            public void parse(Menu menu, Tree<Long> tree) {
                tree.setId(menu.id);
                tree.setParentId(menu.pid);
                tree.setName(menu.name);
                tree.setWeight(menu.weight);
                // 额外字段,如果不设置那么构建的树就不会有这些字段
                tree.put("code", menu.code);
                tree.put("createTime", menu.createTime);
            }
        });
        System.out.println(JSONUtil.toJsonStr(treeList));
    }

    class Menu {
        private Long id;
        private Long pid;
        private String name;
        private Integer weight;
        private String code;
        private LocalDateTime createTime;

        public Menu(Long id, Long pid, String name, Integer weight, String code, LocalDateTime createTime) {
            this.id = id;
            this.pid = pid;
            this.name = name;
            this.weight = weight;
            this.code = code;
            this.createTime = createTime;
        }
    }
}

  1. 结果
[
    {
        "id": 1,
        "pid": 0,
        "name": "一级菜单",
        "code": "first",
        "createTime": "first",
        "children": [
            {
                "id": 2,
                "pid": 1,
                "name": "二级菜单",
                "code": "second",
                "createTime": "second",
                "children": [
                    {
                        "id": 3,
                        "pid": 2,
                        "name": "三级菜单",
                        "code": "first",
                        "createTime": "first"
                    }
                ]
            }
        ]
    },
    {
        "id": 4,
        "pid": 0,
        "name": "一级菜单-1",
        "code": "first-1",
        "createTime": "first-1"
    }
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值