当前位置:网站首页>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; } }
边栏推荐
- 350. 两个数组的交集 II
- MySql必知必会学习
- 系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
- 算法面试经典100题,Android程序员最新职业规划
- A method of removing text blur based on pixel repair
- Benefit a lot, Android interview questions
- 学习探索-使用伪元素清除浮动元素造成的高度坍塌
- C language daily practice - day 22: Zero foundation learning dynamic planning
- Use of deg2rad and rad2deg functions in MATLAB
- 350. Intersection of two arrays II
猜你喜欢
Mysql Information Schema 學習(一)--通用錶
时钟轮在 RPC 中的应用
Synchronous development of business and application: strategic suggestions for application modernization
How to type multiple spaces when editing CSDN articles
学习探索-使用伪元素清除浮动元素造成的高度坍塌
利用 clip-path 绘制不规则的图形
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
冒烟测试怎么做
MySQL information schema learning (II) -- InnoDB table
Black Horse - - Redis Chapter
随机推荐
Cereals Mall - Distributed Advanced p129~p339 (end)
【翻译】供应链安全项目in-toto移至CNCF孵化器
LeetCode_双指针_中等_61. 旋转链表
C language daily practice - day 22: Zero foundation learning dynamic planning
[translation] supply chain security project in toto moved to CNCF incubator
Druid 数据库连接池 详解
Translation D28 (with AC code POJ 26:the nearest number)
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
VMware virtual machine cannot open the kernel device "\.\global\vmx86"
腾讯Android面试必问,10年Android开发经验
Reflection and illegalaccessexception exception during application
Learning and Exploration - Seamless rotation map
Black Horse - - Redis Chapter
DaGAN论文解读
In depth analysis, Android interview real problem analysis is popular all over the network
short i =1; i=i+1与short i=1; i+=1的区别
《数字经济全景白皮书》保险数字化篇 重磅发布
An error occurs when installing MySQL: could not create or access the registry key needed for the
学习探索-无缝轮播图