node.java:
import java.util.List;
public class Node {
private static final long serialVersionUID = -2721191232926604726L;
private int id;
private int parentId;
private Node parent;
private List<Node> children;
private String name;
private int level;
private int sort;
private int rootId;
private String type;
private boolean isLeaf;
private String description;
public Node() {
super();
}
public Node(int id, int parentId, String name) {
super();
this.id = id;
this.parentId = parentId;
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getType() {
return type;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public void setType(String type) {
this.type = type;
}
public boolean isLeaf() {
return isLeaf;
}
public void setLeaf(boolean isLeaf) {
this.isLeaf = isLeaf;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public int getRootId() {
return rootId;
}
public void setRootId(int rootId) {
this.rootId = rootId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + parentId;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (id != other.id)
return false;
if (parentId != other.parentId)
return false;
return true;
}
@Override
public String toString() {
return "Node {id=" + id + ", parentId=" + parentId + ", children="
+ children + ", name=" + name + ", level =" + level + "}";
}
}
Test.java:
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class Test {
@SuppressWarnings("unchecked")
private List<Node> buildListToTree(List<Node> dirs) {
List<Node> roots = findRoots(dirs);
List<Node> notRoots = (List<Node>) CollectionUtils
.subtract(dirs, roots);
for (Node root : roots) {
root.setChildren(findChildren(root, notRoots));
}
return roots;
}
public List<Node> findRoots(List<Node> allNodes) {
List<Node> results = new ArrayList<Node>();
for (Node node : allNodes) {
boolean isRoot = true;
for (Node comparedOne : allNodes) {
if (node.getParentId() == comparedOne.getId()) {
isRoot = false;
break;
}
}
if (isRoot) {
node.setLevel(0);
results.add(node);
node.setRootId(node.getId());
}
}
return results;
}
@SuppressWarnings("unchecked")
private List<Node> findChildren(Node root, List<Node> allNodes) {
List<Node> children = new ArrayList<Node>();
for (Node comparedOne : allNodes) {
if (comparedOne.getParentId() == root.getId()) {
comparedOne.setParent(root);
comparedOne.setLevel(root.getLevel() + 1);
children.add(comparedOne);
}
}
List<Node> notChildren = (List<Node>) CollectionUtils.subtract(allNodes, children);
for (Node child : children) {
List<Node> tmpChildren = findChildren(child, notChildren);
if (tmpChildren == null || tmpChildren.size() < 1) {
child.setLeaf(true);
} else {
child.setLeaf(false);
}
child.setChildren(tmpChildren);
}
return children;
}
public static void main(String[] args) {
Test tb = new Test();
List<Node> allNodes = new ArrayList<Node>();
allNodes.add(new Node(1, 0, "节点1"));
allNodes.add(new Node(2, 0, "节点2"));
allNodes.add(new Node(3, 0, "节点3"));
allNodes.add(new Node(11, 7, "节点11"));
allNodes.add(new Node(4, 1, "节点4"));
allNodes.add(new Node(5, 1, "节点5"));
allNodes.add(new Node(6, 1, "节点6"));
allNodes.add(new Node(7, 4, "节点7"));
allNodes.add(new Node(8, 4, "节点8"));
allNodes.add(new Node(9, 5, "节点9"));
allNodes.add(new Node(10, 100, "节点10"));
List<Node> roots = tb.buildListToTree(allNodes);
JSONArray array = new JSONArray();
for (Node node : roots) {
System.out.println(node.getName());
JSONObject o = new JSONObject();
o.put("id", node.getId());
o.put("name", node.getName());
List<Node> child = node.getChildren();
array.add(o);
if(child!=null && child.size() > 0){
tb.fillChildren(o, child);
}
}
System.out.println(array);
}
private void fillChildren(JSONObject o, List<Node> childs) {
JSONArray array = new JSONArray();
for (Node node : childs) {
JSONObject oo = new JSONObject();
List<Node> child = node.getChildren();
oo.put("id", node.getId());
oo.put("name", node.getName());
System.out.println(node.getName());
array.add(oo);
if(child!=null && child.size() > 0){
fillChildren(oo, child);
}
}
o.put("child", array);
}
}