服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java递归遍历树形结构

Java递归遍历树形结构

2020-04-09 14:36郝世海 JAVA教程

这篇文章主要介绍了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
//菜单树形结构
public JSONArray treeMenuList(JSONArray menuList, int parentId) {
JSONArray childMenu = new JSONArray();
for (Object object : menuList) {
JSONObject jsonMenu = JSONObject.fromObject(object);
int menuId = jsonMenu.getInt("id");
int pid = jsonMenu.getInt("parentId");
if (parentId == pid) {
JSONArray c_node = treeMenuList(menuList, menuId);
jsonMenu.put("childNode", c_node);
childMenu.add(jsonMenu);
}
}
return childMenu;
}
public static void main(String args[]) {
MenuCacheService menuCacheService = new MenuCacheService();
JSONArray jsonArray = new JSONArray();
Menu menu1 = new Menu();
menu1.setId(1l);
menu1.setParentId(0);
menu1.setLevel(0);
Menu menu2 = new Menu();
menu2.setId(2l);
menu2.setParentId(0);
menu2.setLevel(0);
Menu menu3 = new Menu();
menu3.setId(3l);
menu3.setParentId(2);
menu3.setLevel(1);
Menu menu4 = new Menu();
menu4.setId(4l);
menu4.setParentId(2);
menu4.setLevel(1);
Menu menu5 = new Menu();
menu5.setId(5l);
menu5.setParentId(4);
menu5.setLevel(2);
Menu menu6 = new Menu();
menu6.setId(6l);
menu6.setParentId(1);
menu6.setLevel(1);
jsonArray.add(menu1);
jsonArray.add(menu2);
jsonArray.add(menu3);
jsonArray.add(menu4);
jsonArray.add(menu5);
jsonArray.add(menu6);
System.out.print(menuCacheService.treeMenuList(jsonArray,0));
}

ps:java实现树的递归遍历(用于实现折叠菜单)

1.核心算法

?
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
package com.zf.tag.dao;
import java.util.ArrayList;
import java.util.List;
import com.zf.tag.entity.dept.Dept;
public class DeptDao extends BaseDao{
public static List<Dept> searchAllDept() throws Exception{
List<Object[]> listAllObject=new ArrayList<Object[]>();
List<Object[]> tmp=new ArrayList<Object[]>();
List<Dept> listAllDept=new ArrayList<Dept>();
String sql="select id,dname,up_did from tbl_dept";
listAllObject=getResult(sql);
for (Object[] row : listAllObject) {
Object[] ro = new Object[3];
ro[0] = row[0];
ro[1] = row[1];
ro[2] = row[2];
tmp.add(row);
}
for (Object[] row : tmp) {
if (null == row[2]) {
Dept d = new Dept();
d.setId(Integer.parseInt(String.valueOf(row[0])));
d.setDname(String.valueOf(row[1]));
listAllDept.add(d);
fomateDept(tmp, d);
}
}
return listAllDept;
}
private static void fomateDept(List<Object[]> tmp, Dept dept) {
for (Object[] row : tmp) {
if ((null != row[2])
&& Integer.parseInt(String.valueOf(row[2])) == dept.getId()) {
Dept d = new Dept();
d.setId(Integer.parseInt(String.valueOf(row[0])));
d.setDname(String.valueOf(row[1]));
List<Dept> list = dept.getChildren();
if (list == null) {
list = new ArrayList<Dept>();
}
list.add(d);
dept.setChildren(list);
fomateDept(tmp, d);
}
}
}
}

2.实体类(部门)

?
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
package com.zf.tag.entity.dept;
import java.util.List;
public class Dept {
private Integer id;
private String dname;
private List<Dept> children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public List<Dept> getChildren() {
return children;
}
public void setChildren(List<Dept> children) {
this.children = children;
}
public String toString(){
return this.getDname();
}
}

以上所述是小编给大家介绍的Java递归遍历树形结构的相关内容,希望对大家有所帮助!

延伸 · 阅读

精彩推荐