当前位置:网站首页>Recursive implementation of department tree
Recursive implementation of department tree
2022-07-06 19:40:00 【linsa_ pursuer】
1.Department Entity class import lombok.Data; import java.util.List; @Data public class Department { String id; String parentId; List<Department> children; }
2. Test class
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Test { public static void main(String[] args) throws Exception { System.out.println(streamToTree(addList(), "000000")); } public static List<Department> streamToTree(List<Department> treeList, String parentId) { return treeList.stream() // Filter parent nodes .filter(parent -> parent.getParentId().equals(parentId)) // Put the parent node children Recursive assignment becomes a child node .map(child -> { child.setChildren(streamToTree(treeList, child.getId())); return child; }).collect(Collectors.toList()); } private static List<Department> addList(){ List<Department> list = new ArrayList<Department>(); Department departmentOne = new Department(); departmentOne.setParentId("000000"); departmentOne.setId("111111"); list.add(departmentOne); Department departmentOne1 = new Department(); departmentOne1.setParentId("111111"); departmentOne1.setId("111222"); list.add(departmentOne1); Department departmentOne2 = new Department(); departmentOne2.setParentId("111111"); departmentOne2.setId("111333"); list.add(departmentOne2); Department departmentTwo = new Department(); departmentTwo.setParentId("000000"); departmentTwo.setId("222222"); list.add(departmentTwo); Department departmentTwo1 = new Department(); departmentTwo1.setParentId("222222"); departmentTwo1.setId("222111"); list.add(departmentTwo1); return list; } }
边栏推荐
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- 【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
- short i =1; I=i+1 and short i=1; Difference of i+=1
- How to type multiple spaces when editing CSDN articles
- A method of removing text blur based on pixel repair
- Learning and Exploration - Seamless rotation map
- 关于图像的读取及处理等
- 测试用里hi
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
猜你喜欢
Hudi vs Delta vs Iceberg
Phoenix Architecture 3 - transaction processing
深度剖析原理,看完这一篇就够了
黑马--Redis篇
[玩转Linux] [Docker] MySQL安装和配置
How to access localhost:8000 by mobile phone
Low CPU load and high loadavg processing method
谷粒商城--分布式高级篇P129~P339(完结)
How to type multiple spaces when editing CSDN articles
Black Horse - - Redis Chapter
随机推荐
C language daily practice - day 22: Zero foundation learning dynamic planning
Black Horse - - Redis Chapter
Computer network: sorting out common network interview questions (I)
CCNP Part 11 BGP (III) (essence)
通俗的讲解,带你入门协程
Hudi vs Delta vs Iceberg
DaGAN论文解读
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
USB host driver - UVC swap
How to access localhost:8000 by mobile phone
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
利用 clip-path 绘制不规则的图形
Learning and Exploration - Seamless rotation map
[translation] a GPU approach to particle physics
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
CPU负载很低,loadavg很高处理方法
Using clip path to draw irregular graphics
MySQL information Schema Learning (i) - - General table
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Druid database connection pool details