当前位置:网站首页>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;
}
}
边栏推荐
- Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
- usb host 驱动 - UVC 掉包
- 1805. 字符串中不同整数的数目
- Blue Bridge Cup microbial proliferation C language
- 【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
- 关于图像的读取及处理等
- LeetCode-1279. 红绿灯路口
- DOM operation
- 反射及在运用过程中出现的IllegalAccessException异常
- USB host driver - UVC swap
猜你喜欢

Dark horse -- redis

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

在解决了 2961 个用户反馈后,我做出了这样的改变...

spark基础-scala

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

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

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

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

LeetCode-1279. Traffic light intersection

反射及在运用过程中出现的IllegalAccessException异常
随机推荐
MySql必知必会学习
Druid database connection pool details
An error occurs when installing MySQL: could not create or access the registry key needed for the
关于图像的读取及处理等
JDBC details
Excel 中VBA脚本的简单应用
受益匪浅,安卓面试问题
测试用里hi
How to do smoke test
Looting iii[post sequence traversal and backtracking + dynamic planning]
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
Dom 操作
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
深入浅出,面试突击版
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
short i =1; i=i+1与short i=1; i+=1的区别
MySQL information schema learning (II) -- InnoDB table
Low CPU load and high loadavg processing method
How to access localhost:8000 by mobile phone
保证接口数据安全的10种方案