Hutool构建树结构

news/2024/7/7 21:18:43 标签: , hutool, Java
  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 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);
                Map<String, Object> map = new HashMap();
                map.put("code", menu.code);
                map.put("createTime", menu.createTime);
                tree.putAll(map);
            }
        });
        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"
    }
]

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

相关文章

VIM介绍

VIM&#xff08;Vi IMproved&#xff09;是一种高度可配置的文本编辑器&#xff0c;用于有效地创建和更改任何类型的文本。它是从 vi 编辑器发展而来的&#xff0c;后者最初是 UNIX 系统上的一个文本编辑器。VIM 以其键盘驱动的界面和强大的文本处理能力而闻名&#xff0c;是许…

在Linux上部署和管理OpenStack云平台

部署和管理OpenStack云平台在Linux上是一个相当复杂的任务&#xff0c;涉及到多个组件和服务的配置和集成。下面是一个简化的步骤概述&#xff0c;用于在Linux上部署和管理OpenStack云平台&#xff1a; 1. 环境准备: • 确保你有足够数量的服务器作为控制节点、计算节点、网络…

鸿蒙 HarmonyOs 动画效果 快速入门

一、理论 1.1 animation属性 名称参数类型必填描述durationnumber否设置动画时长&#xff0c;默认值&#xff1a;1000&#xff0c;单位&#xff1a;毫秒temponumber否动画播放速度。数值越大&#xff0c;速度越快&#xff0c;默认为1curvestring | Curve否 设置动画曲线。 默…

【SQL】索引过多的缺点

索引并不是建得越多越好。虽然索引可以提高查询性能&#xff0c;但它们也带来了一些负面影响&#xff0c;特别是在数据修改操作&#xff08;插入、更新、删除&#xff09;和存储空间方面。以下是一些需要考虑的因素和权衡&#xff1a; 1. 写操作的性能影响 每个索引在数据修改…

Redis基础教程(四):redis键(key)

&#x1f49d;&#x1f49d;&#x1f49d;首先&#xff0c;欢迎各位来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里不仅可以有所收获&#xff0c;同时也能感受到一份轻松欢乐的氛围&#xff0c;祝你生活愉快&#xff01; &#x1f49d;&#x1f49…

cpu,缓存,辅存,主存之间的关系及特点

关系图 示意图&#xff1a; ------------------- | CPU | | ------------- | | | 寄存器 | | | ------------- | | | L1缓存 | | | ------------- | | | L2缓存 | | | ------------- | | | L3缓存 | | | ------------- | ----…

详细分析Oracle修改默认的时间格式(四种方式)

目录 前言1. 会话级别2. 系统级别3. 环境配置4. 函数格式化5. 总结 前言 默认的日期和时间格式由参数NLS_DATE_FORMAT控制 如果需要修改默认的时间格式&#xff0c;可以通过修改会话级别或系统级别的参数来实现 1. 会话级别 在当前会话中设置日期格式&#xff0c;这只会影响…

【嵌入式DIY实例-ESP8266篇】-LCD ST7735显示网络时间

LCD ST7735显示网络时间 文章目录 LCD ST7735显示网络时间1、硬件准备2、代码实现本文将介绍如何使用 ESP8266 NodeMCU Wi-Fi 板实现互联网时钟,其中时间和日期显示在 ST7735 TFT 显示屏上。 ST7735 TFT是一款分辨率为128160像素的彩色显示屏,采用SPI协议与主控设备通信。 1…