In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you the relevant knowledge of what is the realization method of JAVA recursive tree menu. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Recursively generate a menu as shown in the figure, write two class data models Menu, and create a tree MenuTree. This is achieved through the following process:
1. First get all the root nodes from the menu data.
two。 Build a secondary subtree for the root node and splice it together.
3. Recursively create a secondary subtree for the child node and connect it until the "tree" above the end node is spliced.
First, write the data model Menu. Each menu has its own id, parent parentId, menu name text, and a secondary menu children.
Import java.util.List;public class Menu {private String id; private String parentId; private String text; private String url; private String yxbz; private List children; public Menu (String id,String parentId,String text,String url,String yxbz) {this.id=id; this.parentId=parentId; this.text=text; this.url=url; this.yxbz=yxbz;} / * omitting get\ set*/}
Create a class MenuTree with a tree structure. Method getRootNode obtains all the root nodes, method builTree aggregates the root nodes to create a tree structure, buildChilTree establishes a secondary tree for the node and splices the current tree, and recursively calls buildChilTree to open branches and leaves for the current tree until no new subtree can be found. Complete the recursion and get the tree structure.
Import java.util.ArrayList;import java.util.List;public class MenuTree {private List menuList= new ArrayList (); public MenuTree (List menuList) {this.menuList=menuList;} / / create a tree structure public List builTree () {List treeMenus = new ArrayList (); for (Menu menuNode: getRootNode ()) {menuNode=buildChilTree (menuNode); treeMenus.add (menuNode);} return treeMenus } / / Recursive, establish the subtree structure private Menu buildChilTree (Menu pNode) {List chilMenus = new ArrayList (); for (Menu menuNode: menuList) {if (menuNode.getParentId (). Equals (pNode.getId () {chilMenus.add (buildChilTree (menuNode));}} pNode.setChildren (chilMenus); return pNode } / / get the root node private List getRootNode () {List rootMenuLists = new ArrayList (); for (Menu menuNode: menuList) {if (menuNode.getParentId (). Equals ("0")) {rootMenuLists.add (menuNode);}} return rootMenuLists;}}
Finally, insert some data to try the effect. The resulting json can generate the menu in figure 1.
Import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;public class Hello {public static void main (String [] args) {List menuList= new ArrayList (); / * insert some data * / menuList.add (new Menu ("GN001D000", "0", "system Administration", "/ admin", "Y")) MenuList.add (new Menu ("GN001D100", "GN001D000", "Rights Management", "/ admin", "Y"); menuList.add (new Menu ("GN001D110", "GN001D100", "password change", "/ admin", "Y")); menuList.add ("GN001D120", "GN001D100", "New user", "/ admin", "Y") MenuList.add (new Menu ("GN001D200", "GN001D000", "system Monitoring", "/ admin", "Y"); menuList.add (new Menu ("GN001D210", "GN001D200", "online users", "/ admin", "Y"); menuList.add ("GN002D000", "0", "subscription area", "/ admin", "Y") MenuList.add (new Menu ("GN003D000", "0", "unknown territory", "/ admin", "Y")); / * Let's create a tree * / MenuTree menuTree = new MenuTree (menuList); menuList=menuTree.builTree (); / * convert to json to see the effect * / String jsonOutput= JSON.toJSONString (menuList); System.out.println (jsonOutput);}}
Add: java recursive spanning tree structure menu
1. Mysql table, prepare data by yourself
CREATE TABLE `sys_ menu` (`id` int (11) NOT NULL AUTO_INCREMENT, `pid` bigint (20) DEFAULT NULL, `title` varchar (255) CHARACTER SET utf8 DEFAULT NULL, `path`menu`, `level`int (11) DEFAULT NULL, `create_ timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time, `update_ time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT' update time', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=200 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Second, create the corresponding entity class
Public class Menu implements Serializable {private static final long serialVersionUID =-5990021029947688358L; private Integer id; private String title;// menu title private String path;// path private Integer pid;// parent menu ID first level menu pid is null private Integer level;// level, sorted by private List children = new ArrayList () Public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title } public String getPath () {return path;} public void setPath (String path) {this.path = path;} public Integer getPid () {return pid;} public void setPid (Integer pid) {this.pid = pid } public Integer getLevel () {return level;} public void setLevel (Integer level) {this.level = level;} public List getChildren () {return children;} public void setChildren (List children) {this.children = children;}}
Third, recursively assemble tree structure function.
/ * * @ method name: parseMenuTree
* @ description: Assembly menu
* @ param list database full menu list * @ return * / public static List parseMenuTree (List list) {List result = new ArrayList (); / / 1. Get the first-level node for (Menu menu: list) {if (null = = menu.getPid ()) {result.add (menu) }} / / 2. Recursively get child node for (Menu parent: result) {parent = recursiveTree (parent, list);} return result } public static Menu recursiveTree (Menu parent, List list) {for (Menu menu: list) {if (Objects.equals (parent.getId (), menu.getPid () {menu = recursiveTree (menu, list); parent.getChildren (). Add (menu);}} return parent;}
Fourth, get the menu data from the database and call the assembly menu function to generate the data with tree structure.
Public static void main (String [] args) {List list = new ArrayList (); / / TODO here get the full menu from the database and put it into list / / generate tree structure data List result = parseMenuTree (list); System.out.println (JSONObject.toJSONString (result)) } these are all the contents of the article "what is the implementation of JAVA recursive spanning tree menu". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.