当前位置:网站首页>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; } }
边栏推荐
- 蓝桥杯 微生物增殖 C语言
- LeetCode_ Gray code_ Medium_ 89. Gray code
- It's super detailed in history. It's too late for you to read this information if you want to find a job
- Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
- Detailed idea and code implementation of infix expression to suffix expression
- 1805. 字符串中不同整数的数目
- [translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
- 测试用里hi
- 接雨水问题解析
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
猜你喜欢
PMP practice once a day | don't get lost in the exam -7.6
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
《数字经济全景白皮书》保险数字化篇 重磅发布
Spark foundation -scala
Zero foundation entry polardb-x: build a highly available system and link the big data screen
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
算法面试经典100题,Android程序员最新职业规划
企业精益管理体系介绍
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
随机推荐
121. The best time to buy and sell stocks
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Detailed idea and code implementation of infix expression to suffix expression
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
MySQL information Schema Learning (i) - - General table
手把手教你学会js的原型与原型链,猴子都能看懂的教程
蓝桥杯 微生物增殖 C语言
Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
Translation D28 (with AC code POJ 26:the nearest number)
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
usb host 驱动 - UVC 掉包
从sparse.csc.csr_matrix生成邻接矩阵
Problems encountered in using RT thread component fish
黑马--Redis篇
算法面试经典100题,Android程序员最新职业规划
保证接口数据安全的10种方案
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
USB host driver - UVC swap
ZABBIX proxy server and ZABBIX SNMP monitoring
Interpretation of Dagan paper