Java递归遍历树形结构的实现代码
废话不多说了,直接给大家贴代码,具体代码如下所示:
//菜单树形结构
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.核心算法
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.实体类(部门)
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递归遍历树形结构的相关内容,希望对大家有所帮助!
更多精彩内容请关注公众号【Java技术迷】,可以通过以下二维码关注
以上是 Java递归遍历树形结构的实现代码 的全部内容, 来源链接: utcz.com/z/340396.html