자바 리스트 생 성 트 리(증강 판)

8014 단어 자바listtotree
더 읽 기
 
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 extends Node> buildListToTree(List extends Node> 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 extends Node> 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;
	}
}

 
 
 
 

좋은 웹페이지 즐겨찾기