java递归生成目录树 返回list<T>

1,java递归生成目录树 返回list<T>

递归方法返回List<T>类型

public List<ProductCategory> treeselect() {
        // 获取数据库表中的所有数据
        List<ProductCategory> dataList = productCategoryMapper.selectAll();
        log.info("dataList--->>>" + dataList.toString());
        List<ProductCategory> ProductCategoryList = new ArrayList<>();
        // 先找到1级分类
        for (ProductCategory ProductCategory : dataList) {
            if (ProductCategory.getParentId() == 0) {
                ProductCategoryList.add(ProductCategory);
            }
        }
        log.info("ProductCategoryList--->>>" + ProductCategoryList.toString());
        // 为一级菜单设置子菜单,getChild是递归调用的
        for (ProductCategory productCategory : ProductCategoryList) {
            productCategory.setChildren(getChilde(productCategory.getId(), dataList));
        }
        return ProductCategoryList;
    }

递归方法获取子菜单

    private List<ProductCategory> getChilde(Long id, List<ProductCategory> rootList) {
        //子菜单
        List<ProductCategory> childList = new ArrayList<>();
        for (ProductCategory productCategory : rootList) {
            // 遍历所有节点,将父菜单id与传过来的id比较
            if (productCategory.getParentId().equals(id)) {
                childList.add(productCategory);
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (ProductCategory productCategory : childList) {
            productCategory.setChildren(getChilde(productCategory.getId(), rootList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

查询的数据结果

{
  "code": "0",
  "message": "成功",
  "data": [
    {
      "id": "3771306d-b43f-4eea-b759-d06cccb0d74f",
      "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
      "name": "技术",
      "weight": 1,
      "parentId": "top",
      "childList": [
        {
          "id": "7007496f-e438-4be1-95cd-2460346adafc",
          "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
          "name": "集成",
          "weight": 27,
          "parentId": "3771306d-b43f-4eea-b759-d06cccb0d74f",
          "childList": [
			"childList": [
            {
              "id": "7470fdbb-b7fa-4476-98aa-a54478dab960",
              "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
              "name": "计划与调度",
              "weight": 16,
              "parentId": "93cebf9d-2c9f-4316-975a-c8a97822f5b1",
              "variable": 24
            },
            {
              "id": "79d3006b-19df-46f6-aa68-891e2dc05963",
              "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
              "name": "安全环保",
              "weight": 13,
              "parentId": "93cebf9d-2c9f-4316-975a-c8a97822f5b1",
              "variable": 27
            },
            {
              "id": "b6ae80a7-9c75-466d-9025-82dc64e6321b",
              "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
              "name": "生产作业",
              "weight": 26,
              "parentId": "93cebf9d-2c9f-4316-975a-c8a97822f5b1",
              "variable": 25
            }
		  ]
          "variable": 17
        },
        {
          "id": "c109d13e-c1e3-4480-89eb-81f3f32ff2b5",
          "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
          "name": "信息安全",
          "weight": 27,
          "parentId": "3771306d-b43f-4eea-b759-d06cccb0d74f",
          "childList": null,
          "variable": 18
        },
        {
          "id": "f6075a8d-1301-4447-be78-06ee6bd013ac",
          "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
          "name": "数据",
          "weight": 46,
          "parentId": "3771306d-b43f-4eea-b759-d06cccb0d74f",
          "childList": null,
          "variable": 16
        }
      ],
      "variable": 11
    },
    {
      "id": "483d73d8-f5ce-4ff0-82d4-c17bafcc31f6",
      "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
      "name": "人员",
      "weight": 1,
      "parentId": "top",
      "childList": [
        {
          "id": "0046bad2-6e4b-4c3b-a4a5-92d3f39edd70",
          "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
          "name": "人员技能",
          "weight": 50,
          "parentId": "483d73d8-f5ce-4ff0-82d4-c17bafcc31f6",
          "childList": null,
          "variable": 15
        },
        {
          "id": "ad76e507-77c0-4846-b500-42f3675824af",
          "scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
          "name": "组织战略",
          "weight": 50,
          "parentId": "483d73d8-f5ce-4ff0-82d4-c17bafcc31f6",
          "childList": null,
          "variable": 14
        }
      ],
      "variable": 10
    }
  ],
  "timestamp": 1626937445890
}

2,java 求树中每一层的所有节点之和

递归方法的参数为List类型,返回值为void

 	/**
     * 校验指标权值是否合法
     * @param list
     * @return
     */
    public void checkList(List<DiagnosisMetric> list){
        // 出口
        if(list.size() == 0){
            return;
        }
        // 校验权值和是否等于100
        if(checkWeight(list) != 100){
            String parentMetricName = getParentMetricName(list.get(0).getParentId());
            log.info("【"+parentMetricName+"】的下级指标权重和不等于1!");
            throw new BusinessRuntimeException(ResultStatus.DIAG_METRIC_WEIGHT_ERROR);
        }
        // 继续校验下一级指标
        for(DiagnosisMetric parent : list){
            List<DiagnosisMetric> childs = getChilds(parent);
            checkList(childs);
        }
    }
    /**
     * 校验同一级指标权重是否合法
     * @param metrics
     * @return
     */
    public Integer checkWeight(List<DiagnosisMetric> metrics){
        int sum = 0;
        if(metrics.size() > 0){
            for(DiagnosisMetric metric : metrics){
                sum += metric.getWeight();
            }
            if(sum != 100){
                log.error("指标权重不等于100:{}", JSONObject.toJSONString(metrics));
            }
            return sum;
        }
        return sum;
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是一个名为 `GetEmpTreeforRole` 的公共方。它的目的是根据角色 ID 获取员工的字符串表示。下面是代码的详细解释: 1. `if (sb.ToString().Trim() != "")`:这行代码检查字符串变量 `sb` 是否为空。如果 `sb` 不为空,说明之前已经生成过员工字符串,需要进行清空操作。 2. `sb.Clear();`:这行代码清空 `sb` 对象,以便重新生成员工字符串。 3. `str = "";`:这行代码将字符串变量 `str` 清空。变量 `str` 可能在 `SetEmpTreeNode` 方中使用,但在这段代码中没有给出。 4. `List<tb_Dept> ListDept = DB.tb_Dept.OrderBy(m => m.deParentCode).OrderBy(m => m.deSortId).ToList();`:这行代码从数据库中获取所有的部门信息,并按照 `deParentCode` 和 `deSortId` 进行排序,然后将结果存储在名为 `ListDept` 的 `List<tb_Dept>` 对象中。 5. `List<tb_Employee> ListEmp = DB.tb_Employee.Where(m => m.emState == 1).ToList();`:这行代码从数据库中获取状态为 1(代表激活状态)的所有员工信息,并存储在名为 `ListEmp` 的 `List<tb_Employee>` 对象中。 6. `List<int> ListEmpId = DB.tb_EmpRole.Where(m => m.erRoId == roId).Select(m => m.eremId).ToList();`:这行代码从数据库中获取与角色 ID (`roId`) 相关联的员工 ID,并存储在名为 `ListEmpId` 的 `List<int>` 对象中。 7. `SetEmpTreeNode("-1", ListDept, ListEmp, ListEmpId);`:这行代码调用名为 `SetEmpTreeNode` 的方,传递了四个参数:"-1" 表示根节点的 ID,`ListDept` 表示部门列表,`ListEmp` 表示员工列表,`ListEmpId` 表示与角色相关的员工 ID 列表。 8. `return sb.ToString();`:这行代码将生成的员工字符串返回。 根据这段代码,可以推测 `SetEmpTreeNode` 方的实现与递归有关,它可能会根据传入的参数递归地构建员工的字符串表示,并且根据角色相关的员工 ID 进行特殊处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值