java中怎么递归部门树
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇文章为大家展示了java中怎么递归部门树,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。部门对象package com.ytx.demo.tree;impo
千家信息网最后更新 2025年12月02日java中怎么递归部门树
本篇文章为大家展示了java中怎么递归部门树,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
部门对象
package com.ytx.demo.tree;import java.util.ArrayList;import java.util.List;public class Department {private int id;private String name;private int parentId;private List children = new ArrayList();public Department(int id, String name, int parentId) { this.id = id; this.name = name; this.parentId = parentId;}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 getParentId() { return parentId;}public void setParentId(int parentId) { this.parentId = parentId;}public List getChildren() { return children;}public void setChildren(List children) { this.children = children;}} 递归实现
package com.ytx.demo.tree;import java.util.ArrayList;import java.util.List;public class DempartmentThree {public static void main(String[] args) { List departmentList = new ArrayList<>(); departmentList.add(new Department(1, "研发部门", 0)); departmentList.add(new Department(2, "研发团队1", 1)); departmentList.add(new Department(3, "研发团队2", 1)); departmentList.add(new Department(4, "财务部门", 0)); departmentList.add(new Department(5, "财务A部门", 4)); departmentList.add(new Department(6, "财务B部门", 4)); departmentList.add(new Department(7, "财务A部门团队1", 5)); departmentList.add(new Department(8, "财务A部门团队2", 5)); departmentList.add(new Department(9, "财务B部门团队1", 6)); departmentList.add(new Department(10, "财务B部门团队2", 6)); List listTree = getThree(departmentList,0); System.out.println(listTree);}private static List getThree(List list,int parentId){ //获取所有子节点 List childTreeList = getChildTree(list,parentId); for (Department dept:childTreeList) { dept.setChildren(getThree(list,dept.getId())); } return childTreeList;}private static List getChildTree(List list,int id){ List childTree = new ArrayList<>(); for (Department dept:list) { if(dept.getParentId() == id){ childTree.add(dept); } } return childTree;}} 上述内容就是java中怎么递归部门树,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
部门
财务
团队
递归
内容
技能
知识
简明
简明扼要
对象
就是
文章
更多
篇文章
节点
行业
资讯
资讯频道
频道
一亮
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
河北嵌入式软件开发哪家专业
网络技术安全措施
大学数据库的ER图
数据库关系代数量习题
西安网络安全巅峰对决在哪
网络安全 演练
服务器管理检查表
网络安全工程师可以在哪就业
数据库和应用迁移实施步骤
ios如何查看数据库文件
mc服务器如何快速打出玩家名字
工商银行合肥分行软件开发
服务器4个网口都干什么用
hp服务器阵列卡驱动
杭州公务员网络安全专业
联合国贸易数据库产品
军队手机网络安全宣传图片
山东网络安全主任张成
查看oracle数据库名称
网络安全性不高
中国期刊物和万方数据库
cbdb 数据库
大专学网络技术出来找什么工作
像素世界服务器
苹果手机的虚拟手机服务器
社科成果数据库
保尔思维导图软件开发
公司网络安全
服务器的日志文件什么时候创建
信通公司网络安全培训记录