美文网首页
Java 遍历之树形菜单

Java 遍历之树形菜单

作者: 赵哥窟 | 来源:发表于2021-05-13 09:40 被阅读0次
818605-20190420093151307-954262998.png

如图,实现这样一个树形结构的菜单,java怎么实现?这里就需要用到遍历。

新建一个实体类
import java.util.List;

public class Menu {
    private String id;
    private String parentId;
    private String text;
    private String url;
    private List<Menu> children;

    public Menu(String id, String parentId, String text, String url) {
        this.id = id;
        this.parentId = parentId;
        this.text = text;
        this.url = url;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<Menu> getChildren() {
        return children;
    }

    public void setChildren(List<Menu> children) {
        this.children = children;
    }
}

树形结构

思路:首先找根节点(一般都是判断parentId 为null 或者 isEmpty()或者equals("xx")),找到根节点后循环根节点,遍历找出根节点对应的子节点一直找到最后一个节点。没有则遍历下一个根节点。

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

public class MenuTree {
    private List<Menu> menuList = new ArrayList<Menu>();

    public MenuTree(List<Menu> menuList) {
        this.menuList = menuList;
    }

    /**
     * 功能描述: 建立树形结构
     *
     * @param:
     * @return:
     * @auther: Destiny
     * @date: 2021/5/13 上午9:14
     */
    public List<Menu> buildTree() {
        List<Menu> treeMenus = new ArrayList<Menu>();
        List<Menu> rootNodes = getRootNode();
        for (Menu rootNode : rootNodes) {
            Menu menuNode = buildChildTree(rootNode);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    /**
     * 功能描述: 获取根节点
     *
     * @param:
     * @return:
     * @auther: Destiny
     * @date: 2021/5/13 上午9:14
     */
    private List<Menu> getRootNode() {
        List<Menu> rootMenuList = new ArrayList<Menu>();
        for (Menu menuNode : menuList) {
            if (menuNode.getParentId().isEmpty()) {
                rootMenuList.add(menuNode);
            }
        }
        return rootMenuList;
    }

    /**
     * 功能描述: 递归,根据根目录获取子目录
     *
     * @param:
     * @return:
     * @auther: Destiny
     * @date: 2021/5/13 上午9:14
     */
    private Menu buildChildTree(Menu pNode) {
        List<Menu> childMenus = new ArrayList<Menu>();
        for (Menu menuNode : menuList) {
            if (menuNode.getParentId().equals(pNode.getId())) {
                childMenus.add(buildChildTree(menuNode));
            }
        }
        pNode.setChildren(childMenus);
        return pNode;
    }

测试
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

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

public class MenuTreeTest {

    @Test
    public void testTreeMenu() {

        List<Menu> menuList = new ArrayList<Menu>();
        /*测试数据*/
        menuList.add(new Menu("ND001D000", "", "系统管理", "/admin"));
        menuList.add(new Menu("ND001D100", "ND001D000", "权限管理", "/admin"));
        menuList.add(new Menu("ND001D110", "ND001D100", "密码修改", "/admin"));
        menuList.add(new Menu("ND001D120", "ND001D100", "新加用户", "/admin"));
        menuList.add(new Menu("ND001D200", "ND001D000", "系统监控", "/admin"));
        menuList.add(new Menu("ND001D210", "ND001D200", "在线用户", "/admin"));
        menuList.add(new Menu("ND002D000", "", "订阅区", "/admin"));
        menuList.add(new Menu("ND003D000", "", "未知领域", "/admin"));
        /*创建树*/
        MenuTree menuTree = new MenuTree(menuList);
        menuList = menuTree.buildTree();
        /*转为json*/
        String jsonOutput = JSON.toJSONString(menuList);
        System.out.println(jsonOutput);
    }

}

结果
[
    {
        "children":[
            {
                "children":[
                    {
                        "children":Array[0],
                        "id":"ND001D110",
                        "parentId":"ND001D100",
                        "text":"密码修改",
                        "url":"/admin"
                    },
                    {
                        "children":Array[0],
                        "id":"ND001D120",
                        "parentId":"ND001D100",
                        "text":"新加用户",
                        "url":"/admin"
                    }
                ],
                "id":"ND001D100",
                "parentId":"ND001D000",
                "text":"权限管理",
                "url":"/admin"
            },
            {
                "children":[
                    {
                        "children":Array[0],
                        "id":"ND001D210",
                        "parentId":"ND001D200",
                        "text":"在线用户",
                        "url":"/admin"
                    }
                ],
                "id":"ND001D200",
                "parentId":"ND001D000",
                "text":"系统监控",
                "url":"/admin"
            }
        ],
        "id":"ND001D000",
        "parentId":"",
        "text":"系统管理",
        "url":"/admin"
    },
    {
        "children":Array[0],
        "id":"ND002D000",
        "parentId":"",
        "text":"订阅区",
        "url":"/admin"
    },
    {
        "children":Array[0],
        "id":"ND003D000",
        "parentId":"",
        "text":"未知领域",
        "url":"/admin"
    }
]

相关文章

网友评论

      本文标题:Java 遍历之树形菜单

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