构建树结构的几种方式

表结构

sql 复制代码
CREATE TABLE `sys_dept` (
  `dept_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `pid` bigint(20) DEFAULT NULL COMMENT '上级部门',
  `sub_count` int(5) DEFAULT '0' COMMENT '子部门数目',
  `name` varchar(255) NOT NULL COMMENT '名称',
  `dept_sort` int(5) DEFAULT '999' COMMENT '排序',
  `enabled` bit(1) NOT NULL COMMENT '状态',
  `create_by` varchar(255) DEFAULT NULL COMMENT '创建者',
  `update_by` varchar(255) DEFAULT NULL COMMENT '更新者',
  `create_time` datetime DEFAULT NULL COMMENT '创建日期',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`dept_id`) USING BTREE,
  KEY `inx_pid` (`pid`),
  KEY `inx_enabled` (`enabled`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='部门';
java 复制代码
 @Test
    public void getDeptTree() {
        List<Dept> list = new ArrayList();
        Set<Dept> parent = new LinkedHashSet<>();
        // 获取所有的部门信息
        List<Dept> deptList = deptMapper.getAllDepts();

        for (Dept dept : deptList) {
            Dept dept1 = buildParentTree(dept, deptList);
            if (dept1 != null) {
                parent.add(dept1);
            }
        }

        // 构建部门树
//       List<Dept> deptTreeList =  deptList.stream()
//               .filter((dept)-> dept.getPid() == null)
//               .collect(Collectors.toList());
        for (Dept tree : parent) {
            Dept dept = buildDeptTree(tree, deptList);
            list.add(dept);
        }

        for (Dept dept : list) {
            System.out.println("dept - - - - >" + dept);
        }


    }

    private Dept buildParentTree(Dept dept, List<Dept> deptList) {
        Dept dept12 = dept;
        for (Dept dept1 : deptList) {
            if (dept.getPid().equals(dept1.getId())) {
                dept12 = buildParentTree(dept1, deptList);
            }
        }
        return dept12;

    }

    /**
     * 构建部门树
     *
     * @param dept
     * @param deptList
     * @return
     */
    private Dept buildDeptTree(Dept dept, List<Dept> deptList) {

        List<Dept> list = new ArrayList();
        for (Dept dept1 : deptList) {
            if (dept1.getPid().equals(dept.getId())) {
                list.add(buildDeptTree(dept1, deptList));
            }
        }
        dept.setChildren(list);

        return dept;
    }
java 复制代码
    // 使用第二种方法
    @Test
    public void tree() {
        // 获取所有的部门信息
        List<Dept> deptList = deptMapper.getAllDepts();

        List<Dept> collect = deptList.stream().filter((dept) -> dept.getPid() == null)
                .map((dept) -> {
                    dept.setChildren(buildTree(dept, deptList));
                    return dept;
                }).sorted((dept1, dept2) -> {
                    return dept1.getDeptSort() - dept2.getDeptSort();
                })
                .collect(Collectors.toList());

        for (Dept dept : collect) {
            System.out.println(dept);
        }
    }

    private List<Dept> buildTree(Dept dept, List<Dept> deptList) {

        List<Dept> collect = deptList.stream().filter((deptvo) -> {
            return deptvo.getPid() == dept.getId();
        }).map((item) -> {
            item.setChildren(buildTree(item, deptList));
            return item;
        }).sorted((dept1, dept2) -> {
            return dept1.getDeptSort() - dept2.getDeptSort();
        })
                .collect(Collectors.toList());
        return collect;
    }
java 复制代码
    // 第三种
    @Test
    public void buildTree(){
        List<Dept> allDepts = deptMapper.getAllDepts();
        // 构建父子关系
        Map<Long,Dept> map = new HashMap<>();
        for (Dept dept : allDepts) {
            map.put(dept.getId(),dept);
        }

        List<Dept> deptList = new ArrayList<>();
        for (Dept dept : allDepts) {
            if (dept.getPid() == null){
                dept.setChildren(buildChildrenTree(dept.getId(),map));
                deptList.add(dept);
            }
        }
        log.info("构建树资源:{}",allDepts);
        for (Dept allDept : allDepts) {
            System.out.println(allDept);
        }
    }

    private List<Dept> buildChildrenTree(Long parentId, Map<Long, Dept> map) {

        List<Dept> children = new ArrayList<>();
        for (Dept dept : map.values()) {
            if (parentId == dept.getPid()){
                buildChildrenTree(dept.getId(),map);
                children.add(dept);
            }
        }
        return children;
    }
   
相关推荐
TE-茶叶蛋5 小时前
Windows安装Flutter开发环境
windows·flutter
库库林_沙琪马6 小时前
1、nacos
windows
徐子元竟然被占了!!7 小时前
Linux-top
linux·运维·windows
Bruce_Liuxiaowei7 小时前
Windows系统映像劫持:网络安全中的“李代桃僵”战术
windows·安全·web安全
bleach-7 小时前
内网渗透之横向移动&持久化远程控制篇——利用ipc、sc、schtasks、AT,远程连接的winrm,wmic的使用和定时任务的创建
网络·windows·安全·web安全·网络安全·系统安全·安全威胁分析
Nerd Nirvana7 小时前
WSL——Windows Subsystem for Linux流程一览
linux·运维·服务器·windows·嵌入式·wsl·wsl2
JH30738 小时前
Java 是值传递:深入理解参数传递机制
java·开发语言·windows
私人珍藏库8 小时前
[Windows] Office Tool Plus V10.29.50.0
windows·工具·office·辅助
吕了了9 小时前
116Dism++备份系统,如何选择性的备份文件?
运维·windows·电脑·系统
爱倒腾的老唐9 小时前
02、打不开某个网站
windows·笔记·电脑