Hutool TreeUtil工具使用,3步转成树型结构

maven pom

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.5.6</version>
            </dependency>
        // 1.查数据
        List<Menu> all = new ArrayList<>();
        
        // 2.配置
        TreeNodeConfig config = new TreeNodeConfig();
        //config.setIdKey("id");//默认为id可以不设置
        //config.setParentIdKey("parentId");//默认为parentId可以不设置
        //config.setDeep(4);//最大递归深度
        config.setWeightKey("priority");//排序字段
        
        // 3.转树,Tree<>里面泛型为id的类型
        List<Tree<Long>> build = TreeUtil.build(all, 0L, config, (object, tree) -> {
            // 也可以使用 tree.setId(object.getId());等一些默认值
            tree.putExtra("id", object.getId());
            tree.putExtra("parentId", object.getParentId());
            tree.putExtra("icon", object.getIcon());
            tree.putExtra("name", object.getName());
            tree.putExtra("url", object.getUrl());
            tree.putExtra("priority", object.getPriority());
            tree.putExtra("type", object.getType());
        });

猜你喜欢

转载自blog.csdn.net/Anenan/article/details/113029547