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

Java 8 中使用 Stream 遍历树形结构

        在实际开发中,我们经常会开发菜单,树形结构,数据库一般就使用父id来表示,为了降低数据库的查询压力,我们可以使用Java8中的Stream流一次性把数据查出来,然后通过流式处理,我们一起来看看,代码实现为了实现简单,就模拟查看数据库所有数据到List里面。

        为了实现这种效果:

下面就使用的一个简单的例子进行演示:

实体类:Departments.java

@Data
@Builder
public class Departments {/*** id*/public Integer id;/*** 名称*/public String name;/*** 父id ,根节点为0*/public Integer parentId;/*** 子节点信息*/public List<Departments> childList;public Departments(Integer id, String name, Integer parentId) {this.id = id;this.name = name;this.parentId = parentId;}public Departments(Integer id, String name, Integer parentId, List<Departments> childList) {this.id = id;this.name = name;this.parentId = parentId;this.childList = childList;}
}

使用递归构建树形结构

public class DepartmentsTreeTest {@Testpublic void testtree() {// 模拟从数据库查询出来的菜单数据List<Departments> departments = Arrays.asList(new Departments(1, "总行", 0),new Departments(2, "分行", 1),new Departments(3, "攀枝花分行", 2),new Departments(4, "成都分行", 2),new Departments(5, "凉山分行", 2),new Departments(6, "支行", 1),new Departments(7, "绵阳支行", 6),new Departments(8, "德阳支行", 6),new Departments(9, "绵阳支行街道", 7),new Departments(10, "德阳支行街道", 7),new Departments(11, "子公司", 1),new Departments(12, "我是子公司", 11));// 获取部门菜单信息// 通过filter()方法筛选出所有部门菜单项。部门的特征是parentId为0,即没有父节点。这些部门菜单项的列表被称为collectList<Departments> collect = departments.stream().filter(m -> m.getParentId() == 0)// 对于每个部门菜单项,我们使用map()方法来递归所有部门地获取其所有子菜单项,并将这些子菜单项设置为部门菜单项的childList属性。.map((m) -> {m.setChildList(getChildrens(m, departments));return m;}).collect(Collectors.toList());System.out.println("-------转json输出结果-------");System.out.println(JSON.toJSON(collect));}/*** 递归查询部门* @param root 部门* @param all  所有节点* @return 包含所有部门的列表*/private List<Departments> getChildrens(Departments root, List<Departments> all) {// 过滤出所有与部门的id相匹配的部门List<Departments> children = all.stream().filter(m -> {// 当所有节点中的parentid与部门的id一致时,表示为部门的部门return Objects.equals(m.getParentId(), root.getId());}).map((m) -> {// 递归查询该部门的部门m.setChildList(getChildrens(m, all));return m;}).collect(Collectors.toList());return children;}}

输出结果:

        输出的JSON格式的结果你可以直接复制进行测试查看。

 

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

相关文章:

  • 网络安全防火墙体验实验
  • YOLOv5引入FasterNet主干网络,目标检测速度提升明显
  • SpringBoot运行时注入一个Bean
  • Pyspark
  • Spring Boot 项目五维度九层次分层架构实现实践研究——持续更新中
  • stm32常见数据类型
  • mac m1使用docker安装kafka
  • SpringBoot核心配置和注解
  • 第三章 图论 No.3 flody之多源汇最短路,传递闭包,最小环与倍增
  • Leetcode-每日一题【剑指 Offer 17. 打印从1到最大的n位数】
  • 远程调试MySQL内核
  • 前端学习---vue2--选项/数据--data-computed-watch-methods-props
  • UML-构件图
  • uniapp使用视频地址获取视频封面
  • java操作PDF:转换、合成、切分
  • 递增子序列——力扣491
  • 解密!品牌独立站为何能成为外国消费者的心头爱?
  • 【HDFS】每天一个RPC系列----complete(二):客户端侧
  • 五、PC远程控制ESP32 LED灯
  • 详解PHP反射API
  • 打开虚拟机进行ip addr无网络连接
  • Spring Boot如何整合mybatisplus
  • webpack基础知识一:说说你对webpack的理解?解决了什么问题?
  • 小研究 - 基于 MySQL 数据库的数据安全应用设计(二)
  • 大数据-数据内容分类
  • Babel编译与Webpack
  • 0805hw
  • ROS实现机器人移动
  • Dockerfile构建LNMP镜像
  • 总结七大排序!