Java constructs a hashmap tree structure using the list data obtained by the database

Source: Internet
Author: User

 

In this example, a three-level tree structure is built through a three-tier loop, and a list and hashmap are used in combination to create a unique method attribute.

Public hashmap getflowtreelist ()
{

List listoptp = getsqlsession (). selectlist ("com. Cons. pmconf. flowchart. getoptype ");
Hashmap mapreturn = new hashmap ();
// Listoptp
List newlistoptp = new arraylist ();

For (INT I = 0; I <listoptp. Size (); I ++)
{

Hashmap Hm = (hashmap) listoptp. Get (I );
Int code = integer. parseint (Hm. Get ("optypecode"). tostring ());
Flowchart. setoptypecode (CODE );
List flow = getsqlsession (). selectlist ("com. Cons. pmconf. flowchart. getflowchart", flowchart );
List newflow = new arraylist ();
If (flow. Size ()> 0)
{
For (Int J = 0; j <flow. Size (); j ++)
{
Hashmap FL = (hashmap) flow. Get (j );
Int flnum = integer. parseint (FL. Get ("flownum"). tostring ());
Flowchart. setflownum (flnum );
List vision = getsqlsession (). selectlist ("com. Cons. pmconf. flowchart. getflowrevision", flowchart );
List newvision = new arraylist ();
For (int K = 0; k <vision. Size (); k ++)
{
Hashmap VN = (hashmap) vision. Get (k); // use hashmap to store a record. hashmap can continue to add fields (key-value pairs) required by the foreground)
String vnnum = Vn. Get ("flowrevision"). tostring ();
String vnname = "version" + vnnum;
Vn. Put ("treetype", "Rev ");
Vn. Put ("vnname", vnname );
Newvision. Add (VN );
}
If (FL. Get ("flowtype") = "1 ")
Fl. Put ("treetype", "FC ");
Else if (FL. Get ("flowtype") = "40 ")
Fl. Put ("treetype", "childflow ");
Else
Fl. Put ("treetype", "workflow flow ");
Fl. Put ("children", newvision );
Newflow. Add (FL );
}

}
Hm. Put ("treetype", "ft ");
Hm. Put ("children", newflow );

Newlistoptp. Add (HM );
}
Mapreturn. Put ("treelist", newlistoptp );

Return mapreturn;
}

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.