Home > Back-end > 001001 processing for Java tree structure
001001 processing for Java tree structure
Time:09-16
Existing a database tables, in accordance with the requirements, the need will be displayed in the table in a tree, only have these three fields, can't add the parent id, how to use the code to handle, please give a little advice
Public void settId (String dar) { This. TId=dar; }
Public String getTreeName () { Return treeName; }
Public void setTreeName (String treeName) { Enclosing treeName=treeName; }
Public String getTreeCode () { Return treeCode; }
Public void setTreeCode (String treeCode) { Enclosing treeCode=treeCode; }
Public List GetChildren () { Return the children==null? New ArrayList<> () : the children; }
Public void setChildren (List The list) { This. The children=list; } }
public List TreeBin (List The list) { TreeNode root=new TreeNode (" root ", "root", "root"); //by treecode order, also can use SQL directly order List. Sort ((a, b) - & gt; Atul gawande etTreeCode (). CompareTo (b.g etTreeCode ())); Map The map=new HashMap<> (a); For (TreeNode node: list) { if (! Map. Either containsKey (node. GetTreeCode ())) { The map. The put (node. GetTreeCode (), the node). } String parentCode=node. GetTreeCode (). The substring (0, node. GetTreeCode (). The length () - 3); If (parentCode. Length ()==0) { Root. GetChildren (). The add (node); } If (map. Either containsKey (parentCode)) { The map. The get (parentCode). GetChildren (). The add (node); } } Return a root. The getChildren (); }
CodePudding user response:
If you are familiar with pid method is how to operate, so also is very simple,