Java Create tree structure algorithm instance __ code

Source: Internet
Author: User

In the related development of javaweb often involves multi-level menu display, in order to facilitate the management of the menu needs to use the database for support, this example uses the relevant algorithm to speak the database of the bar records related assembly and ordering of the menu assembled into a tree-shaped structure.

The first is the need for JavaBean

  1 2 3 Import java.io.Serializable;
  4 Import java.util.ArrayList;
  5 Import java.util.Collections;
  6 Import Java.util.Comparator;
  7 Import Java.util.Date;
  8 Import Java.util.Iterator;
 9 Import java.util.List; /** * <Description> Menu extension <br> @email lu.wei<br> * @author 1025742048@qq. COM <br> @date December 19, 2016 <br> @since v1.0<br> * @see com.luwei.console.mg.entity &L T;br> public class Menuext implements Serializable {21 22/** 23 * Menu ID/P
 Rivate Long ID;
 26 27/** 28 * Menu Name * * Private String name;
 31 32/** 33 * Menu Address * * Private String URL;
 36 37/** 38 * Menu icon/private String icon;
 41 42/** 43 * Parent Menu ID/private Long parentid; 46 47/** 48 * Submenu/private list<menuext> children = NEW arraylist<menuext> ();
 51 52/** 53 * Menu Order of the private Integer ordby;
 56 57/** 58 * Menu Status/Private String state;  
 61 62//Ellipsis getter and setter/** * <Description> child node sort <br> 68 * @author lu.wei<br> * @email 1025742048@qq.com <br> * @date December 22, 2016 10:54: <br> A * <br> sortchildren public void () Collections.sort (children , new comparator<menuext> () {@Override the public int compare (Menuext menu1, Menuext me
 NU2) {The result of int = 0;
 The Integer ordby1 = Menu1.getordby ();
 Bayi Integer ordby2 = Menu2.getordby ();
 A Long id1 = Menu1.getid ();
 Long Id2 = Menu2.getid ();    if (null!= ordby1 && null!= ordby2) {86                 result = (Ordby1 < ordby2 1: (ordby1 = = ordby2? 0:1));
 More {The result = (Id1 < ID2 1: (id1 = = Id2? 0:1));
 The return of result;
 91} 92 93}); 94//The next level node for each node is sorted for (iterator<menuext> it = Children.iterator (); It.hasnext ();)
 {It.next (). SortChildren (); 
The public list<menuext> GetChildren () {Children 102} 103 list<menuext> public void Setchildren (children) {This.children = children; 106} 107}

Java algorithm

 1 public static list<menuext> Createtreemenus (list<menuext> menus) {2 list<menuext> Treemenus
 = NULL; 3 if (null!= menus &&!menus.isempty ()) {4//Create root node 5 menuext root = new Menue
 XT ();
 6 Root.setname ("Menu root directory");
7 8//assembly MAP Data 9 Map<long, menuext> datamap = new Hashmap<long, menuext> ();             for (Menuext Menu:menus) {One datamap.put (Menu.getid (), menu); 12} 13 14
Assemble the tree-shaped structure set<entry<long, menuext>> entryset = Datamap.entryset ();                 For (Entry<long, menuext> entry:entryset) {menuext menu = Entry.getvalue (); 18 if (null = = Menu.getparentid () | | 0 = menu.getparentid ()) {Root.getchildren (). Add
(menu);
else {datamap.get (Menu.getparentid ()). GetChildren (). Add (menu);22} 23} 24 25//tree-structured binary tree sort root.sortchildren ();
Treemenus = Root.getchildren ();
return treemenus; 30}

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.