Java build tree menu, java build tree

Source: Internet
Author: User

Java build tree menu, java build tree

Build a tree menu

:Multi-level menus are supported.

Menu entity class:

Public class Menu {// Menu id private String id; // Menu name private String name; // parent Menu id private String parentId; // Menu url private String url; // Menu icon private String icon; // Menu order private int order; // sub-Menu private List <Menu> children ;//... the getter and setter methods and toString methods are omitted}

Menus generally need to be sorted. We sort the menus according to the order field of the Menu:

/** Sort by order */public Comparator <Menu> order () {Comparator <Menu> comparator = new Comparator <Menu> () {@ Override public int compare (Menu o1, Menu o2) {if (o1.getOrder ()! = O2.getOrder () {return o1.getOrder ()-o2.getOrder ();} return 0 ;}; return comparator ;}

Method of Generating tree:

Public Map <String, Object> findTree () {Map <String, Object> data = new HashMap <String, Object> (); try {// query all Menu lists <Menu> allMenu = menuDao. findTree (); // root node List <Menu> rootMenu = new ArrayList <Menu> (); for (Menu nav: allMenu) {if (nav. getParentId (). equals ("0") {// The parent node is 0, which is the root node. RootMenu. add (nav) ;}}/* sort by order of the Menu class */Collections. sort (rootMenu, order (); // you can specify a sub-Menu for the root Menu. getClild Is A Recursively called for (Menu nav: rootMenu) {/* obtain all subnodes under the root node using the getChild Method */List <Menu> childList = getChild (nav. getId (), allMenu); nav. setChildren (childList); // set a subnode for the root node}/*** output the constructed menu data. **/Data. put ("success", "true"); data. put ("list", rootMenu); return data;} catch (Exception e) {data. put ("success", "false"); data. put ("list", new ArrayList (); return data ;}}

Obtain the sub-menu:

/*** Get subnode ** @ param id parent node id * @ param allMenu all menu lists * @ return under each root node, list of all sub-menus */public List <Menu> getChild (String id, List <Menu> allMenu) {// sub-Menu List <Menu> childList = new ArrayList <Menu> (); for (Menu nav: allMenu) {// traverse all nodes, compare the parent id of all menus with the id of the passed root node // equal Description: it is the child node of the root node. If (nav. parentId (). equals (id) {childList. add (nav) ;}}// recursive for (Menu nav: childList) {nav. setChildren (getChild (nav. getId (), allMenu);} Collections. sort (childList, order (); // sort // if there are no subnodes under the node, an empty List is returned (recursively exiting) if (childList. size () = 0) {return new ArrayList <Menu> () ;}return childList ;}

The returned JSON string is as follows:

{"Success": "true", "list": [{"id": "1", "name": "Java", "parentid": "0 ", "url": "http://www.aliouchen.com", "order": 1, "children": [{"id": "2", "name": "concurrent programming ", "parentid": "1", "url": "http://www.aliouchen.com", "order": 1, "children": []}, {"id": "3 ", "name": "multithreading", "parentid": "1", "url": "http://www.aliouchen.com", "order": 2, "children": ["id ": "4", "name": "Thread", "parentid": "3", "url": "http://www.aliouchen.com", "order": 1,
"Children": []}] },{ "id": "5", "name": "Python", "parentid": "0", "url ": "http://www.aliouchen.com", "order": 2, "children": []}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.