(original) Parent-child relationship data is processed recursively into tree data (reduces database pressure)

Source: Internet
Author: User

Package Test;import Java.util.arraylist;import java.util.hashmap;import java.util.linkedlist;import java.util.List; Import java.util.map;/** * idpiddatastotree<br> * &LT;P&GT;AUTHOR:TD </P> * <p>date:2016 Year September 30 pm 4: 54:59</p> * &LT;P&GT;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 &lt ;        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)

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.