当前位置:网站首页>部门树递归实现
部门树递归实现
2022-07-06 11:38:00 【linsa_pursuer】
1.Department实体类 import lombok.Data; import java.util.List; @Data public class Department { String id; String parentId; List<Department> children; }
2.测试类
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 -> parent.getParentId().equals(parentId)) // 把父节点children递归赋值成为子节点 .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; } }
边栏推荐
- 在解决了 2961 个用户反馈后,我做出了这样的改变...
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- Cereals Mall - Distributed Advanced p129~p339 (end)
- Interpretation of Dagan paper
- ZABBIX proxy server and ZABBIX SNMP monitoring
- Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
- Spark foundation -scala
- IC设计流程中需要使用到的文件
- CF960G - Bandit Blues(第一类斯特林数+OGF)
猜你喜欢
A method of removing text blur based on pixel repair
Leetcode 30. 串联所有单词的子串
算法面试经典100题,Android程序员最新职业规划
MySQL information schema learning (I) -- general table
深度剖析原理,看完这一篇就够了
反射及在运用过程中出现的IllegalAccessException异常
通俗的讲解,带你入门协程
手把手教你学会js的原型与原型链,猴子都能看懂的教程
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Blue Bridge Cup microbial proliferation C language
随机推荐
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
CCNP Part 11 BGP (III) (essence)
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
About image reading and processing, etc
Mysql Information Schema 学习(二)--Innodb表
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
How to do smoke test
Low CPU load and high loadavg processing method
Synchronous development of business and application: strategic suggestions for application modernization
Mysql Information Schema 學習(一)--通用錶
Documents to be used in IC design process
关于图像的读取及处理等
反射及在运用过程中出现的IllegalAccessException异常
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
VMware virtual machine cannot open the kernel device "\.\global\vmx86"
Spark foundation -scala
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
A method of removing text blur based on pixel repair