当前位置:网站首页>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;
}
}
边栏推荐
- 黑马--Redis篇
- [translation] a GPU approach to particle physics
- Spark foundation -scala
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- How to do smoke test
- 接雨水问题解析
- CCNP Part 11 BGP (III) (essence)
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- Analysis of rainwater connection
猜你喜欢

How to access localhost:8000 by mobile phone

Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe

MySQL information schema learning (II) -- InnoDB table

LeetCode-1279. Traffic light intersection

反射及在运用过程中出现的IllegalAccessException异常

史上超级详细,想找工作的你还不看这份资料就晚了

zabbix 代理服务器 与 zabbix-snmp 监控

接雨水问题解析

Hudi vs Delta vs Iceberg

Hudi vs Delta vs Iceberg
随机推荐
史上超级详细,想找工作的你还不看这份资料就晚了
IC设计流程中需要使用到的文件
Phoenix Architecture 3 - transaction processing
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
谷粒商城--分布式高级篇P129~P339(完结)
Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
Swagger2 reports an error illegal DefaultValue null for parameter type integer
冒烟测试怎么做
MySQL information schema learning (I) -- general table
接雨水问题解析
Computer network: sorting out common network interview questions (I)
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
MySQL must know and learn
Interpretation of Dagan paper
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
ZABBIX proxy server and ZABBIX SNMP monitoring
Zero foundation entry polardb-x: build a highly available system and link the big data screen
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
面试突击63:MySQL 中如何去重?