Hutool工具类TreeUtil的使用(记录)

news/2025/2/25 23:04:51

推荐一个工具网站:Java代码生成平台

添加依赖:

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

Hutool官网

树结构工具-TreeUtil

官方示例:

构建tree:

// 构建node列表
List<TreeNode<String>> nodeList = CollUtil.newArrayList();

nodeList.add(new TreeNode<>("1", "0", "系统管理", 5));
nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));

TreeNode表示一个抽象的节点,也表示数据库中一行数据。 如果有其它数据,可以调用setExtra添加扩展字段。

// 0表示最顶层的id是0
List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");

因为两个Tree是平级的,再没有上层节点,因此为List。

自定义字段名:

//配置
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都要默认值的
treeNodeConfig.setWeightKey("order");
treeNodeConfig.setIdKey("rid");
// 最大递归深度
treeNodeConfig.setDeep(3);

//转换器
List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
        (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setWeight(treeNode.getWeight());
            tree.setName(treeNode.getName());
            // 扩展属性 ...
            tree.putExtra("extraField", 666);
            tree.putExtra("other", new Object());
        });

通过TreeNodeConfig我们可以自定义节点的名称、关系节点id名称,这样就可以和不同的数据库做对应。

使用实例:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AClothClassVo {
    private Integer id;
    private String name;
    private Integer pid;
    private Integer status;
    private String desc;
    // 子目录列表
    private List<AClothClassVo> treeNode;

    public static void main(String[] args) {
        // 构建数据
        List<AClothClassVo> lists = CollUtil.newArrayList();
        lists.add(new AClothClassVo(1, "顶级目录1", 0, 1, "父目录01", null));
        lists.add(new AClothClassVo(2, "顶级目录2", 0, 1, "父目录02", null));
        lists.add(new AClothClassVo(3, "顶级目录3", 0, 1, "父目录03", null));
        lists.add(new AClothClassVo(4, "二级目录4", 1, 1, "父目录01的子类", null));
        lists.add(new AClothClassVo(5, "三级目录5", 4, 1, null, null));
        lists.add(new AClothClassVo(6, "四级目录6", 5, 1, null, null));
        lists.add(new AClothClassVo(7, "二级目录7", 2, 1, "父目录01的子类7", null));
        lists.add(new AClothClassVo(8, "二级目录8", 2, 1, "父目录01的子类8", null));
        lists.add(new AClothClassVo(9, "三级目录9", 4, 1, null, null));

        TreeNodeConfig config = new TreeNodeConfig();

        // 树形数据中id的属性名,写成id1方便区分,实际上写AClothClassVo实体类的id属性名
        config.setIdKey("id1");

        // 展示目录深度,数据中一共四级目录
        config.setDeep(2);

        /**
         * 入参
         * tree:  最终要返回的数据
         * node:  lists数据
         *
         * 返回
         * Tree<String>
         *   Tree: 转换的实体 为数据源里的对象类型
         *   String: ID类型
         *
         */

        List<Tree<String>> list = TreeUtil.build(lists, "0", config, (node, tree) -> {
            tree.setId(node.getId().toString());
            tree.setName(node.getName());
            tree.setParentId(node.getPid().toString());

            // 额外的值
            tree.put("status", node.getStatus());
            tree.put("desc", node.getDesc());
        });

        System.out.println(JSON.toJSONString(list));
    }
}

返回数据:

[
    {
        "id1":"1",
        "name":"顶级目录1",
        "parentId":"0",
        "status":1,
        "desc":"父目录01",
        "children":[
            {
                "id1":"4",
                "name":"二级目录4",
                "parentId":"1",
                "status":1,
                "desc":"父目录01的子类"
            }
        ]
    },
    {
        "id1":"2",
        "name":"顶级目录2",
        "parentId":"0",
        "status":1,
        "desc":"父目录02",
        "children":[
            {
                "id1":"7",
                "name":"二级目录7",
                "parentId":"2",
                "status":1,
                "desc":"父目录01的子类7"
            },
            {
                "id1":"8",
                "name":"二级目录8",
                "parentId":"2",
                "status":1,
                "desc":"父目录01的子类8"
            }
        ]
    },
    {
        "id1":"3",
        "name":"顶级目录3",
        "parentId":"0",
        "status":1,
        "desc":"父目录03"
    }
]


http://www.niftyadmin.cn/n/4054434.html

相关文章

动态规划练习3

题目描述&#xff1a; lw很喜欢玩一种战略游戏&#xff0c;在一个地图上&#xff0c;有n座城堡&#xff0c;每座城堡都有一定的宝物&#xff0c;在每次游戏中lw允许攻克m个城堡并获得里面的宝物。但由于地理位置原因&#xff0c;有些城堡不能直接攻克&#xff0c;要攻克这些城堡…

Java8流式编程GroupBy和求最值示例

流的创建(演示常用的) 数组创建 Arrays.stream Arrays的静态方法stream() 可以获取数组流 String[] arr { "a", "b", "c", "d", "e", "f", "g" };Stream<String> stream Stream.of(arr);Stre…

JavaScript中常见的字符串操作函数及用法汇总

转载地址&#xff1a;http://www.jb51.net/article/65358.htm这篇文章主要介绍了JavaScript中常见的字符串操作函数及用法,实例汇总了javascript常见的字符串转换、分割、查询、替换等技巧,非常具有实用价值,需要的朋友可以参考下。本文实例总结了JavaScript中常见的字符串操作…

基于Redis实现分布式定时任务调度

项目开发过程中&#xff0c;难免会有许多定时任务的需求进来。如果项目中还没有引入quarzt框架的情况下&#xff0c;我们通常会使用Spring的Schedule(cron"* * * * *")注解 样例如下&#xff1a; package com.slowcity.redis;import org.slf4j.Logger; import org.…

linux路由route

一、永久添加路由 重启network服务生效 支持用#注释 方法一 a、添加默认网关&#xff0c;即默认路由 两块网卡在配置文件ifcfg-ethX中不配置网关&#xff0c;在/etc/sysconfig/network中设置默认网关 vim /etc/sysconfig/network GATEWAY192.168.14.254 b、添加路由 创…

mybatis-plus开启sql日志打印

方法一&#xff1a; mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志或者&#xff1a;mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl #关闭sql日志 方法二&#xff1a; loggin…

phonegap(cordova) 自己定义插件代码篇(三)----支付宝支付工具整合

建议读者&#xff0c;先阅读官方文档&#xff0c;知晓其支付流程之后再来使用此代码&#xff0c;比方客户须要做什么&#xff0c;服务端须要做什么&#xff08;非常重要&#xff01;非常重要&#xff01;非常重要&#xff01;&#xff09;&#xff0c;由于这几个篇幅都是纯代码…

mybatis实现自定义分页插件

一、环境搭建 创建一个maven工程&#xff0c;然后引入mybatis依赖和mysql依赖即可。 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.0.4</version> </dependency> <dependency…