java List 转 Tree结构
来源:互联网 发布:淘宝天猫优惠券软件 编辑:程序博客网 时间:2024/05/17 08:32
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);
}
}
- java List 转 Tree结构
- java list 转 tree,加排序
- 简单list转化为tree结构
- java tree 结构递归查询
- Tree结构数据拆分成List集合算法
- List数据按照树状结构(tree)进行排序
- 简单的list查询转换为tree结构
- Flatten Binary Tree to Linked List:二叉树变更结构
- Flatten Binary Tree to Linked List (Java)
- java 生成 树形结构数据(tree)
- tree list
- json集合转tree结构
- java中的List结构的集合类
- Java List Map Set 常用结构比较
- tree结构
- List转树形数据,供tree使用
- [Leetcode] Convert Sorted List to Binary Search Tree (Java)
- [Leetcode] Flatten Binary Tree to Linked List (Java)
- 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
- python+TensorFlow常见错误汇总,持续更新中......
- JavaWeb项目excel文件导入
- 稀疏矩阵的三元组存储和逆置
- Linux内核ARM构架中原子变量的底层实现研究
- java List 转 Tree结构
- UE4 树木积雪效果
- C#——构造函数
- 算法训练 友好数
- 什么是单例设计模式?
- app下载判断手机是安卓还是苹果
- 用SpannableString打造绚丽多彩的文本显示效果
- 2017上海书展暨“书香中国”上海周会刊(参展商名录)
- 计算文本相似度方法大全-简单说