当前位置:网站首页>部门树递归实现
部门树递归实现
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;
}
}
边栏推荐
- [pytorch] yolov5 train your own data set
- Druid database connection pool details
- IC设计流程中需要使用到的文件
- 思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
- 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
- Computer network: sorting out common network interview questions (I)
- 主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
- Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
- 利用 clip-path 绘制不规则的图形
猜你喜欢

How to type multiple spaces when editing CSDN articles
![[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system](/img/63/3addcecb69dcb769c4736653952f66.png)
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system

A method of removing text blur based on pixel repair

Mysql Information Schema 学习(一)--通用表
时钟轮在 RPC 中的应用

黑馬--Redis篇

A popular explanation will help you get started

Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go

Computer network: sorting out common network interview questions (I)

Learning and Exploration - Seamless rotation map
随机推荐
Reflection and illegalaccessexception exception during application
Phoenix Architecture 2 - accessing remote services
从sparse.csc.csr_matrix生成邻接矩阵
short i =1; i=i+1与short i=1; i+=1的区别
黑马--Redis篇
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
学习探索-函数防抖
[玩转Linux] [Docker] MySQL安装和配置
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
MySQL information Schema Learning (i) - - General table
A method of removing text blur based on pixel repair
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
121. 买卖股票的最佳时机
An error occurs when installing MySQL: could not create or access the registry key needed for the
The slave i/o thread stops because master and slave have equal MySQL serv
Interpretation of Dagan paper
Tensorflow and torch code verify whether CUDA is successfully installed
CCNP Part 11 BGP (III) (essence)
Yyds dry goods inventory leetcode question set 751 - 760
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)