数据利用map转换为树形结构
# 利用有序Map把一维数据转换为树结构的方法
# 转换为树结构的工具类:
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class AssmTreeUtil {
/**
* 组装树结构数据的方法
*/
public static List<TreeNode> assmTree(List<TreeNode> singleTreeNodes) {
// 判断排序数据是否为空
if(singleTreeNodes == null || singleTreeNodes.isEmpty()) {
return null;
}
// 用有序Map把传参组装起来
Map<String,TreeNode> nodeId2treeNodes = new LinkedHashMap<String,TreeNode>();
for(TreeNode node : singleTreeNodes){
TreeNode treeNode = new TreeNode();
treeNode.setNodeId(node.getNodeId());
treeNode.setNodeName(node.getNodeName());
treeNode.setPid(node.getPid());
nodeId2treeNodes.put(node.getNodeId(), treeNode);
}
// 用来保存组装好的数据, 作为返回值
List<TreeNode> topNodeTrees = new ArrayList<TreeNode>();
// 遍历所有节点
for(String nodeId : nodeId2treeNodes.keySet()){
TreeNode treeNode = nodeId2treeNodes.get(nodeId);
String pid = treeNode.getPid();
// 当前节点的父id为空 或 整个列表中没有以此父id作为自己的id的情况
// 即当前节点是根节点
if(pid==null || pid.length()==0 || !nodeId2treeNodes.containsKey(pid)){
// treeNode.setPid(""); // 此处可以根据需要调整父id的值
topNodeTrees.add(treeNode);
}else{ // 不是父节点, 作为子节点添加进去
TreeNode parentTreeNode = nodeId2treeNodes.get(pid);
if(parentTreeNode.getChildren()==null){
parentTreeNode.setChildren(new ArrayList<TreeNode>());
}
parentTreeNode.getChildren().add(treeNode);
}
}
return topNodeTrees;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# TreeNode bean类:
import java.util.List;
public class TreeNode {
/**
* 父节点ID
*/
private String pid;
/**
* 节点ID
*/
private String nodeId;
/**
* 节点名称
*/
private String nodeName;
/**
* 子节点
*/
private List<TreeNode> children;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 测试:
import java.util.ArrayList;
import java.util.List;
/**
* 测试组装树结构方法
* @Author: Rebecca Zhong
* @Description:
* @Date: Created in 2019/3/7 10:31
* @Modified By:
*/
public class Test {
public static void main(String[] args) {
List<TreeNode> singleNodes = new ArrayList<TreeNode>();
singleNodes.add(new TreeNode("a","1","节点1"));
singleNodes.add(new TreeNode("b","2","节点2"));
singleNodes.add(new TreeNode("c","3","节点3"));
singleNodes.add(new TreeNode("1","4","节点1-1"));
singleNodes.add(new TreeNode("2","5","节点2-1"));
singleNodes.add(new TreeNode("1","6","节点1-3"));
singleNodes.add(new TreeNode("1","7","节点1-2"));
singleNodes.add(new TreeNode("2","8","节点2-2"));
List<TreeNode> treeNodes = AssmTreeUtil.assmTree(singleNodes);
outputResult(treeNodes);
}
private static void outputResult(List<TreeNode> treeNodes) {
for (TreeNode treeNode : treeNodes) {
System.out.println("nodeId: " + treeNode.getNodeId() + "; pid: " + treeNode.getPid() + "; name: "+treeNode.getNodeName());
List<TreeNode> childrenNode = treeNode.getChildren();
if (childrenNode != null) {
outputResult(childrenNode);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 期望结果:
- 节点1
- 节点1-1
- 节点1-3
- 节点1-2
- 节点2
- 节点2-1
- 节点2-2
- 节点3
# 输出结果:
nodeId: 1; pid: a; name: 节点1
nodeId: 4; pid: 1; name: 节点1-1
nodeId: 6; pid: 1; name: 节点1-3
nodeId: 7; pid: 1; name: 节点1-2
nodeId: 2; pid: b; name: 节点2
nodeId: 5; pid: 2; name: 节点2-1
nodeId: 8; pid: 2; name: 节点2-2
nodeId: 3; pid: c; name: 节点3
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 小结:
利用ArrayList也可以实现相关功能,但代码整体较长,不利于维护。
利用LinkedHashMap代码更简洁一些。
上次更新: 2022/11/17, 13:20:04