当前位置:网站首页>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;
}
}
边栏推荐
- Low CPU load and high loadavg processing method
- 企业精益管理体系介绍
- spark基础-scala
- 手把手教你学会js的原型与原型链,猴子都能看懂的教程
- Mysql Information Schema 学习(一)--通用表
- Cereals Mall - Distributed Advanced p129~p339 (end)
- Use of deg2rad and rad2deg functions in MATLAB
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
- Leetcode topic [array] - 119 Yang Hui triangle II
- 力扣101题:对称二叉树
猜你喜欢

Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
![[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)](/img/1e/b270a81c8457f1eae34f55c004a01a.png)
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)

CPU负载很低,loadavg很高处理方法

系统性详解Redis操作Hash类型数据(带源码分析及测试结果)

DaGAN论文解读

谷粒商城--分布式高级篇P129~P339(完结)

Zero foundation entry polardb-x: build a highly available system and link the big data screen

【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
深入分析,Android面试真题解析火爆全网

社招面试心得,2022最新Android高频精选面试题分享
随机推荐
通俗的讲解,带你入门协程
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
MySql必知必会学习
学习探索-无缝轮播图
Learning and Exploration - function anti shake
时钟轮在 RPC 中的应用
Lick the dog until the last one has nothing (simple DP)
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
Use of deg2rad and rad2deg functions in MATLAB
Take a look at how cabloyjs workflow engine implements activiti boundary events
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Tensorflow2.0 self defined training method to solve function coefficients
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
深度剖析原理,看完这一篇就够了
Excel 中VBA脚本的简单应用
深入分析,Android面试真题解析火爆全网