Java实现:从List结构到Tree结构的组织/部门数据转换

该段代码定义了一个queryOrganizationsTree方法,其目的是构建并返回一个组织架构树(OrganizationsTree)。这个树状结构反映了组织或部门的层级关系

以下是该函数的详细解释:

  1. 首先,通过调用DepartmentInfoDao.querySetupBusinessGroupAll()方法,从数据库中获取所有的组织部门信息,这些信息被存储在departAll列表中。

  2. 接着,使用Java 8的流操作对departAll进行处理。通过Collectors.groupingBy方法,以parentId为分组依据,将所有组织部门信息分组存储到一个masterMap中。这个映射表的键是父组织的ID,值是属于该父组织的所有子组织列表。

  3. 创建一个新的OrganizationsTree对象,该对象代表组织结构树的一个节点。设置根节点ID和NAME,这通常代表整个组织的根节点。

  4. 调用getOrganizationTree方法,传入根节点的ID和之前创建的masterMap,该方法会根据父子关系递归地构建出完整的组织结构树。返回的子节点列表被设置为根节点的子节点。

  5. 最后,返回构建好的组织结构树的根节点。

POJO:

//list数据库PO
public class DepartmentInfoPO  {
    /**
     * 部门ID
     */
    private Integer deptId;

    /**
     * 名称
     */
    private String deptName;

    /**
     * 父节点ID
     */
    private Integer parentId;
}
//tree结构bo
public class OrganizationsTree {
    //ID
    private Integer id;
    //名称
    private String label;
    //下级部门
    private List<OrganizationsTree> children;

    public OrganizationsTree(Integer id, String label) {
        this.id = id;
        this.label = label;
    }
}

实现方法: 

    /**
     * 获取组织
     * @return
     */
    private OrganizationsTree queryOrganizationsTree() {
        //查询数据库list
        List<DepartmentInfoPO> departAll = departmentInfoDao.querySetupBusinessGroupAll();
        // 分组
        Map<Integer, List<DepartmentInfoPO>> masterMap = departAll.stream().collect(Collectors.groupingBy(DepartmentInfoPO::getParentId));
        OrganizationsTree organizationsTree = new OrganizationsTree();
        organizationsTree.setId("根节点ID");
        organizationsTree.setLabel("根节点NAME");
        
        List<OrganizationsTree> departmentInfoNext = getOrganizationTree(GroupConstants.BAI_DU_DEPT_ID, masterMap);
        organizationsTree.setChildren(departmentInfoNext);

        return organizationsTree;
    }
   /**
     * 获取本部门的所有子部门信息列表
     */
    public List<OrganizationsTree> getOrganizationTree(Integer dept, Map<Integer, List<DepartmentInfoPO>> masterMap) {
        List<OrganizationsTree> list = new ArrayList<>();
        // 获取本部门下的所有子部门
        List<DepartmentInfoPO> departmentInfoList = masterMap.get(dept);
        if (CollectionUtils.isNotEmpty(departmentInfoList)) {
            for (DepartmentInfoPO departmentInfo : departmentInfoList) {
                Integer deptId = departmentInfo.getDeptId();
                OrganizationsTree organizationsTree = new OrganizationsTree();
                organizationsTree.setId(deptId);
                organizationsTree.setLabel(departmentInfo.getDeptName());
                // 递归至末级部门
                List<OrganizationsTree> infoNext = this.getOrganizationTree(deptId, masterMap);
                if (CollectionUtils.isNotEmpty(infoNext)) {
                    organizationsTree.setChildren(infoNext);
                }
                list.add(organizationsTree);
            }
        }
        return list;
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java皇帝

有帮助就赏点吧,博主点杯水喝喝

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值