Java 实现无限层级树形结构遍历部门

代码如下:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* 部门实体类
*/
public class Department {

public Department(Integer id, String name, Integer parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}

private Integer id;
private String name;

/**
* 上级部门 id
*/
private Integer parentId;

public Integer getId() {
return id;
}

public void setId(Integer 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 class TreeNode {

public TreeNode(Integer id, String name, Integer parentId, List<TreeNode> children) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.children = children;
}

private Integer id;
private String name;

/**
* 父级节点 id
*/
private Integer parentId;

/**
* 子级节点
*/
private List<TreeNode> children;

public Integer getId() {
return id;
}

public void setId(Integer 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 List<TreeNode> getChildren() {
return children;
}

public void setChildren(List<TreeNode> children) {
this.children = children;
}
}

/**
* 循环遍历出子节点
* @param parentId
* @param treeNodeList
* @return
*/
public List<TreeNode> forEachChildrenTreeNode(int parentId, List<TreeNode> treeNodeList) {
List<TreeNode> children = new ArrayList<>(0);
for(TreeNode treeNode : treeNodeList) {
if(treeNode.getParentId() != null && treeNode.getParentId().equals(parentId)) {
children.add(treeNode);
}
}

for(TreeNode child : children) {
List<TreeNode> childChildren = forEachChildrenTreeNode(child.getId(), treeNodeList);
if(childChildren == null) {
childChildren = new ArrayList<>(0);
}
child.setChildren(childChildren);
}

if(children.size() == 0) {
return null;
}
return children;
}

/**
* 测试代码
*/
public void testApp() {

List<Department> departmentList = new ArrayList<>(6);
departmentList.add(new Department(1,"测试集团公司1", null));
departmentList.add(new Department(2,"测试分公司1", 1));
departmentList.add(new Department(3,"测试分公司2", 1));
departmentList.add(new Department(4,"测试子部门1", 3));
departmentList.add(new Department(5,"测试项目室1", 4));
departmentList.add(new Department(6,"测试项目组1", 5));
departmentList.add(new Department(7,"测试项目室2", 4));
departmentList.add(new Department(8,"测试子部门2", 3));
departmentList.add(new Department(9,"测试分公司3", 1));
departmentList.add(new Department(10,"测试集团公司2", null));

/**
* 将部门数据转成树形节点
*/
List<TreeNode> treeNodeList = new ArrayList<>(10);
for(Department department : departmentList) {
treeNodeList.add(new TreeNode(department.getId(), department.getName(), department.getParentId(), new ArrayList<>()));
}

/**
* 最终输出的树形结构类结果数据
*/
List<TreeNode> treeNodeResultList = new ArrayList<>(0);

/**
* 将 parentId 为空的顶层节点首先写入结果数据
*/
for(TreeNode treeNode : treeNodeList) {
if(treeNode.getParentId() == null) {
treeNodeResultList.add(treeNode);
}
}

/**
* 循环所有在结果数据中的顶层节点,并将子级节点装载进顶层节点的 children 属性
*/
for(TreeNode treeNode : treeNodeResultList) {
treeNode.setChildren(forEachChildrenTreeNode(treeNode.getId(), treeNodeList));
}

/**
* 将结果数据以 JSON 格式输出
*/
ObjectMapper objectMapper = new ObjectMapper();
try {
System.out.println(objectMapper.writeValueAsString(treeNodeResultList));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}

输出结果如下图: