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

树结构转List

 使用LinkedList效率更高

 1、单个顶级节点

public static List<CmsStudentOutline> getTreeList(CmsStudentOutline root) {List<CmsStudentOutline> list = new ArrayList<>();Queue<CmsStudentOutline> queue = new LinkedList<>();if (root == null) {return null;}root.setPid("0");//使用UUID也可以 root.setId(UUID.randomUUID().toString());root.setId(IdUtils.simpleUUID());queue.offer(root);//添加一个元素并返回true如果队列已满,则返回falsewhile (!queue.isEmpty()) {CmsStudentOutline tree = queue.poll();//移除并返问队列头部的元素,如果队列为空,则返回nullString pid = tree.getId();List<CmsStudentOutline> children = tree.getChildren();if (children != null) {for (CmsStudentOutline child : children) {child.setPid(pid);//使用UUID也可以 child.setId(UUID.randomUUID().toString());child.setId(IdUtils.simpleUUID());queue.offer(child);//添加一个元素并返回true,如果队列已满,则返回false}}CmsStudentOutline cmsStudentOutline = new CmsStudentOutline();cmsStudentOutline.setId(tree.getId());cmsStudentOutline.setPid(tree.getPid());cmsStudentOutline.setName(tree.getName());list.add(cmsStudentOutline);}return list;}

2、如果有多个顶级节点(循环即可)

public List<CmsStudentOutline> getList(List<CmsStudentOutline> cmsStudentOutline){List<CmsStudentOutline> list = new ArrayList<>();for (CmsStudentOutline studentOutline : cmsStudentOutline) {list.addAll(breadthFirstTree2List(studentOutline));}return list;}

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

相关文章:

  • Android复习(Android基础-四大组件)——Broadcast
  • Ubuntu下mysql8开启远程连接
  • java对象和json类型转换
  • elasticsearch-head 插件
  • Neo4j之FOREACH基础
  • 【SpringBoot】| 接口架构风格—RESTful
  • CentOS系统环境搭建(十)——CentOS7定时任务
  • 如何在安卓设备上安装并使用 ONLYOFFICE 文档
  • 【制作npm包1】申请npm账号、认识个人包和组织包
  • linux学习(文件描述符)[11]
  • 影响力再度提升,Smartbi多次蝉联Gartner、IDC等权威认可
  • 【动态map】牛客挑战赛67 B
  • mysql(2)
  • 介绍 Apache Spark 的基本概念和在大数据分析中的应用
  • Vue CLI创建Vue项目详细步骤
  • 机器学习算法之-逻辑回归(2)
  • 【业务功能篇65】maven加速 配置settings.xml文件 镜像
  • 题目:售货员的难题(状压dp)
  • Linux 的 MySQL 5.x - 关于 Windows 10 的 Navicat Premium 导入 Excel (.xlsx)文件,报错问题集锦
  • 基于IP网络的存储协议——iSCSI
  • 神经网络基础-神经网络补充概念-27-深层网络中的前向传播
  • 用cpolar生成的公网地址,对位于本地的Cloudreve网盘进行访问
  • docker compose部署zookeeper
  • 【SA8295P 源码分析】77 - QNX Camera 之 ais_server 服务 源码分析
  • 内网搭建电影网站的实现和进行公网访问
  • 5.4 常用滤波算法
  • 【算法系列篇】双指针
  • Web和云开发,Rust会起飞?
  • 深度学习项目学习
  • 【3Ds Max】弯曲命令的简单使用