当前位置:网站首页>部门树递归实现
部门树递归实现
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;
}
}
边栏推荐
猜你喜欢

腾讯Android面试必问,10年Android开发经验

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!

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

中缀表达式转后缀表达式详细思路及代码实现

Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting

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

Zero foundation entry polardb-x: build a highly available system and link the big data screen

Learning and Exploration - Seamless rotation map

Leetcode 30. 串联所有单词的子串

力扣101题:对称二叉树
随机推荐
Learning and Exploration - function anti shake
手把手教你学会js的原型与原型链,猴子都能看懂的教程
short i =1; I=i+1 and short i=1; Difference of i+=1
A method of removing text blur based on pixel repair
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
[pytorch] yolov5 train your own data set
Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
spark基础-scala
Don't miss this underestimated movie because of controversy!
从sparse.csc.csr_matrix生成邻接矩阵
USB host driver - UVC swap
Druid database connection pool details
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
How to type multiple spaces when editing CSDN articles
Dom 操作
学习探索-无缝轮播图
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
Problems encountered in using RT thread component fish
About image reading and processing, etc
利用 clip-path 绘制不规则的图形