• 菜单树(递归遍历)


              下面递归树菜单效率低:

    package com.admin.manager.storeService.util;

    import com.admin.manager.storeService.entity.Menu;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * @author m
    * @date 2019/12/16
    */
    public class TreeUtil {

    /**
    * 获取菜单树
    * @param menus 所有菜单
    * @return
    */
    public static List<Menu> menuTree(List<Menu> menus) {
    //返回的菜单树
    List<Menu> rootMenus = new ArrayList<>();
    for (Menu menu : menus) {
    //pid(上级Id)为0的是根菜单
    if ("0".equals(menu.getPid())) {
    rootMenus.add(menu);
    }
    }
    //遍历,找到二级菜单(根菜单的id和所有菜单中的pid比较)
    for (Menu rootMenu : rootMenus) {
    List<Menu> child = getChild(String.valueOf(rootMenu.getId()), menus);
    rootMenu.setChildren(child);
    }
    return rootMenus;
    }

    /**
    * 递归获取下级菜单
    * @param pid 上级Id
    * @param menus 所有菜单
    * @return
    */
    public static List<Menu> getChild(String pid, List<Menu> menus) {
    //子菜单列表
    List<Menu> childList = new ArrayList<>();
    for (Menu menu : menus) {
    if (pid.equals(menu.getPid())) {
    childList.add(menu);
    }
    }
    //遍历 递归获取子菜单的子菜单
    for (Menu menu : childList) {
    List<Menu> child = getChild(String.valueOf(menu.getId()), menus);
    menu.setChildren(child);
    }
    //递归出口 childList长度为0
    if (childList.size() == 0) {
    return new ArrayList<>();
    }
    return childList;
    }
    }

    这个效率高点:

    import com.energy.service.vo.RegionTree;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    * @author m
    * @date 2020/4/2
    */
    public class TreeUtil {
    /**
    * 创建树结构
    * @param regionTrees
    * @return
    */
    private List<RegionTree> tree(List<RegionTree> regionTrees) {
    //返回的节点树
    List<RegionTree> rootNodes = new ArrayList<>();
    Iterator<RegionTree> it = regionTrees.iterator();
    while (it.hasNext()) {
    RegionTree next = it.next();
    //parent(上级Id)为0的是根节点
    if ("0".equals(next.getParentId())) {
    rootNodes.add(next);
    it.remove();
    }
    }
    //遍历,找到二级节点
    for (RegionTree regionTree : rootNodes) {
    List<RegionTree> child = getChild(regionTrees, regionTree.getId().toString());
    regionTree.setChildren(child);
    }
    return rootNodes;

    }

    /**
    * 查子节点
    *
    * @param regions
    * @param parentId
    * @return
    */
    private List<RegionTree> getChild(List<RegionTree> regions, String parentId) {

    //子节点列表
    List<RegionTree> childList = new ArrayList<>();
    Iterator<RegionTree> it = regions.iterator();
    while (it.hasNext()) {
    RegionTree regionTree = it.next();
    if (parentId.equals(regionTree.getParentId())) {
    childList.add(regionTree);
    it.remove();
    }
    }

    //遍历 递归获取子节点的子节点
    for (RegionTree regionTree : childList) {
    List<RegionTree> child = getChild(regions, regionTree.getId().toString());
    regionTree.setChildren(child);
    }
    //递归出口 childList长度为0
    if (childList.size() == 0) {
    return new ArrayList<>();
    }
    return childList;
    }
    }

  • 相关阅读:
    element ui 修改默认样式
    npm ERR! Cannot read property 'match' of undefined 错误处理
    Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.
    (转)教你怎么理解正则表达式之零宽断言(环视)
    (转)通过扩展让ASP.NET Web API支持JSONP
    (转)走进AngularJs(六) 服务
    (转)在ASP.NET MVC3 中利用Jsonp跨域访问
    (转)DataTable与结构不同实体类之间的转换
    (转)深入理解最强桌面地图控件GMAP.NET --- 百度地图
    (转)打造一套UI与后台并重.net通用权限管理系统
  • 原文地址:https://www.cnblogs.com/zxy-come-on/p/14164579.html
Copyright © 2020-2023  润新知