Java List 生成 树(增强版)

 

Java List 生成 树:http://ysj5125094.iteye.com/blog/2283159

 

maven pom.xml


	commons-collections
	commons-collections
	3.2.1

 

TreeBuilder.java

package com.yusj.util.tree;

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

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import lac.framework.support.dictionary.domain.Dictionary;

public class TreeBuilder {
	@SuppressWarnings("unchecked")
	public List buildListToTree(List dirs) {
		List roots = findRoots(dirs);
		List notRoots = (List) CollectionUtils.subtract(dirs, roots);
		for (Node root : roots) {
			root.setChildren(findChildren(root, notRoots));
		}
		return roots;
	}

	private List findRoots(List allNodes) {
		List results = new ArrayList();
		for (Node node : allNodes) {
			boolean isRoot = true;
			for (Node comparedOne : allNodes) {
				if (StringUtils.isNotBlank(node.getParentId()) && node.getParentId().equals(comparedOne.getId())) {
					isRoot = false;
					break;
				}
			}
			if (isRoot) {
				node.setLevel(0);
				results.add(node);
				node.setRootId(node.getId());
			}
		}
		return results;
	}

	@SuppressWarnings("unchecked")
	private List findChildren(Node root, List allNodes) {
		List children = new ArrayList();

		for (Node comparedOne : allNodes) {
			if (StringUtils.isNotBlank(comparedOne.getParentId()) && comparedOne.getParentId().equals(root.getId())) {
				comparedOne.setParent(root);
				comparedOne.setLevel(root.getLevel() + 1);
				children.add(comparedOne);
			}
		}
		List notChildren = (List) CollectionUtils.subtract(allNodes, children);
		for (Node child : children) {
			List tmpChildren = findChildren(child, notChildren);
			if (tmpChildren == null || tmpChildren.size() < 1) {
				child.setLeaf(true);
			} else {
				child.setLeaf(false);
			}
			child.setChildren(tmpChildren);
		}
		return children;
	}

	private List getLeafChildren(List resultList, List children) {
		for (Node node : children) {
			if (node.isLeaf()) {
				resultList.add(node);
			} else {
				getLeafChildren(resultList, node.getChildren());
			}
		}
		return resultList;
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		TreeBuilder tb = new TreeBuilder();
		List allNodes = new ArrayList();

		allNodes.add(new Dictionary("1", "0", "001", "节点001", 0));
		allNodes.add(new Dictionary("2", "0", "002", "节点002", 0));
		allNodes.add(new Dictionary("3", "0", "003", "节点003", 0));
		allNodes.add(new Dictionary("4", "1", "004", "节点004", 0));
		allNodes.add(new Dictionary("5", "1", "005", "节点005", 0));
		allNodes.add(new Dictionary("6", "1", "006", "节点006", 0));
		allNodes.add(new Dictionary("7", "4", "007", "节点007", 0));
		allNodes.add(new Dictionary("8", "4", "008", "节点008", 0));
		allNodes.add(new Dictionary("9", "5", "009", "节点009", 0));
		allNodes.add(new Dictionary("10", "5", "010", "节点010", 0));
		allNodes.add(new Dictionary("11", "7", "011", "节点011", 0));
		allNodes.add(new Dictionary("12", "7", "012", "节点012", 0));

		// 显示所有节点
		List roots = (List) tb.buildListToTree(allNodes);
		for (Node n : roots) {
			System.out.println(JSON.toJSONString(n));
		}

		// 查找所有子节点
		List children = tb.findChildren(new Dictionary("1", "0"), allNodes);
		for (Node n : children) {
			System.out.println(JSON.toJSONString(n));
		}
		// 查找所有叶子节点
		System.out.println("------------------");
		List resultList = tb.getLeafChildren(new ArrayList(), children);
		for (Node n : resultList) {
			System.out.println(JSON.toJSONString(n));
		}
	}
}

 

Node.java(待转换的bean必须继承该类)

package com.yusj.util.tree;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lac.framework.core.entity.IdEntity;

public class Node extends IdEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8875995344582620331L;
	private String parentId;
	private Node parent;
	private List children;
	private int level;
	private String rootId;
	private boolean leaf;
	
	public Node(){}
	public Node(String id, String parentId){
		this.setId(id);
		this.parentId = parentId;
	}
	
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	@JsonIgnore
	public Node getParent() {
		return parent;
	}
	public void setParent(Node parent) {
		this.parent = parent;
	}
	public List getChildren() {
		return children;
	}
	public void setChildren(List children) {
		this.children = children;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public String getRootId() {
		return rootId;
	}
	public void setRootId(String rootId) {
		this.rootId = rootId;
	}
	public boolean isLeaf() {
		return leaf;
	}
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}
}

 

Dictionary.java

package com.yusj.support.dictionary.domain;

import com.yusj.util.tree.Node;

public class Dictionary extends Node{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8875995344582620331L;
	private String code;
	private String label;
	private Integer sort;
	
	public Dictionary(){}
	public Dictionary(String id, String parentId){
		super(id, parentId);
	}
	public Dictionary(String id, String parentId, String code, String label, Integer sort){
		super(id, parentId);
		this.code = code;
		this.label = label;
		this.sort = sort;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
}

 

 

 

 

 

你可能感兴趣的:(java)