java递归获取某个父节点下面的所有子节点

2021-01-17 16:14

阅读:1367

标签:string   equal   private   port   etl   nod   result   google   算法   

package com.demo.web.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.demo.web.model.Menu;
import com.google.gson.Gson;

public class Test7 {
    private static List treeList = new ArrayList();  //全局变量

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

        List pidList=new ArrayList
();
        Menu mu=new Menu();
        mu.setId(1);
        pidList.add(mu);
        mu.setName("目录");
        mu.setParentid("0");
        Menu mu1=new Menu();
        mu1.setId(2);
        mu1.setName("目录1");
        mu1.setParentid("1");
        Menu mu2=new Menu();
        mu2.setId(3);
        mu2.setName("目录2");
        mu2.setParentid("1");
        Menu mu3=new Menu();
        mu3.setId(4);
        mu3.setName("目录3");
        mu3.setParentid("1");
        
        //目录3下子节点
        Menu mu4=new Menu();
        mu4.setId(5);
        mu4.setName("目录4");
        mu4.setParentid("4");
        
        treeList.add(mu);
        treeList.add(mu1);
        treeList.add(mu2);
        treeList.add(mu3);
        treeList.add(mu4);
        //pid
        System.out.println("pidList:-------------------"+pidList);
        List tree = getTree(treeList,pidList);
        for (Menu menu : tree) {
            System.out.println(menu.getId()+" ------- "+menu.getName()+"  "+menu.getChildren().get(2).getChildren());
        }
        
        System.out.println("7715-=-----------"+tree.get(0).getChildren());
        Gson gson = new Gson();//import com.google.gson.Gson;
        String result = gson.toJson(tree);//pojo
        System.out.println("我看看0---------"+result);
    }
    
    
    public static List getTree(List menu,List
 parentList) {
        //先获取到所有数据
//        treeList=MenuMapper.getList();   
        if(menu==null) return null;

        //获取到所有一级节点
//        List parentList = this.MenuMapper.findParentList();
        List list = new ArrayList();
        if(parentList != null){
            for (int i = 0; i ) {
                list.add(recursiveTree(parentList.get(i).getId()));
            }
        }
        return list;
    }


    /**
     * 递归算法解析成树形结构
     * @param cid
     */
    public static Menu recursiveTree(Integer cid) {
        Menu node = getMenuById(cid);
        List childTreeNodes  = getChildTreeById(cid);
        for(Menu child : childTreeNodes){
            Menu n = recursiveTree(child.getId());
            List list = new ArrayList
();
            list.add(n);
            System.out.println(node.getChildren());
            if (node.getChildren()==null) {
                List li = new ArrayList
();
                node.setChildren(li);
            }
//            node.setChildren(list);
            node.getChildren().add(n);
        }
        return node;
    }

    /**
     * 根据CID查询节点对象
     */
    public static Menu getMenuById(Integer cid){
        Map map =  getTreeMap();
        return (Menu) map.get(cid);
    }

    /**
     * 一次性取所有数据,为了减少对数据库查询操作
     * @return
     */
    public static Map getTreeMap(){
        Map map =  new HashMap();
        if(null != treeList){
            for(Menu d : treeList){
                map.put(d.getId(), d);
            }
        }
        return map;
    }

    /**
     * 根据父节点CID获取所有了节点
     */
    public static List
 getChildTreeById(Integer cid){
        List list = new ArrayList();
        if(null != treeList){
            for (Menu d : treeList) {
                if(null != cid){
                    if (cid.equals(Integer.valueOf(d.getParentid()))) {
                        list.add(d);
                    }
                }
            }
        }
        return list;
    }
}














 

java递归获取某个父节点下面的所有子节点

标签:string   equal   private   port   etl   nod   result   google   算法   

原文地址:https://www.cnblogs.com/demon7715/p/12916423.html


评论


亲,登录后才可以留言!