java List 转 Tree结构

来源:互联网 发布:淘宝天猫优惠券软件 编辑:程序博客网 时间:2024/05/17 08:32
package com.cnjy.ecampus.comm.tree.multi;


import java.util.HashMap;
import java.util.List;
import java.util.Map; 
import com.google.common.collect.Lists;


public class TreeUtils {

/**
* 构建树结构
* @param nodes
* @param sort
* @return
*/
public static<T> Node<T> build(List<Node<T>> nodes,boolean sort){

HashMap<String, Node<T>> nodeMap = new HashMap<String, Node<T>>();
for(Node<T> node:nodes){
nodeMap.put(node.getId(), node);
}

List<Node<T> > roots = Lists.newArrayList();
for(Map.Entry<String, Node<T>> nodeEntry:nodeMap.entrySet()){
Node<T> node = nodeEntry.getValue();
if (node.getParentId() == null || node.getParentId().equals("")) {
roots.add(node);
} else {
Node<T> parent = nodeMap.get(node.getParentId());
if(parent!=null)
parent.addChild(node);
}
}

Node<T> root = new Node<T>(null, "root", null, 0);
for(Node<T> n:roots){
root.addChild(n);
}
if(sort)
root.sortChildren();
return root;
}

/**
* 测试方法
* @param args
*/
public static void main(String[] args) {
List<Node<Object>> nodes = Lists.newArrayList();

Node<Object> node1 = new Node<Object>("2", "根节点", null, 1);
Node<Object> node8 = new Node<Object>("1", "根节点", null, 2);
Node<Object> node2 = new Node<Object>("11", "根节点", "1", 3);
Node<Object> node3 = new Node<Object>("12", "根节点", "2", 4);
Node<Object> node4 = new Node<Object>("111", "根节点", "11", 5);
Node<Object> node5 = new Node<Object>("121", "根节点", "12", 7);
Node<Object> node6 = new Node<Object>("122", "根节点", "12", 6);
Node<Object> node7 = new Node<Object>("1221", "根节点", "122", 8);

nodes.add(node7);
nodes.add(node2);
nodes.add(node3);
nodes.add(node4);
nodes.add(node5);
nodes.add(node6);
nodes.add(node1);
nodes.add(node8);

//TreeBuild<Object> treeUtils = new TreeBuild<Object>();
Node<Object> root = TreeUtils.<Object>build(nodes, true);

System.out.println(root.getText());

}


}


package com.cnjy.ecampus.comm.tree.multi;


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


public class Node<T> {


/**
* 节点编号
*/
private String id;
/**
* 节点内容
*/
private String text;
/**
* 父节点编号
*/
private String parentId;


/**
* 排序号
*/
private Integer sort; 

private boolean expand = false;

private boolean selected = false;

/**
* 自身对象
*/
private T obj;


/**
* 孩子节点列表
*/
private List<Node<?>> children = new ArrayList<Node<?>>();


public Node() {
}


public Node(String id, String text, String parentId, Integer sort) {
this.id = id;
this.text = text;
this.parentId = parentId;
this.sort = sort;
}

public Node(String id, String text, String parentId, Integer sort,T obj) {
this.id = id;
this.text = text;
this.parentId = parentId;
this.sort = sort;
this.obj = obj;
}


public String getId() {
return id;
}


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


public String getText() {
return text;
}


public void setText(String text) {
this.text = text;
}


public String getParentId() {
return parentId;
}


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


public Integer getSort() {
return sort;
}


public void setSort(Integer sort) {
this.sort = sort;
}


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


// 添加孩子节点
public void addChild(Node<?> node) {
this.children.add(node);
}


public void sortChildren() {
if (this.children != null && !this.children.isEmpty()) {
Collections.sort(this.children, new NodeSortComparator());
for (Node<?> child : this.children)
child.sortChildren();
}
}


public T getObj() {
return obj;
}


public void setObj(T obj) {
this.obj = obj;



public boolean isExpand() {
return expand;
}


public void setExpand(boolean expand) {
this.expand = expand;
}


public boolean isSelected() {
return selected;
}


public void setSelected(boolean selected) {
this.selected = selected;
}


}


package com.cnjy.ecampus.comm.tree.multi;


import java.util.Comparator;


@SuppressWarnings("rawtypes")
public class NodeSortComparator implements Comparator<Node> {


@Override
public int compare(Node node1, Node node2) {
Integer sort1 = node1.getSort();
Integer sort2 = node2.getSort();
return sort1 > sort2 ? 1 : (sort1 == sort2 ? 0 : -1);
}


}

原创粉丝点击