Java recursive menu tree converted to Pojo object _java

Source: Internet
Author: User
Copy Code code as follows:

Package Com.cjonline.foundation.authority.pojo;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Iterator;
Import java.util.List;
Import Org.apache.log4j.Logger;
Import Com.cjonline.foundation.util.CheckNullEmpty;
/**
* Implementation of recursive help classes, the final results can be described as follows:
* Roots (Root)
*--1 (Subsystem 1)
*-----1.1
*-------1.1.1
*-------1.1.2
* ------- ....
*-----1.2
*--2 (Subsystem 2)
*-----2.1
*-------2.1.1
*-------2.1.2
* ------- ....
*-----2.2
*/
public class Modellist implements Java.io.Serializable {
private static final Logger Logger = Logger.getlogger (Modellist.class);
Private static final long serialversionuid = 6384598893693849820L;
/**
* Model represents the current module
*/
Private Sysmodel model;
/**
* Models represents the sub module below the current module,
*/
Private list<modellist> sublist = new arraylist<modellist> ();
/**
* Menu Level
*/
private int flag = 0;
Public Sysmodel Getmodel () {
return model;
}
public void Setmodel (Sysmodel model) {
This.model = model;
}
Public list<modellist> getsublist () {
return sublist;
}
public void Setsublist (list<modellist> sublist) {
This.sublist = sublist;
}
public void Setflag (int flag) {
This.flag = Flag;
}
public int Getflag () {
return flag;
}
/**
* Recursive method to form a hierarchy menu based on the incoming module collection
*/
@SuppressWarnings ("Unchecked")
Public modellist CreateTree2 (list<sysmodel> ms) {
//
Modellist node = new Modellist ();
Arraylist<sysmodel> fu = new arraylist<sysmodel> ();//to store the parent node that parentid is empty;
Arraylist<sysmodel> childs = new arraylist<sysmodel> ();//to store modules that are not systems
Separating the system and module menus
for (iterator<sysmodel> it = Ms.iterator (); It.hasnext ();) {
Sysmodel mode = (Sysmodel) it.next ();
String parentid = Mode.getparentid ();
if (ParentID = null | | parentid.equals ("")) {
Fu.add (mode);
} else {
Childs.add (mode);
}
}
Because it's multiple subsystems, you first need to find out how many subsystems
for (Sysmodel Model:fu) {
Modellist Node1 = new Modellist ();
Node1.setflag (0);
Node1.setmodel (model);
Node.subList.add (Node1);
AppendChild (Node1, Childs);
}
return node;
}
/**
* Node Nodes childs as the sub nodes of the system
*/
public void AppendChild (modellist node, list<sysmodel> Childs) {
if (node!= null) {
String SystemID = Node.getmodel (). Getsystemid ();
String Smid = Node.getmodel (). Getsysmoduleid ();
int flag = Node.getflag ();
if (childs!= null && childs.size () > 0) {
for (Sysmodel model:childs) {
String systemId2 = Model.getsystemid ();
String parentId2 = Model.getparentid ();
if (Systemid.equals (SYSTEMID2)) {
if (Parentid2.equals (Smid)) {
Modellist child = new Modellist ();
Child.setmodel (model);
Child.setflag (flag + 1);
Node.getsublist (). Add (child);
AppendChild (child, Childs);
}
}
}
}
}
}
}

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.