java 递归生成树形结构

package com.lsn.ams;

import com.alibaba.fastjson.JSONArray;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Test {


    public static void main(String[] args) {
        List data = new ArrayList();

        Tree tree = new Tree();
        tree.setId("1");
        tree.setPid("0");
        tree.setName("系统管理");

        data.add(tree);

        tree = new Tree();

        tree.setId("2");
        tree.setPid("1");
        tree.setName("管理员列表");
        data.add(tree);

        tree = new Tree();
        tree.setId("3");
        tree.setPid("2");
        tree.setName("新增管理员");
        data.add(tree);

        tree = new Tree();
        tree.setPid("0");
        tree.setId("4");
        tree.setName("导览管理");
        data.add(tree);

        tree = new Tree();
        tree.setId("5");
        tree.setPid("4");
        tree.setName("POI管理");
        data.add(tree);


        MenuTree menuTree = new MenuTree();
        List list = menuTree.menuList(data);

        System.out.println(JSONArray.toJSON(list));

    }


}


/**
 *
 * 递归类
 */
class MenuTree {

    public static Map mapArray = new LinkedHashMap();
    public List menuCommon;
    public List list = new ArrayList();

    public List menuList(List menu) {

        this.menuCommon = menu;
        for (Tree x : menu) {

            Map mapArr = new LinkedHashMap();
            if (x.getPid() == "0") {
                mapArr.put("id", x.getId());
                mapArr.put("name", x.getName());
                mapArr.put("pid", x.getPid());
                mapArr.put("childList", menuChild(x.getId()));
                list.add(mapArr);
            }
        }


        return list;
    }

    public List menuChild(String id) {
        List lists = new ArrayList();
        for (Tree a : menuCommon) {
            Map childArray = new LinkedHashMap();
            if (a.getPid() == id) {
                childArray.put("id", a.getId());
                childArray.put("name", a.getName());
                childArray.put("pid", a.getPid());
                childArray.put("childList", menuChild(a.getId()));
                lists.add(childArray);
            }
        }

        return lists;
    }

}

/**
 * 树实体类
 */
class Tree {

    private String id;
    private String pid;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pId) {
        this.pid = pId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Tree [id=" + id + ", pid=" + pid + ", name=" + name + "]";
    }

} 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(java)