当前位置: 首页 > news >正文

java将list转成树结构

首先是实体类

public class DwdCusPtlSelectDto {//idprivate String key;//值private String value;//中文名private String title;private List<DwdCusPtlSelectDto> children;private String parentId;public void addChild(DwdCusPtlSelectDto child) {if(this.children == null){this.children = new LinkedList<>();}this.children.add(child);}
}

方法

private List<DwdCusPtlSelectDto> rulesToTree(List<DwdCusPtlSelectDto> assetRules) {List<DwdCusPtlSelectDto> list = new LinkedList<>();Map<String, DwdCusPtlSelectDto> nodeMap = new HashMap<>();// 将所有节点放入Map中,方便后续查找for (DwdCusPtlSelectDto node : assetRules) {nodeMap.put(node.getKey(), node);}//不知道为啥概率性出现重复结点,没办法就拿set判断下Set<String> set = new HashSet<>();// 构建树结构for (DwdCusPtlSelectDto node : assetRules) {if ("all".equals(node.getParentId())) {list.add(node);// 找到根节点} else {DwdCusPtlSelectDto parentNode = nodeMap.get(node.getParentId());if (parentNode != null&&!set.contains(node.getKey())) {parentNode.addChild(node);set.add(node.getKey());}}}return list;}

如果有老哥知道为啥会概率醒出现重复结点问题,可以评论告诉我一下

http://www.lryc.cn/news/533478.html

相关文章:

  • 互联网分布式ID解决方案
  • xinference 安装(http导致错误解决)
  • 334递增的三元子序列贪心算法(思路解析+源码)
  • 【Linux】29.Linux 多线程(3)
  • 利用UNIAPP实现短视频上下滑动播放功能
  • vscode+CMake+Debug实现 及权限不足等诸多问题汇总
  • 【提示词工程】探索大语言模型的参数设置:优化提示词交互的技巧
  • 基于 .NET 8.0 gRPC通讯架构设计讲解,客户端+服务端
  • 6.Centos7上部署flask+SQLAlchemy+python+达梦数据库
  • 【C语言系列】深入理解指针(5)
  • mysql自连接 处理层次结构数据
  • ##__VA_ARGS__有什么作用
  • 鸿蒙 router.back()返回不到上个页面
  • 深度学习模型蒸馏技术的发展与应用
  • STM32G0B1 ADC DMA normal
  • <tauri><rust><GUI>基于rust和tauri,在已有的前端框架上手动集成tauri示例
  • 模型 冗余系统(系统科学)
  • Deepseek部署的模型参数要求
  • AI-学习路线图-PyTorch-我是土堆
  • [LeetCode]day17 349.两个数组的交集
  • axios 发起 post请求 json 需要传入数据格式
  • linux交叉编译paho-mqtt-c
  • feign Api接口中注解问题:not annotated with HTTP method type (ex. GET, POST)
  • 安装指定版本的pnpm
  • 【系统设计】Spring、SpringMVC 与 Spring Boot 技术选型指南:人群、场景与实战建议
  • 常用数据结构之String字符串
  • 深入Linux系列之进程地址空间
  • HAL库外设宝典:基于CubeMX的STM32开发手册(持续更新)
  • 网络安全-HSTS
  • 全程Kali linux---CTFshow misc入门(38-50)