java递归查询多层

1不多说直接上代码



    /**
     * 查询多层数据,树形展示
     *  2021年04月07日
     */
    @RequestMapping("ceshi")
    public Result ceshi(@RequestBody EquipmentParameter parameter) {
        log.info(" ============正在查询 ");
        //查询全部数据list
        List<EquipmentParameter> equipmentParameterListStart =equipmentService.equipmentParameterList(parameter);
        //创建一个一级list
        List<EquipmentParameter> equipmentParameterList=new ArrayList<>();
        // 先找到所有的一级分组
        for (int i = 0; i < equipmentParameterListStart.size(); i++){
            EquipmentParameter sysMenu=equipmentParameterListStart.get(i);
            //parentLevelId=0时候意思为一级分组
            int parentLevelId=sysMenu.getParentLevelId();
            // 如果是顶级分组,没有父分组
            if (parentLevelId==0){
                equipmentParameterList.add(sysMenu);
            }
        }
        // 为一级分组设置子分组,getChild是递归调用的
        for (EquipmentParameter menu : equipmentParameterList) {
            //获取子分组
            menu.setChildren(getChild(menu.getId(), equipmentParameterListStart));
        }
        return Result.success(equipmentParameterList);
    }

    // 递归查找子分组
    private List<EquipmentParameter> getChild(int id, List<EquipmentParameter> equipmentParameterListStart) {
        //子分组
        List<EquipmentParameter> childList = new ArrayList<EquipmentParameter>();
        for (EquipmentParameter menu : equipmentParameterListStart) {
            //遍历所有节点,将父分组id与传过来的id比较
            if (menu.getParentLevelId()==id) {
                childList.add(menu);
            }

        }
        //递归(如果就两层递归下面这个for循环可以不用)
        for (EquipmentParameter entity : childList) {
            entity.setChildren(getChild(entity.getId(), equipmentParameterListStart));
        }

        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;

    }


2展示结果

  "code": "200",
  "message": "成功",
  "data": [
    {
      "id": 1,
      "tabName": "江苏",
      "tabId": "01",
      "parentLevelId": 0,
      "children": [
        {
          "id": 2,
          "tabName": "宿迁",
          "tabId": "1",
          "parentLevelId": 1,
          "children": [
            {
			  "id": 4,
              "tabName": "王集",
              "tabId": "1",
              "parentLevelId": 2,
              "children": null
            }
          ]
        }
      ]
    },
    {
      "id": 3,
      "tabName": "南京",
      "tabId": "19",
      "parentLevelId": 0,
      "children": null
    }
  ],
  "success": true
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用递归来判断多层Map中是否存在create_time属性,具体代码如下: ```java public static boolean isExistCreateTime(Map<String, Object> map) { for (Map.Entry<String, Object> entry : map.entrySet()) { Object value = entry.getValue(); // 如果value是Map类型,则递归调用isExistCreateTime方法 if (value instanceof Map) { boolean result = isExistCreateTime((Map<String, Object>) value); if (result) { return true; } } else if (entry.getKey().equals("create_time")) { return true; } } return false; } ``` 以上代码中,我们首先遍历Map中的每一个键值对,如果对应的值是一个Map类型,则递归调用isExistCreateTime方法来查找是否存在create_time属性。如果存在,则返回true。如果对应的值不是Map类型,我们判断该键是否为create_time,如果是,则返回true。如果遍历完整个Map都没有找到create_time属性,则返回false。 使用方法如下: ```java Map<String, Object> map = new HashMap<>(); map.put("key1", "value1"); Map<String, Object> innerMap = new HashMap<>(); innerMap.put("key2", "value2"); innerMap.put("create_time", "2021-07-01 10:00:00"); map.put("key3", innerMap); boolean result = isExistCreateTime(map); if (result) { System.out.println("存在create_time属性"); } else { System.out.println("不存在create_time属性"); } ``` 以上代码中,我们创建一个多层Map,然后调用isExistCreateTime方法来判断是否存在create_time属性。如果存在,则输出存在,否则输出不存在。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值