递归遍历无限极节点树

1、查询出所有的根节点:
public List getGroupTree() {
List list=new ArrayList();
List groupList=groupDao.getGroupList("/");
for (Group group : groupList) {
list.add(getChildrenList(group));
}
return list;
}
2、遍历某根节点的所有子节点:
public Map getChildrenList(Group group){
if (group==null) {
return null;
}
Map map=new HashMap();
String path=group.getPath();
String idPath="/"+group.getId();
if (!"/".equals(path)) {
idPath+=group.getPath();
}
map.put("id", idPath);
map.put("text", group.getName());
List groupList=groupDao.getGroupList(idPath);
if (groupList==null||groupList.size()<=0) {// 没有子节点了
return map;
}
map.put("state", "closed");
List> childrenList=new ArrayList>();
for (Group group2 : groupList) {
childrenList.add(getChildrenList(group2));
}
map.put("children", childrenList);
return map;
}

你可能感兴趣的:(研发管理)