1. 树结构的节点,设置children属性,可嵌套
package com.jiangxb.test.util.tree;
import java.util.ArrayList;
import java.util.List;
/**
* @author jiangxiangbo
* @date 2020/8/19
* @Description: 树结构实体
*/
public class TreeItem {
private String id;
private String parentId;
private String name;
private List<TreeItem> children = new ArrayList<>();
public TreeItem(String id, String parentId, String name) {
this.id = id;
this.parentId = parentId;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<TreeItem> getChildren() {
return children;
}
public void setChildren(List<TreeItem> children) {
this.children = children;
}
}
2. 生成树的工具类,需要转化的实体要先转为一个个的TreeItem放入List中作为参数。可在实体类中添加toTreeItem方法返回一个TreeItem。
package com.jiangxb.test.util.tree;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author jiangxiangbo
* @date 2020/8/19
* @Description: 生成树结构的工具类(DCL单例)
*/
public class TreeUtil {
private volatile static TreeUtil INSTANCE;
// 构造方法私有化
private TreeUtil() {
}
// 获取树工具单例 (DCL单例)
public static TreeUtil getInstance() {
if (INSTANCE == null) {
synchronized (TreeUtil.class) {
if (INSTANCE == null) {
INSTANCE = new TreeUtil();
}
}
}
return INSTANCE;
}
public TreeItem enquireTree(List<TreeItem> treeItemList) {
if (treeItemList.isEmpty()) {
return null;
}
// 过滤空对象
List<TreeItem> treeItems = treeItemList.stream().filter(treeItem -> treeItem != null).collect(Collectors.toList());
// 存储 id treeItem
HashMap<String, TreeItem> itemMap = new HashMap<>();
treeItems.forEach(treeItem -> {
itemMap.put(treeItem.getId(), treeItem);
});
// 声明一个变量存放根节点
TreeItem root = null;
// 数据组装
for (TreeItem treeItem : treeItems) {
String pid = treeItem.getParentId();
if (pid.isEmpty() || pid == "0") {
// 说明该节点为根节点
root = treeItem;
continue;
}
TreeItem parent = itemMap.get(pid);
parent.getChildren().add(treeItem);
}
return root;
}
}
3. 测试效果:
package com.jiangxb.test.util.tree;
import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import java.util.List;
/**
* @author jiangxiangbo
* @date 2020/8/19
* @Description: TreeItem测试
*/
public class TreeTest {
public static void main(String[] args) {
List<TreeItem> treeItemList = Arrays.asList(
new TreeItem("1", "0", "中国"),
new TreeItem("10", "1", "河北"),
new TreeItem("100", "10", "北京"),
new TreeItem("110", "10", "天津"),
new TreeItem("20", "1", "河南"),
new TreeItem("200", "20", "郑州"),
new TreeItem("210", "20", "洛阳"),
new TreeItem("220", "20", "许昌"),
new TreeItem("221", "220", "禹州")
);
TreeItem treeItem = TreeUtil.getInstance().enquireTree(treeItemList);
System.out.println(JSONObject.toJSONString(treeItem));
}
}
4. 输出(格式化后):
{
"children": [
{
"children": [
{
"children": [
],
"name": "北京",
"id": "100",
"parentId": "10"
},
{
"children": [
],
"name": "天津",
"id": "110",
"parentId": "10"
}
],
"name": "河北",
"id": "10",
"parentId": "1"
},
{
"children": [
{
"children": [
],
"name": "郑州",
"id": "200",
"parentId": "20"
},
{
"children": [
],
"name": "洛阳",
"id": "210",
"parentId": "20"
},
{
"children": [
{
"children": [
],
"name": "禹州",
"id": "221",
"parentId": "220"
}
],
"name": "许昌",
"id": "220",
"parentId": "20"
}
],
"name": "河南",
"id": "20",
"parentId": "1"
}
],
"name": "中国",
"id": "1",
"parentId": "0"
}