当前位置:网站首页>部门树递归实现
部门树递归实现
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; } }
边栏推荐
猜你喜欢
Druid 数据库连接池 详解
Looting iii[post sequence traversal and backtracking + dynamic planning]
Don't miss this underestimated movie because of controversy!
冒烟测试怎么做
VMware virtual machine cannot open the kernel device "\.\global\vmx86"
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
在解决了 2961 个用户反馈后,我做出了这样的改变...
MySQL information schema learning (I) -- general table
[玩转Linux] [Docker] MySQL安装和配置
黑马--Redis篇
随机推荐
In depth analysis, Android interview real problem analysis is popular all over the network
通俗的讲解,带你入门协程
usb host 驱动 - UVC 掉包
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
ZABBIX proxy server and ZABBIX SNMP monitoring
swagger2报错Illegal DefaultValue null for parameter type integer
short i =1; I=i+1 and short i=1; Difference of i+=1
Excel 中VBA脚本的简单应用
The list of people who passed the fifth phase of personal ability certification assessment was published
A method of removing text blur based on pixel repair
Druid 数据库连接池 详解
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
CCNP Part 11 BGP (III) (essence)
社招面试心得,2022最新Android高频精选面试题分享
接雨水问题解析
MATLAB中deg2rad和rad2deg函数的使用
Lick the dog until the last one has nothing (simple DP)
DaGAN论文解读
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
Phoenix Architecture 2 - accessing remote services