应用场景 : 有的时候 , 前端需要展示出一个分类以及这个分类下的子分类 , 一层一层形成树结构
定义节点
首先定义每一个类型为一个节点 , 定义它的必要属性
package com.starw.cloudy.utils.treeTools;
import java.util.ArrayList;
import java.util.List;
/**
* 节点
*/
public class Node {
//节点的唯一id,此处可以对应数据库中的主键,根据实际情况可以修改
private int id;
//节点需要展示的名称
private String name;
//顶级节点没有父id, 因此允许null的存在, 要用Integer
private Integer parentId;
//是否展开树内容
private int expand;
//节点的子节点集合
private List<Node> children;
public Node(int id, String name, Integer parentId, int expand) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.expand = expand;
this.children = new ArrayList<>();
}
public Node(int id, String name, Integer parentId, int expand, List<Node> children) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.expand = expand;
this.children = children;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public int getExpand() {
return expand;
}
public void setExpand(int expand) {
this.expand = expand;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
@Override
public String toString() {
return "Node{" +
"id=" + id +
", name='" + name + '\'' +
", parentId=" + parentId +
", expand=" + expand +
", children=" + children +
'}';
}
}
建树工具
package com.starw.cloudy.utils.treeTools;
import com.alibaba.fastjson.JSONArray;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by dell on 2017/12/22.
*/
public class TreeUtil {
public static JSONArray buildTree(List<Node> nodes) {
JSONArray result = new JSONArray();
Iterator<Node> iterator = nodes.iterator();
while (iterator.hasNext()) {
Node node = iterator.next();
if (node.getParentId() == null) {//没有父id, 为顶级父类,要从顶级父类开始
//将此node和所有的node传入
buildNode(node, nodes);
//递归完毕,此处是一个顶级节点下的所有子节点,装入结果集
result.add(node);
}
}
return result;
}
private static Node buildNode(Node parent, List<Node> nodes) {
Iterator<Node> iterator = nodes.iterator();
while (iterator.hasNext()) {
Node node = iterator.next();
if (node.getParentId() != null && node.getParentId().intValue() == parent.getId()) {
parent.getChildren().add(node);
//此处递归,一直查找此子节点
buildNode(node, nodes);
}
}
return parent;
}
public static void main(String[] args) {
//测试
List<Node> nodes = new ArrayList<>();
//id name parentId expend children
Node a = new Node(1, "A", null, 0, new ArrayList<>());
Node b = new Node(2, "B", 1, 0, new ArrayList<>());
Node c = new Node(3, "C", 1, 0, new ArrayList<>());
Node d = new Node(4, "D", 3, 0, new ArrayList<>());
Node e = new Node(5, "E", 3, 0, new ArrayList<>());
Node f = new Node(6, "F", 5, 0, new ArrayList<>());
Node g = new Node(7, "G", null, 0, new ArrayList<>());
nodes.add(a);
nodes.add(b);
nodes.add(c);
nodes.add(d);
nodes.add(e);
nodes.add(f);
nodes.add(g);
System.out.println(buildTree(nodes));
}
}
测试结果
[{"children":[{"children":[],"expand":0,"id":2,"name":"B","parentId":1},{"children":[{"children":[],"expand":0,"id":4,"name":"D","parentId":3},{"children":[{"children":[],"expand":0,"id":6,"name":"F","parentId":5}],"expand":0,"id":5,"name":"E","parentId":3}],"expand":0,"id":3,"name":"C","parentId":1}],"expand":0,"id":1,"name":"A"},{"children":[],"expand":0,"id":7,"name":"G"}]