zoukankan      html  css  js  c++  java
  • 递归树结构封装

    /**
    * 递归转化树形菜单
    */
    private List<Map<String, Object>> getMenuTree(List<Authorities> authorities, Integer parentId) {
    List<Map<String, Object>> list = new ArrayList<>();
    for (int i = 0; i < authorities.size(); i++) {
    Authorities temp = authorities.get(i);
    //第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
    //第二次temp.getParentId()=-1 parentId=1 不递归不进判断
    //第二次temp.getParentId()=2 parentId=1 不递归不进判断
    if (temp.getIsMenu() == 0 && parentId == temp.getParentId()) {
    Map<String, Object> map = new HashMap<>();
    map.put("menuName", temp.getAuthorityName());
    map.put("menuIcon", temp.getMenuIcon());
    map.put("menuUrl", StringUtil.isBlank(temp.getMenuUrl()) ? "javascript:;" : temp.getMenuUrl());
    map.put("subMenus", getMenuTree(authorities, authorities.get(i).getAuthorityId()));
    list.add(map);
    }
    }
    return list;
    }

     

    public class Sheng {

    private String name;

    private int id;

    private Integer pid;

    private static List<Sheng> shenList;
    private List<Shi> shiList;
    private List<Xian> xianList;
    static {
    shenList=new ArrayList() {{
    add(new Sheng("河南省",1,0));
    add(new Sheng("郑州市",2,1));
    add(new Sheng("开封市",3,1));
    add(new Sheng("洛阳市",4,1));
    add(new Sheng("洛阳市-1",5,4));
    add(new Sheng("洛阳市-2",6,4));
    add(new Sheng("洛阳市-3",7,4));
    add(new Sheng("洛阳市-4",8,4));
    }};
    }

    public static List<Map<String, Object>> bulidTree(List<Sheng> sheng, int pid_) {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    for (Sheng item : sheng) {
    Sheng shen = item;
    //第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
    //第二次temp.getParentId()=-1 parentId=1 不递归不进判断
    //第二次temp.getParentId()=2 parentId=1 不递归不进判断
    if (pid_ == shen.getPid()) {
    Map<String, Object> map = new HashMap<>();
    map.put("menuName", shen.getName());
    map.put("menuIcon", "icon");
    map.put("menuUrl", "login/test");
    map.put("subMenus", bulidTree(sheng, shen.getId()));
    list.add(map);
    }
    }
    return list;
    }

    public static void main(String[] args) {
    Sheng sheng = new Sheng();
    System.out.println(bulidTree(sheng.getShenList(), 0));
    }
    }

    https://blog.csdn.net/qq_36476972/article/details/75089927

  • 相关阅读:
    IIS6.0应用程序池回收和工作进程
    SQL Server 中的 bit 类型的使用
    VS2008 SP1简体中文版下载与.Net Framework 3.5 SP1简体中文版下载
    C#技术教程:http://www.cftea.com/docs/asp.net/csharp/
    C#中得到程序当前工作目录和执行目录的一些方法
    C#中 ? 运算符 和 ??运算符
    dhl:页面中调用 通过类返回SqlDataReader类型的对象
    C# 枚举(enum)-设计
    CnBlogs博文排版技巧 写博文了~~
    金山快译2007版
  • 原文地址:https://www.cnblogs.com/java-llp/p/10962908.html
Copyright © 2011-2022 走看看