JAVA递归生成树形菜单

时间:2022-06-28 12:40:55

  递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:

    1.首先从菜单数据中获取所有根节点。

    2.为根节点建立次级子树并拼接上。

    3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。

JAVA递归生成树形菜单

  首先,编写数据模型Menu。每条菜单有自己的id、父节点parentId、菜单名称text、菜单还拥有次级菜单children。

 1 import java.util.List;
 2 
 3 public class Menu {
 4     private String id;
 5     private String parentId;
 6     private String text;
 7     private String url;
 8     private String yxbz;
 9     private List<Menu> children;     
10     public Menu(String id,String parentId,String text,String url,String yxbz) {
11         this.id=id;
12         this.parentId=parentId;
13         this.text=text;
14         this.url=url;
15         this.yxbz=yxbz;   
16     }
17         /*省略get\set*/  
18 }

 

  创建树形结构的类MenuTree。方法getRootNode获取所有根节点,方法builTree将根节点汇总创建树形结构,buildChilTree为节点建立次级树并拼接上当前树,递归调用buildChilTree不断为当前树开枝散叶直至找不到新的子树。完成递归,获取树形结构。

 

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 public class MenuTree {
 5     private List<Menu> menuList = new ArrayList<Menu>();
 6     public MenuTree(List<Menu> menuList) {
 7         this.menuList=menuList;
 8     }
 9 
10     //建立树形结构
11     public List<Menu> builTree(){
12         List<Menu> treeMenus =new  ArrayList<Menu>();
13         for(Menu menuNode : getRootNode()) {
14             menuNode=buildChilTree(menuNode);
15             treeMenus.add(menuNode);
16         }
17         return treeMenus;
18     }
19 
20     //递归,建立子树形结构
21     private Menu buildChilTree(Menu pNode){
22         List<Menu> chilMenus =new  ArrayList<Menu>();
23         for(Menu menuNode : menuList) {
24             if(menuNode.getParentId().equals(pNode.getId())) {
25                 chilMenus.add(buildChilTree(menuNode));
26             }
27         }
28         pNode.setChildren(chilMenus);
29         return pNode;
30     }
31 
32     //获取根节点
33     private List<Menu> getRootNode() {         
34         List<Menu> rootMenuLists =new  ArrayList<Menu>();
35         for(Menu menuNode : menuList) {
36             if(menuNode.getParentId().equals("0")) {
37                 rootMenuLists.add(menuNode);
38             }
39         }
40         return rootMenuLists;
41     }
42 }

 

  最后,插入一些数据试试效果。得到的json就可以生成图一菜单了。

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import com.alibaba.fastjson.JSON;
 4 
 5 public class Hello {
 6     public static void main(String []args) {
 7         List<Menu>  menuList= new ArrayList<Menu>();
 8         /*插入一些数据*/
 9         menuList.add(new Menu("GN001D000","0","系统管理","/admin","Y"));
10         menuList.add(new Menu("GN001D100","GN001D000","权限管理","/admin","Y"));
11         menuList.add(new Menu("GN001D110","GN001D100","密码修改","/admin","Y"));
12         menuList.add(new Menu("GN001D120","GN001D100","新加用户","/admin","Y"));
13         menuList.add(new Menu("GN001D200","GN001D000","系统监控","/admin","Y"));
14         menuList.add(new Menu("GN001D210","GN001D200","在线用户","/admin","Y"));
15         menuList.add(new Menu("GN002D000","0","订阅区","/admin","Y"));
16         menuList.add(new Menu("GN003D000","0","未知领域","/admin","Y"));
17         /*让我们创建树*/
18         MenuTree menuTree =new MenuTree(menuList);
19         menuList=menuTree.builTree();
20         /*转为json看看效果*/
21         String jsonOutput= JSON.toJSONString(menuList);
22         System.out.println(jsonOutput);
23     }
24 }

@luqp 本文地址:https://www.cnblogs.com/lucky-pin/p/10740037.html