美文网首页
List转Tree

List转Tree

作者: 前端菜篮子 | 来源:发表于2021-05-27 13:16 被阅读0次
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StringUtils {
    
    /**
     * 将menus根据pid分组
     * @param menus
     * @return
     */
    public static Map<String, Object>list2Map(List<Menu> menus) {
        Map<String, Object> objs = new HashMap<String, Object>();
        Map<String, Menu> newMenus = new HashMap<String, Menu>();
        Map<String, List<Menu>> map = new HashMap<String, List<Menu>>();
        for(Menu menu : menus) {
            newMenus.put(menu.getId(), menu);
            List<Menu>childMenus = map.get(menu.getPid());
            if(childMenus == null){
                childMenus = new ArrayList<Menu>();
                map.put(menu.getPid(), childMenus);
            }
            childMenus.add(menu);
        }
        objs.put("mapByPid", map);
        objs.put("mapByid", newMenus);
        return objs;
    }
    
    public static Menu Map2Tree(Map<String, Object> objs) {
        Map<String, List<Menu>> map = (Map<String, List<Menu>>) objs.get("mapByPid");
        Map<String, Menu> menus = (Map<String, Menu>) objs.get("mapByid");
        
        Map<String, Menu>pMenus = new HashMap<String, Menu>();
        for(Map.Entry<String, List<Menu>> entry : map.entrySet()){
            String pid = entry.getKey();
            List<Menu> childMenus = entry.getValue(); //子菜单
            Menu pMenu = pMenus.get(pid); //父菜单
            //父id相同的菜单挂在一起
            if(pMenu == null){
                pMenu = menus.get(pid); 
                if(pMenu == null && "/".equals(pid)){
                    pMenu = new Menu("/", "/", "根节点");
                }
                if(pMenu != null)pMenus.put(pid, pMenu);    
            }
            if(pMenu != null)pMenu.setChild(childMenus);  //子菜单挂在父菜单下
        }
        return pMenus.get("/");
    }
    
    
    /**测试*/
    public static void main(String[] args) {
        List<Menu> menus = new ArrayList<Menu>();
        Menu menu1 = new Menu("/", "1", "一级菜单");
        Menu menu99 = new Menu("/", "99", "一级菜单2");
        Menu menu2 = new Menu("1", "2", "二级菜单1");
        Menu menu3 = new Menu("1", "3", "二级菜单2");
        Menu menu4 = new Menu("2", "4", "三级菜单1");
        Menu menu5 = new Menu("2", "5", "三级菜单2");
        Menu menu6 = new Menu("3", "6", "三级菜单3");
        menus.add(menu1);
        menus.add(menu5);
        menus.add(menu6);
        menus.add(menu2);
        menus.add(menu3);
        menus.add(menu4);
        menus.add(menu99);
        Menu menu = Map2Tree(list2Map(menus));
        getName(menu);
    }
    
    public static void getName(Menu menu) {
        if(menu != null && menu.getChild() != null) {
            List<Menu> menus = menu.getChild();
            for(Menu m : menus) {
                getName(m);
            }
        }
        System.out.println("pid:" + menu.getPid() + ";  id:" + menu.getId() + ";  name:" + menu.getName());
    }
       

Menu

public class Menu {
    private String pid;
    private String id;
    private String name;
    
    private Menu parent;

    private List<Menu> child;
    
    public Menu(){
        
    }
    public Menu(String pid, String id, String name) {
        this.pid = pid;
        this.id = id;
        this.name = name;
    }
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }
    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 Menu getParent() {
        return parent;
    }
    public void setParent(Menu parent) {
        this.parent = parent;
    }
    public List<Menu> getChild() {
        return child;
    }
    public void setChild(List<Menu> child) {
        this.child = child;
    }

相关文章

网友评论

      本文标题:List转Tree

      本文链接:https://www.haomeiwen.com/subject/qknrsltx.html