当前位置:网站首页>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;
}
}
边栏推荐
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- [translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
- Simple application of VBA script in Excel
- map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
- Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
- [translation] supply chain security project in toto moved to CNCF incubator
- CCNP Part 11 BGP (III) (essence)
- Cereals Mall - Distributed Advanced p129~p339 (end)
- CF960G - Bandit Blues(第一类斯特林数+OGF)
- In 50W, what have I done right?
猜你喜欢

Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease

面试突击63:MySQL 中如何去重?

Leetcode 30. Concatenate substrings of all words

LeetCode-1279. Traffic light intersection

Dark horse -- redis

How to type multiple spaces when editing CSDN articles

【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。

【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)

【计算情与思】扫地僧、打字员、信息恐慌与奥本海默

Don't miss this underestimated movie because of controversy!
随机推荐
Leetcode 30. Concatenate substrings of all words
10 schemes to ensure interface data security
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Dom 操作
利用 clip-path 绘制不规则的图形
Mysql Information Schema 学习(二)--Innodb表
How to do smoke test
深度剖析原理,看完这一篇就够了
How to access localhost:8000 by mobile phone
Low CPU load and high loadavg processing method
Tensorflow2.0 self defined training method to solve function coefficients
在解决了 2961 个用户反馈后,我做出了这样的改变...
Reflection and illegalaccessexception exception during application
中缀表达式转后缀表达式详细思路及代码实现
Leetcode 30. 串联所有单词的子串
short i =1; i=i+1与short i=1; i+=1的区别
企业精益管理体系介绍
腾讯Android面试必问,10年Android开发经验
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv