• 多级菜单无限递归


    public Map<String, Object> findAll(HttpServletRequest request) {
    Map<String, Object> rs = new HashMap<String, Object>();

    List<Map<String, Object>> articleTypeList = articleTypeDao.findAllMap();
    List<Map<String, Object>> loadChildArticleType = this.loadChildArticleType(articleTypeList, 1);


    return rs;
    }

    /**
    * 递归聚合
    */
    private List<Map<String, Object>> loadChildArticleType(List<Map<String, Object>> allList, int level) {
    if (allList == null || allList.isEmpty() || level == 0) {
    return allList;
    }
    List<Map<String, Object>> mainList = new ArrayList<>();
    List<Map<String, Object>> childList = new ArrayList<>();
    for (Map<String, Object> item : allList) {
    if (toInteger(item.get("level")) == level) {
    mainList.add(item);
    } else if (toInteger(item.get("level")) == level + 1) {
    childList.add(item);
    }
    }
    allList.removeAll(mainList);
    if (!childList.isEmpty()) {
    childList = this.loadChildArticleType(allList, toInteger(childList.get(0).get("level")));
    }
    for (Map<String, Object> main : mainList) {
    for (Map<String, Object> child : childList) {
    if (child.get("upperId").equals(main.get("id"))) {
    List<Map<String, Object>> typeList;
    if (main.get("childTypeList") == null) {
    typeList = new ArrayList<>();
    } else {
    typeList = (List<Map<String, Object>>) main.get("childTypeList");
    }
    typeList.add(child);
    main.put("childTypeList", typeList);
    }
    }
    }
    return mainList;
    }

    实体字段

    private Long id; 主键
    private Long upperId; 上级id
    private int level; 第几极

  • 相关阅读:
    python中用exit退出程序
    习题5-2 使用函数求奇数和 (15分)
    习题5-1 符号函数 (10分)
    练习5-3 数字金字塔 (15分)
    练习5-2 找两个数中最大者 (10分)
    练习5-1 求m到n之和 (10分)
    ubuntu使用教程
    图解HTTP 上
    Sublime Text 3 插件
    两千行PHP学习笔记
  • 原文地址:https://www.cnblogs.com/laixin09/p/10006038.html
Copyright © 2020-2023  润新知