博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
父子结构数据(id,pid)递归查询所有子id合集和父id合集
阅读量:6184 次
发布时间:2019-06-21

本文共 2327 字,大约阅读时间需要 7 分钟。

查询子id合集

创建实体类

@Datapublic class Department {
  private int id; private int pid; private String name; public Department(int id, int pid, String name) { this.id = id; this.pid = pid; this.name = name; }

方法

//递归查询子部门---lists所有部门合集,set为id合集,    private static Set
intset = new HashSet<>(); private static void getChiildrenDepartment(List
lists, Set
set) {
Set
set1 = new HashSet<>(); for (Department depart : lists) {
for (Integer i : set) {
if (i.equals(new Integer(depart.getPid()))) {
set1.add(depart.getId()); intset.add(depart.getId()); getChiildrenDepartment(lists, set1); } } } } //递归查询父部门---lists所有部门合集,set为id合集,topId为顶层id private static void getParentDepartmnet(List
lists, Set
set, int topId){ Set
set1 = new HashSet<>(); for (Department depart : lists) { for (Integer i : set) { if (i.equals(new Integer(depart.getId()))) { set1.add(depart.getPid()); intset.add(depart.getPid()); } if( depart.getPid() == topId){ continue; }else { getParentDepartmnet(lists, set1, topId); } } } }

 测试

public static void main(String[] args) {        Department d1 = new Department(1, 0, "总部");        Department d2 = new Department(2, 1, "分部2");        Department d3 = new Department(3, 1, "分部3");        Department d4 = new Department(4, 1, "分部4");        Department d5 = new Department(5, 2, "分部2子部1");        Department d6 = new Department(6, 2, "分部2子部2");        Department d7 = new Department(7, 3, "分部3子部1");        List
list = new ArrayList<>(); list.add(d1); list.add(d2); list.add(d3); list.add(d4); list.add(d5); list.add(d6); list.add(d7); Set
set = new HashSet<>(); set.add(7); getChiildrenDepartment(list, set); System.out.println(intset); intset.addAll(set); System.out.println(intset); intset.clear(); getParentDepartmnet(list, set,0); System.out.println(intset); intset.addAll(set); System.out.println(intset); }

 

转载于:https://www.cnblogs.com/hmpcly/p/10857779.html

你可能感兴趣的文章
Puppet parser命令参数介绍(八)
查看>>
C# 转义符
查看>>
Spring3配置声明式事务
查看>>
Haproxy做LB负载均衡集群的搭建和配置,可以通过web页面监控web服务器的运行状态...
查看>>
python遍历的三种方式
查看>>
C++ list容器系列功能函数详解
查看>>
SQL全角半角标点互转函数
查看>>
蚂蚁变大象:浅谈常规网站是如何从小变大的
查看>>
服务器LVS配置
查看>>
实战Kibana的日志关键词搜索和日志可视化
查看>>
Nginx的安装
查看>>
获取软连接指定的真实文件名
查看>>
2013.6.7
查看>>
且行且珍惜
查看>>
实验指令(3)
查看>>
pl/sql developer 登陆提示ORA-12514
查看>>
handler之bundle
查看>>
U盘数据恢复
查看>>
mysql优化--触发器和auto_increment
查看>>
wget命令 scp命令 rcp命令
查看>>