JAVA树形结构工具类

2019-1-3

片段 1片段 2片段 3


BaseTreeObj.java

/**
 * 基层树形结构实体类
 * 必备属性:id,parentId,childsList
 */
@Data
public class BaseTreeObj implements Serializable {
	private static final long serialVersionUID = 1L;
	private String id;
	private String parentId;
	private String name;
	private List<BaseTreeObj> childsList = new ArrayList<>();
}


TreeUtil.java

package com.soyea.tree;

import org.springframework.util.CollectionUtils;

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

/**
 * 获得树形结构的工具类
 * 实际使用,可将BaseTreeObj直接覆盖为指定类
 */
public class TreeUtil {

	/**
	 * 获得指定节点下所有归档
	 * @param list
	 * @param parentId
	 * @return
	 */
	public static List<BaseTreeObj> list2TreeConverter(List<BaseTreeObj> list, String parentId) {
		List<BaseTreeObj> returnList = new ArrayList<>();

		for (BaseTreeObj res : list) {
			//判断对象是否为根节点
			if (res.getParentId().equals(parentId)) {
				//该节点为根节点,开始递归
				recursionFn(list, res); //通过递归为节点设置childList

				returnList.add(res);
			}
		}

		return returnList;
	}

	/**
	 * 递归列表
	 * 通过递归,给指定t节点设置childList
	 * @param list
	 * @param t
	 */
	public static void recursionFn(List<BaseTreeObj> list, BaseTreeObj t) {
		//只能获取当前t节点的子节点集,并不是所有子节点集
		List<BaseTreeObj> childsList = getChildList(list, t);
		//设置他的子集对象集
		t.setChildsList(childsList);

		//迭代子集对象集
		for (BaseTreeObj nextChild : childsList) { //遍历完,则退出递归

			//判断子集对象是否还有子节点
			if (!CollectionUtils.isEmpty(childsList)) {
				//有下一个子节点,继续递归
				recursionFn(list, nextChild);
			}
		}
	}	/**
	 * 获得指定节点下的所有子节点
	 * @param list
	 * @param t
	 * @return
	 */
	public static List<BaseTreeObj> getChildList(List<BaseTreeObj> list, BaseTreeObj t) {
		List<BaseTreeObj> childsList = new ArrayList<BaseTreeObj>();
		//遍历集合元素,如果元素的Parentid==指定元素的id,则说明是该元素的子节点
		for (BaseTreeObj t1 : list) {
			if (t1.getParentId() .equals(t.getId()) ) {
				childsList.add(t1);
			}
		}

		return childsList;
	}	/**
	 * 判断是否还有下一个子节点
	 * @param list
	 * @param t
	 */
	public static boolean hasChild(List<BaseTreeObj> list, BaseTreeObj t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

}


树形工具类

package com.github.wxiaoqi.security.common.util;

import com.github.wxiaoqi.security.common.vo.TreeNode;

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

/**
 * Created by Ace on 2017/6/12.
 */
public class TreeUtil{
  /**
   * 两层循环实现建树
   * 
   * @param treeNodes 传入的树节点列表
   * @return
   */
  public static <T extends TreeNode> List<T> bulid(List<T> treeNodes,Object root) {

    List<T> trees = new ArrayList<T>();

    for (T treeNode : treeNodes) {

      if (root.equals(treeNode.getParentId())) {
        trees.add(treeNode);
      }

      for (T it : treeNodes) {
        if (it.getParentId() == treeNode.getId()) {
          if (treeNode.getChildren() == null) {
            treeNode.setChildren(new ArrayList<TreeNode>());
          }
          treeNode.add(it);
        }
      }
    }
    return trees;
  }

  /**
   * 使用递归方法建树
   * 
   * @param treeNodes
   * @return
   */
  public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes,Object root) {
    List<T> trees = new ArrayList<T>();
    for (T treeNode : treeNodes) {
      if (root.equals(treeNode.getParentId())) {
        trees.add(findChildren(treeNode, treeNodes));
      }
    }
    return trees;
  }

  /**
   * 递归查找子节点
   * 
   * @param treeNodes
   * @return
   */
  public static <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
    for (T it : treeNodes) {
      if (treeNode.getId() == it.getParentId()) {
        if (treeNode.getChildren() == null) {
          treeNode.setChildren(new ArrayList<TreeNode>());
        }
        treeNode.add(findChildren(it, treeNodes));
      }
    }
    return treeNode;
  }

}package com.github.wxiaoqi.security.common.vo;

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

/**
 * Created by Ace on 2017/6/12.
 */
public class TreeNode {
    protected int id;
    protected int parentId;
    List<TreeNode> children = new ArrayList<TreeNode>();

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }
    public int getId() {
        return id;
    }

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

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public void add(TreeNode node){
        children.add(node);
    }
}

下载地址

百度网盘
密码: