当前位置:网站首页>部门树递归实现
部门树递归实现
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; } }
边栏推荐
- Swagger2 reports an error illegal DefaultValue null for parameter type integer
- usb host 驱动 - UVC 掉包
- 测试用里hi
- Cereals Mall - Distributed Advanced p129~p339 (end)
- Modulenotfounderror: no module named 'PIL' solution
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- 10 schemes to ensure interface data security
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- C language daily practice - day 22: Zero foundation learning dynamic planning
- zabbix 代理服务器 与 zabbix-snmp 监控
猜你喜欢
The list of people who passed the fifth phase of personal ability certification assessment was published
谷粒商城--分布式高级篇P129~P339(完结)
接雨水问题解析
深入分析,Android面试真题解析火爆全网
In depth analysis, Android interview real problem analysis is popular all over the network
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Benefit a lot, Android interview questions
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
中缀表达式转后缀表达式详细思路及代码实现
Hudi vs Delta vs Iceberg
随机推荐
保证接口数据安全的10种方案
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
How to type multiple spaces when editing CSDN articles
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
Hudi vs Delta vs Iceberg
ZABBIX proxy server and ZABBIX SNMP monitoring
MySql必知必会学习
LeetCode-1279. Traffic light intersection
Looting iii[post sequence traversal and backtracking + dynamic planning]
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
CCNP Part 11 BGP (III) (essence)
Cereals Mall - Distributed Advanced p129~p339 (end)
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
121. 买卖股票的最佳时机
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
VMware virtual machine cannot open the kernel device "\.\global\vmx86"
JDBC details
LeetCode-1279. 红绿灯路口