当前位置:网站首页>通过递归的方式实现树形结构
通过递归的方式实现树形结构
2022-07-29 11:36:00 【theLuckyLong】
背景介绍
我有一个部门的表结构,如果我获取到下列数据,如何通过后端来构造该公司的树形结构
代码
- POJO类
import java.util.List;
public class Company {
private int id;//公司id
private String name;//公司名称
private int pid;//上级公司
private List<Company> subCompanyList;//下游公司
public Company() {
}
public Company(int id, String name, int pid) {
this.id = id;
this.name = name;
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public List<Company> getSubCompanyList() {
return subCompanyList;
}
public void setSubCompanyList(List<Company> subCompanyList) {
this.subCompanyList = subCompanyList;
}
@Override
public String toString() {
return "Company{" +
"id=" + id +
", name='" + name + '\'' +
", pid=" + pid +
", subCompanyList=" + subCompanyList +
'}';
}
}
- 递归方法
public static Company addSubCompany(Company company,List<Company> data){
//构造一个子公司列表
List<Company> subCompany = new ArrayList<>();
for(Company item: data){
//遍历数据
//如果遍历到的部门是当前部门的子部门
if(item.getPid() == company.getId()){
//把遍历到的公司添加到子公司列表,并且递归地调用本方法
subCompany.add(addSubCompany(item,data));
}
}
company.setSubCompanyList(subCompany);
return company;
}
- 具体测试类
这里为了方便,就不搞DAO层了,自己模拟一些数据代替
public static void main(String[] args) {
List<Company> data = new ArrayList<>();
Company c1 = new Company(1,"总公司",-1);
Company c2 = new Company(10,"财务部",1);
Company c3 = new Company(11,"市场部",1);
Company c4 = new Company(12,"研发部",1);
Company c5 = new Company(100,"财务小组1",10);
Company c6 = new Company(101,"财务小组2",10);
Company c7 = new Company(110,"市场小组1",11);
Company c8 = new Company(111,"市场小组2",11);
Company c9 = new Company(120,"研发小组1",12);
Company c10 = new Company(121,"研发小组2",12);
data.add(c1);data.add(c2);data.add(c3);data.add(c4);data.add(c5);
data.add(c6);data.add(c7);data.add(c8);data.add(c9);data.add(c10);
addSubCompany(c1,data);
System.out.println(c1);
}
运行结果
Company{
id=1, name='总公司', pid=-1, subCompanyList=[Company{
id=10, name='财务部', pid=1, subCompanyList=[Company{
id=100, name='财务小组1', pid=10, subCompanyList=[]}, Company{
id=101, name='财务小组2', pid=10, subCompanyList=[]}]}, Company{
id=11, name='市场部', pid=1, subCompanyList=[Company{
id=110, name='市场小组1', pid=11, subCompanyList=[]}, Company{
id=111, name='市场小组2', pid=11, subCompanyList=[]}]}, Company{
id=12, name='研发部', pid=1, subCompanyList=[Company{
id=120, name='研发小组1', pid=12, subCompanyList=[]}, Company{
id=121, name='研发小组2', pid=12, subCompanyList=[]}]}]}
Process finished with exit code 0
边栏推荐
- 7月3日文: 表面上有危险,实属安全周期,大概率会快速上扬的个股
- Gbase8s core data backup
- Function comparison between report control FastReport and stimulus soft
- PHP basics uses arrays to save data
- Paddlelite compilation and code running through the disk
- Golang realizes file upload and download
- Learning with Recoverable Forgetting readings
- 惠及6亿人 投资98亿 沿江高铁武宜段最新进展来了!
- QT's user-defined interface (borderless and movable)
- SkiaSharp of WPF custom painting to bounce ball (case)
猜你喜欢

惠及6亿人 投资98亿 沿江高铁武宜段最新进展来了!

Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem

谷歌“消灭” Cookie 计划延至 2024 年
Xiaoxiao authorization system V5.0 happy version

暑假集训week1

MySql启动、连接sqlog、主从复制、双机热备(开机时)

The heavyweight foundation awarded platinum, gold and silver donors

CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

IPv6 Foundation
随机推荐
QWidget、QDialog、QMainWindow 的异同点
socket+websocket
Learning with Recoverable Forgetting readings
HMS Core Discovery 16 review | with tiger mound, embracing new AI "voice" state
mysql单行,多行子查询
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
即学即用的问题解决思维,给无意识的生活装上“后视镜”
ECCV 2022 | SSP: 自支持匹配的小样本任务新思想
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
什么是 Kubernetes 自定义资源定义 (CRD)?
就这?TypeScript其实并不难!(建议收藏)
Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
深入理解C# 可空类型
游戏合作伙伴专题:BreederDAO 与《王国联盟》结成联盟
sql join中on条件后接and和where
SkiaSharp 之 WPF 自绘 弹动小球(案例版)
593. 有效的正方形
mapbox 地图 生成矢量数据圆
MySQL高级_视图
使用anyio替代asyncio