递归简单应用之获取树形结构(一)

TreesUtils:树形工具类,根据父子ID形成树形结构

package cn.cityworks.utils;

import cn.cityworks.domain.Organization;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zsx
 * @date: 2019/4/16 0016
 */
public class TreesUtils {

    /**
     *  @Description  //TODO
     *  @Param  nodes  :所有的节点列表
     */
    public  List  data(List nodes)  {
        ArrayList rootNode  =  new  ArrayList<>();
        for  (Organization  node  :  nodes)  {
            if  (node.getPid().equals("0"))  {
                rootNode.add(node);
            }
        }
        for  (Organization  node  :  rootNode)  {
            List  child  =  getChild(String.valueOf(node.getId()),  nodes);
            node.setList(child);
        }
        return  rootNode;
    }


    /**
     *  @Description  //TODO  获取根节点的子节点
     */
    public  List  getChild(String  id,  List  allNode)  {
        //存放子菜单的集合
        ArrayList  listChild  =  new  ArrayList<>();
        for  (Organization  node  :  allNode)  {
            if  (node.getPid().equals(id))  {
                listChild.add(node);
            }
        }
        //递归(递归容易栈溢出,后期可以考虑尾递归优化)
        for  (Organization  node  :  listChild)  {
            node.setList(getChild(String.valueOf(node.getId()),  allNode));
        }
        if  (listChild.size()  ==  0)  {
            return  null;
        }
        return  listChild;
    }


















}

Organization实体类:主要根据pid。list。和自身ID去实现树形

package cn.cityworks.domain;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

public class Organization {

    private String id;//-------------id
    private String name;
    private String website;
    private String brief;
    private String path;
    private String telephone;
    @ElementCollection//(fetch=FetchType.EAGER)
    private List types;
    private String color;
    private String ocode;
    private String pid;// --------------树形PID
    //获取菜单
    private List list=new ArrayList();//--------------树形
    private int show;

    /**
     * 对应oracle表中的id
     */
    private String orclid;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public String getPid() {
        return pid;
    }

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

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getWebsite() {
        return website;
    }

    public int getShow() {
        return this.show;
    }

    public void setShow(int show) {
        this.show = show;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getBrief() {
        return brief;
    }

    public void setBrief(String brief) {
        this.brief = brief;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Transient
    public List getTypes() {
        return types;
    }

    public void setTypes(List types) {
        this.types = types;
    }

    public String getOcode() {
        return ocode;
    }

    public void setOcode(String ocode) {
        this.ocode = ocode;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }



    public String getOrclid()
    {
        return orclid;
    }

    public void setOrclid(String orclid)
    {
        this.orclid = orclid;
    }

}

最终调用:data为树形结构

     List orgs = orgService.findAll();
      List data=new TreesUtils().data(orgs);
      return data;

若想使用TreesUtils,只需更换实体类即可

你可能感兴趣的:(Java)