当前位置:网站首页>部门树递归实现
部门树递归实现
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;
}
}
边栏推荐
- Dark horse -- redis
- MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
- CPU负载很低,loadavg很高处理方法
- MATLAB中deg2rad和rad2deg函数的使用
- MySQL must know and learn
- JDBC详解
- Reflection and illegalaccessexception exception during application
- Use of deg2rad and rad2deg functions in MATLAB
- A method of removing text blur based on pixel repair
- 【pytorch】yolov5 训练自己的数据集
猜你喜欢

接雨水问题解析

It's super detailed in history. It's too late for you to read this information if you want to find a job

理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)

零基础入门PolarDB-X:搭建高可用系统并联动数据大屏

Interview assault 63: how to remove duplication in MySQL?
![[translation] a GPU approach to particle physics](/img/07/57036c925155cab36678c696e89440.jpg)
[translation] a GPU approach to particle physics

社招面试心得,2022最新Android高频精选面试题分享

数学知识——高斯消元(初等行变换解方程组)代码实现

快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数

史上超级详细,想找工作的你还不看这份资料就晚了
随机推荐
CCNP Part 11 BGP (III) (essence)
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
[translation] a GPU approach to particle physics
It's super detailed in history. It's too late for you to read this information if you want to find a job
The slave i/o thread stops because master and slave have equal MySQL serv
Detailed idea and code implementation of infix expression to suffix expression
Zero foundation entry polardb-x: build a highly available system and link the big data screen
ZABBIX proxy server and ZABBIX SNMP monitoring
理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
Reflection and illegalaccessexception exception during application
测试用里hi
反射及在运用过程中出现的IllegalAccessException异常
In 50W, what have I done right?
JDBC详解
How to do smoke test
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Dom 操作
Learning and Exploration - function anti shake