The forest required for Java to generate Easyui

Source: Internet
Author: User

In the project, you may encounter the organization tree format, but the data stored in the database can not maintain such a tree, so need intermediate conversion to complete, Ztree can support the pid,id,name format, but Easyui seemingly not, here is the code just code good:

Package Com.xxx.pojo;import Java.util.arraylist;import Java.util.list;public class combotree{private String ID; private string Text;private string State;private string pid;private string Clickflag;private list<combotree> Children;public Combotree () {super () this.setstate ("closed"); This.setid ("xx"); This.settext (null); This.setclickflag ("Off"); This.setchildren (new arraylist<combotree> ());} public void Clear () {this.setstate ("closed"); This.setid (null); This.settext (null); This.setchildren (null);} @Overridepublic String toString () {StringBuilder sb = new StringBuilder (); Sb.append ("{\" id\ ": \" "); Sb.append (this.id); Sb.append ("\", \ "text\": \ ""); Sb.append (This.text); Sb.append ("\", \ "clickflag\": \ ""); Sb.append ("Off"); Sb.append (" \ ""), if (!this.children.isempty ()) {sb.append (", \" state\ ": \" "); Sb.append (this.state); Sb.append (" \ ", \" children\ ": ["); Boolean flag = False;for (Combotree c:this.children) {if (flag) sb.append (", "); Elseflag = True;sb.append (c.tostring ( ));} Sb.append ("]");} Sb.append ("}"); return sb.tostring ();} Public String Getpid () {return PID;} public void Setpid (String pid) {this.pid = pid;} public void Addchildren (Combotree ct) {this.children.add (CT);//System.out.println ("Add");} Add a node public boolean addNode (Combotree ct) {//If the PID of the node that needs to be added is the ID of the current node, then direct//Add, return trueif (this.id = = Ct.pid | | (this.id! = NULL && ct.pid! = null && this.id.equals (ct.pid))) {This.children.add (CT); return true;} Delegate to child node for (Combotree Cct:this.children) {if (Cct.addnode (CT)) return true;} Failed to add success return false;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetText () {return text;} public void SetText (String text) {this.text = text;} Public String GetState () {return state;} public void SetState (String state) {this.state = state;} Public list<combotree> GetChildren () {return children;} public void Setchildren (list<combotree> children) {This.children = children;} Public String Getclickflag () {return clickflag;} public void Setclickflag (String clickflag) {thIs.clickflag = Clickflag;}} 

Package Com.xxx.util;import Java.util.arraylist;import Java.util.hashmap;import java.util.hashset;import Java.util.linkedlist;import java.util.list;import java.util.map;import Java.util.queue;import java.util.Set;import  Com.abc.pojo.combotree;public class Treehelper {/** * input unordered combotree get List type forest * @param list0 unordered combotree node * @return List<combotree> Forest * */@SuppressWarnings ("null") public static list<combotree> Maketree (List<combotree > List0) throws exception{//with PID as key, and PID as the same combotree as List valuemap<string,list<combotree>> Mapforpid = new hashmap<string,list<combotree>> ();//ID tree, used to quickly find Rootpidmap<string,combotree > Mapforid = new hashmap<string,combotree> (),//root pid setset<string> RPS = new hashset<string> () ;//Constructs two types of mapfor (Combotree ct:list0) {//Construction ID Mapmapforid.put (Ct.getid (), CT);//pid Map constructs string PID = Ct.getpid (); list<combotree> LCT = Mapforpid.get (PID), if (LCT = = null) {//construct value type LCT = new Linkedlist<combotree> (); Mapforpid.put (PID, LCT);} Add this node lct.add (CT);} Make rootpidset{//has been looking for idset<string> KeySet = new hashset<string> ();//Search for Rootpidfor (String key: Mapforid.keyset ()) {//along PID until Rootpidwhile (true) {//has already processed this node,//Then be sure that the rootpid of the node is already added if (Keyset.contains (key)) { break;} Added to keyset, indicating that the node has been processed keyset.add (key); Combotree ct = mapforid.get (key); if (ct = = null) {//If CT is null, it means that the key is a rootpidrps.add (key); Next pidkey = Ct.getpid ();}}} Virtual root result tree list<combotree> VTS = new linkedlist<combotree> ();//processing of all root Pid for (String Key:rps) {list& Lt string> queue = new linkedlist<string> (); Combotree vt = new Combotree (), Vt.setid (key), vt.setpid (null), Vt.settext ("Virtual node"),//Add root idqueue.add (key), while (! Queue.isempty ()) {String pid = queue.remove (0); list<combotree> list = Mapforpid.get (PID)///no PID corresponding subtree if (list = = null) continue;for (Combotree ct:list) {// Add to Queue Queue.add (Ct.getid ());//insert to the correct position if (!vt.addnode (CT)) {throw new Exception (ct+ "cannot be insertedInto the tree ");}} Vts.add (VT);} Collation Res result list<combotree> res = new linkedlist<combotree> (); for (Combotree Vct:vts) {///Virtual PID node cannot be a real root node for (Combotree Ct:vct.getChildren ()) {//Add real node res.add (CT);}} return res;} public static Combotree Getcombotree (String id,string pid,string name) {Combotree T = new Combotree (); T.setid (ID); T.setpid (PID); T.settext (name); return t;} public static void Main (string[] args) throws Exception {list<combotree> List = new linkedlist<combotree> (); l Ist.add (Getcombotree ("0", NULL, "root"), List.add (Getcombotree ("1", "0", "root"), List.add (Getcombotree ("2", "0", " Root "), List.add (Getcombotree (" 3 "," 2 "," root "), List.add (Getcombotree (" 4 "," 3 "," root "), List.add (Getcombotree (" 5 "," 4 "," root "), List.add (Getcombotree (" 6 "," 2 "," root "), List.add (Getcombotree (" A ", NULL," root "), List.add ( Getcombotree ("B", "a", "root"), List.add (Getcombotree ("C", "a", "root"), List.add (Getcombotree ("D", "B", "root"); List.add (Getcombotree ("A", "aaaaa", "root"), List.add ("AA", "a", "root"); SyStem.out.println (Maketree (list));}} 

The main function is Maketree, which requires combotree structure support.

The idea of the algorithm is:

Search the collection of rootpid, then build the virtual tree according to Rootpid, note that the root node of the virtual tree is invalid, and it needs to be removed when returning the result set.

Operation Result:

  

The forest required for Java to generate Easyui

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.