总结 树结构工具类(一)

菜单Menu:

package com.jc.bean;

import java.util.List;

/**
 * 菜单
 * @author wang-xiaoming
 *
 */
public class Menu {
    // 菜单id
    private String id;
    // 菜单名称
    private String name;
    // 父菜单id
    private String pId;
    // 菜单url
    private String url;
    // 菜单图标
    private String icon;
    // 菜单顺序
    private int order;
    // 子菜单
    private List<Menu> childMenus;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getpId() {
        return pId;
    }
    public void setpId(String pId) {
        this.pId = pId;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
    public List<Menu> getChildMenus() {
        return childMenus;
    }
    public void setChildMenus(List<Menu> childMenus) {
        this.childMenus = childMenus;
    }
    public Menu(String id, String name, String pId) {
        super();
        this.id = id;
        this.name = name;
        this.pId = pId;
    }
    @Override
    public String toString() {
        return "Menu [id=" + id + ", name=" + name + ", pId=" + pId + ", url=" + url + ", icon=" + icon
                + ", order=" + order + ", childMenus=" + childMenus + "]";
    }
}

工具类MenuUtil:

package com.jc.util;

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

import com.jc.bean.Menu;

/**
 * 菜单工具类
 * @author wang-xiaoming
 *
 */
public class MenuUtil {

    // 私有构造
    private MenuUtil() {
    }
    
    // 测试菜单列表
    private static List<Menu> list = new ArrayList<>(16);
    
    // 初始化
    static {
        list.add(new Menu("1","劳务管理","0"));
        list.add(new Menu("2","设备管理","0"));
        list.add(new Menu("3","物料管理","0"));
        list.add(new Menu("4","施工安全","0"));
        list.add(new Menu("5","施工质量","0"));
        list.add(new Menu("6","施工进度","0"));
        list.add(new Menu("7","技术难点","0"));
        list.add(new Menu("8","资料查询","0"));
        list.add(new Menu("9","任务管理","0"));
        list.add(new Menu("10","物联感控","0"));
        list.add(new Menu("11","考勤","1"));
        list.add(new Menu("12","项目部","1"));
        list.add(new Menu("13","劳务队","1"));
        list.add(new Menu("14","劳务分析","1"));
        list.add(new Menu("15","工资结算","1"));
        list.add(new Menu("16","健康防控","1"));
        list.add(new Menu("17","教育培训","1"));
        list.add(new Menu("18","当前位置","2"));
        list.add(new Menu("19","行驶轨迹","2"));
        list.add(new Menu("20","工作状态","2"));
        list.add(new Menu("21","设备总览","2"));
        list.add(new Menu("22","设备搜索","2"));
        list.add(new Menu("23","原材信息","3"));
        list.add(new Menu("24","地磅数据","3"));
        list.add(new Menu("25","出入库","3"));
        list.add(new Menu("26","教育培训","4"));
        list.add(new Menu("27","教育培训","5"));
        list.add(new Menu("28","教育培训","6"));
        list.add(new Menu("29","教育培训","7"));
        list.add(new Menu("30","教育培训","12"));
    }
    
    // 第二步:递归用到的获取子级菜单列表
    public static List<Menu> getChild(String id, List<Menu> rootMenu){
        List<Menu> childList = new ArrayList<>(16);
        // 为父菜单寻找子菜单列表,如果id=pId,则圈起来
        for (Menu menu : rootMenu) {
            if(id.equals(menu.getpId())){
                childList.add(menu);
            }
        }
        
        // 把子菜单的子菜单补充完整
        for (Menu menu : childList) {
            menu.setChildMenus(getChild(menu.getId(), rootMenu));
        }
        
        return childList;
    }
    
    // 第一步:获取一级菜单列表
    public static List<Menu> getRootMenu(List<Menu> list){
        List<Menu> menuList = new ArrayList<>(16);
        for (Menu menu : list) {
            if("0".equals(menu.getpId())){
                menuList.add(menu);
            }
        }
        return menuList;
    }
    
    // 验证
    public static void main(String[] args) {
        System.out.println("---递归前---");
        for (Menu menu : list) {
            System.out.println(menu);
        }
        
        // 收集所有一级菜单
        List<Menu> menuList = getRootMenu(list);
        
        // 为一级菜单设置子菜单
        for (Menu menu : menuList) {
            menu.setChildMenus(getChild(menu.getId(), list));
        }
        
        System.out.println("---递归后---");
        for (Menu menu : menuList) {
            System.out.println(menu);
        }
        
    }
    
}

结果参考:

参考链接:https://blog.csdn.net/qq_41570658/article/details/89384977

posted @ 2020-07-14 18:32  王晓鸣  阅读(287)  评论(0编辑  收藏  举报