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

JAVA数据结构

树形结构

java基于Map实现集合组装树形结构

public class TestMain {@Data
//    @Builder
//    @NoArgsConstructor@AllArgsConstructorpublic static class DeptEntity {private   Long deptId;private   Long parentId;private   String deptName;private   List<DeptEntity> children = new ArrayList<>();public DeptEntity(Long deptId, Long parentId, String deptName) {this.deptId = deptId;this.parentId = parentId;this.deptName = deptName;}public void addChild(DeptEntity child) {this.children.add(child);}}public static List<DeptEntity> getData() {List<DeptEntity> list = new ArrayList<>();
//        list.add(new DeptEntity(100L, 0L, "若依科技"));list.add(new DeptEntity(101L, 100L, "深圳总公司"));list.add(new DeptEntity(102L, 100L, "长沙分公司"));list.add(new DeptEntity(103L, 101L, "研发部门"));list.add(new DeptEntity(104L, 101L, "市场部门"));list.add(new DeptEntity(105L, 102L, "运维部门"));list.add(new DeptEntity(106L, 102L, "财务部门"));return list;}public static void mapBuildTree() {// 获取假数据List<DeptEntity> data = getData();List<DeptEntity> roots = new ArrayList<>();Map<Long, DeptEntity> childMap = new HashMap<>();// 1.将所有节点存入Map中for (DeptEntity deptEntity : data) {childMap.put(deptEntity.getDeptId(), deptEntity);}// 2.构建树结构for (DeptEntity deptEntity : data) {// 根节点if (deptEntity.getParentId() == null || deptEntity.getParentId() == 0 || !childMap.containsKey(deptEntity.getParentId())) {roots.add(deptEntity);} else {// 非根节点,加入到父节点的children列表DeptEntity parent = childMap.get(deptEntity.getParentId());if (parent != null) {parent.addChild(deptEntity);}}}System.out.println(JSONObject.toJSONString(roots));}public static void main(String[] args) {mapBuildTree();}}

引用 https://blog.csdn.net/zhangfuping123456789/article/details/146275943
引用2:https://www.jb51.net/program/339751h5u.htm

java基于Stream实现集合组装树形结构

public class TestMain {@Data
//    @Builder
//    @NoArgsConstructor@AllArgsConstructorpublic static class DeptEntity {private   Long deptId;private   Long parentId;private   String deptName;private   List<DeptEntity> children = new ArrayList<>();/*** 排序*/private Integer sort;public DeptEntity(Long deptId, Long parentId, String deptName) {this.deptId = deptId;this.parentId = parentId;this.deptName = deptName;}public void addChild(DeptEntity child) {this.children.add(child);}}public static List<DeptEntity> getData() {List<DeptEntity> list = new ArrayList<>();
//        list.add(new DeptEntity(100L, 0L, "若依科技"));list.add(new DeptEntity(101L, 100L, "深圳总公司"));list.add(new DeptEntity(102L, 100L, "长沙分公司"));list.add(new DeptEntity(103L, 101L, "研发部门"));list.add(new DeptEntity(104L, 101L, "市场部门"));list.add(new DeptEntity(105L, 102L, "运维部门"));list.add(new DeptEntity(106L, 102L, "财务部门"));return list;}public static void main(String[] args) {List<DeptEntity> treedata =  listWithTree();System.out.println(JSONObject.toJSONString(treedata));}// 构建树形结构的分类列表public static List<DeptEntity> listWithTree() {// 从某个静态源(可能是数据库或配置)获取所有分类的列表List<DeptEntity> categories = getData();Map<Long, DeptEntity> childMap = new HashMap<>();// 1.将所有节点存入Map中for (DeptEntity deptEntity : categories) {childMap.put(deptEntity.getDeptId(), deptEntity);}// 过滤出所有一级分类(即父分类ID为null的分类),并为每个一级分类设置子分类List<DeptEntity> collect = categories.stream() // 将列表转换为流.filter(m -> !childMap.containsKey(m.getParentId())) // 过滤出一级分类.map((m) -> { // 对每个一级分类进行处理m.setChildren(getChildrenList(m, categories)); // 设置子分类return m; // 返回处理后的分类}).collect(Collectors.toList()); // 收集结果到新的列表中return collect; // 返回构建好的树形结构列表}/*** 获取子节点列表* @param tree 父节点* @param list 分类列表* @return 子节点列表*/public static List<DeptEntity> getChildrenList(DeptEntity tree, List<DeptEntity> list) {// 从列表中过滤出所有父分类ID等于给定父分类ID的分类List<DeptEntity> children = list.stream().filter(item -> Objects.equals(item.getParentId(), tree.getDeptId())).map((item) -> { // 对每个子分类进行处理item.setChildren(getChildrenList(item, list)); // 递归设置子分类的子分类return item; // 返回处理后的子分类}).collect(Collectors.toList()); // 收集结果到新的列表中return children; // 返回构建好的子分类列表}
}
http://www.xdnf.cn/news/2702.html

相关文章:

  • (即插即用模块-特征处理部分) 四十二、(2024 TPAMI) FreqFusion 频率特征融合
  • Nginx的默认主配置文件 “/etc/nginx/nginx.conf“ 解读
  • SQL Server 存储过程开发手册
  • 2025系统架构师---主程序/子程序架构风格
  • 小白学习java第16天(上): javaWeb
  • 【Redis】基础3:一些应用场景
  • TCP协议
  • 2个关键思路,让微课动画场景制作别具一格
  • Fps鬼泣总结:通信——伤害检测
  • 【数据结构】顺序表
  • 伺服电机AB相输出,接入定时器通道,对定时器IO口的速率有何要求【详细分析】
  • 【Unity完整游戏开发案例】从0做一个太空大战游戏
  • MySQL主从同步原理与实践 - Java架构师面试解析
  • 【Python】Matplotlib:立体永生花绘制
  • 单值映射、多值映射
  • Linux:进程间通信->共享内存
  • 开源网络入侵检测与防御系统:Snort
  • 企业私有大模型DeepSeek落地部署该用什么? Ollama还是vLLM
  • PlatformIO 入门学习笔记(一):背景了解
  • 【每天一个知识点】correntropy(相关熵)
  • 08-STM32外部中断
  • el-input限制输入只能是数字 限制input只能输入数字
  • 中国区域250米归一化植被指数数据集(2000-2023)
  • 迅雷精简绿色融合版【高速下载版】12.1.9.2870【11.2.2.1716】【20250426】
  • 树莓派学习专题<10>:使用V4L2驱动获取摄像头数据--申请和管理缓冲区
  • 【PVR】《Adaptive Palm Vein Recognition Method》
  • codeforcesB. Binary Colouring
  • 实人认证开发指南:用API+深度学习构建人证合一系统
  • 【CF】Day45——Codeforces Round 1021 (Div. 2) BC
  • UV工具的安装与使用