Package Test;import Java.util.arraylist;import java.util.hashmap;import java.util.linkedlist;import java.util.List; Import java.util.map;/** * idpiddatastotree<br> * <P>AUTHOR:TD </P> * <p>date:2016 Year September 30 pm 4: 54:59</p> * <P>DESC: Processing the subordinate data into a tree data structure </P> * @param response * @param request */public class Idpiddat astotree {public static void main (string[] args) {List < Map < String, Object >> list = new Arrayl ist < Map < String, Object >> (); Map < String, Object > m = null; for (int i = 1; i < i++) {m = new HashMap < String, Object > (); M.put ("id", i + ""); M.put ("name", "I am node" + i); M.put ("ParentID", I-1 + ""); List.add (m); List < Map < String, object >> treemenulist = new LinkedList < Map < String, Object >> () ; Treemenulist = treemenulist (list, treemenulist, "0"); SYSTEM.OUT.PRINTLN ("The data to be processed is:" + list.tostring ()); System.out.println ("Well-processed tree-like structure:" + treemenulist.tostring ()); }/** * * @param menulist raw data * @param treemenulist Tree Data Object * @param the parent ID of the parentid data * @return */@SuppressWarnings ("unchecked") public static List < Map < String, Object >> treemenulist (list < ; Map < String, object >> list, List < Map < String, object >> treemenulist, String parentid) { If no root node is in the case ("0". Equals (ParentID)) {treemenulist = Getchildnodelist ("0", list); For (Map < string, Object > Treemenu:treemenulist) {String id = (string) treemenu.get ("id"); Treemenu.put ("Childnode", Getchildnodelist (ID, list)); Treemenulist (list, treemenulist, id); }} else {for (Map < String, Object > Treemenu:treemenulist) {//To determine if there are child elements, some words recursive child The vegetarian continues to judge String id = (string) treemenu.get ("id"); if (Treemenu.containskey ("Childnode")) {//Get child elements List < Map < String, Object & Gt;> childnodelist = (List < Map < String, Object >>) treemenu.get ("Childnode"); if (childnodelist! = null && childnodelist.size () > 0) {for (Map < String, Object &G T Tmenu:childnodelist) {treemenulist (list, childnodelist, tmenu.get ("id"). toString ()); }}//If there are no child elements, query according to ID son assigns son to value} else { List < Map < String, Object >> chlist = getchildnodelist (ID, list); if (null! = chlist) {treemenu.put ("Childnode", chlist); Treemenulist (list, chlist, id); }}}} return treemenulist; } /** * * * @param parentid Data Parent ID * @param list raw Data * @return */public static List < Map < Stri Ng, Object >> getchildnodelist (String parentid, List < Map < String, object >> list) {List < Map < String, object >> childnodelist = new LinkedList < Map < String, object >> (); For (Map < String, Object > Childnode:list) {//New New object (if you do not re-new the object, the value of list will change, do not know what happens?) ) Map < String, object > m = new HashMap < String, object > (); M.putall (Childnode); String pId = (string) childnode.get ("ParentID"); if (Pid.equals (ParentID)) {Childnodelist.add (M); }} if (childnodelist.size () = = 0) return null; return childnodelist; }}
(original) Parent-child relationship data is processed recursively into tree data (reduces database pressure)