JAVA递归 ,得到部门树


public DepartmentUserTreeNode getTree(String location,String  name) {
		List depts = deparDao.qryDepts();
		DepartmentUserTreeNode rootDept = deparDao.qryRootDept();
		
		
		recursion(depts, rootDept,0,location,name);
		
		return rootDept;
		
	}
	
	
    private int recursion(List list, DepartmentUserTreeNode node,int count,String location,String  name) {
        List childList = getChildList(list, node);// 得到子节点列表
        List emps = empDao.qryByDept(node.getDeptId(),location,name);
        count = CollectionUtils.isEmpty(emps)?0:emps.size();
        
        
        node.setEmps(emps);
      
        if (!CollectionUtils.isEmpty(childList)) {
          
           node.setDepts(childList);
         
            Iterator it = childList.iterator();
            while (it.hasNext()) {
            	DepartmentUserTreeNode n = (DepartmentUserTreeNode) it.next();
            	count = count+recursion(list, n,count,location,name);
            }
            
        } else {
        	node.setDepts(null);
        }
        
        node.setEmpCount(count);
        return count;
    }
    
    private List getChildList(List list, DepartmentUserTreeNode node) {
        List nodeList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
        	DepartmentUserTreeNode n = (DepartmentUserTreeNode) it.next();
            if (n.getParentId().equals(node.getDeptId()) ) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }

输入,LIST数据

JAVA递归 ,得到部门树,部门人员,以及,当前部门下所有的人员数(一直到底)。

 

结果数据格式:

 

最终展现:



 

你可能感兴趣的:(java)