@Override public List<Map<String, Object>> queryListTree() { List<Map<String,Object>> treeList = new ArrayList<>(); //结果list List<Map<String,Object>> list = reportDao.queryListTree(new HashMap<>()); //查出所有数据list(避免多次连接数据库) for (Map<String, Object> report : list) { if("-1".equals(report.get("rptPid")+"")){ //根节点判断 //userTree.remove(user); ArrayList<Map<String,Object>> newList = new ArrayList<>();//list遍历过程中不允许remove操作,所以新new一个list newList.addAll(list); newList.remove(report);//移除本次节点,减少递归次数 Map<String,Object> userMap = TreeList(report,newList);//递归方法 treeList.add(userMap); } } return treeList; } /** * 递归拼装子节点 * @Title: TreeList * @param @param user * @param @param userTree * @param @return * @return Map<String,Object> */ private Map<String,Object> TreeList(Map<String,Object> report,List<Map<String,Object>> newList) { List<Map<String,Object>> childreList = new ArrayList<>();//子节点list for(Map<String,Object> newReport : newList){ if(report.get("id").equals(newReport.get("rptPid"))){//判断是否当前节点的子节点 String level = newReport.get("rptLevel")+"";//0:文件夹 1:模板 //userTree.remove(userC); if("1".equals(level)){ //叶子节点 -- 不用递归 childreList.add(newReport); continue; } //新的list 模拟remove当前节点 ArrayList<Map<String,Object>> newList2 = new ArrayList<>(); newList2.addAll(newList); newList2.remove(newReport); //递归 Map<String, Object> userL = TreeList(newReport, newList2); childreList.add(userL); } } if("0".equals(report.get("rptLevel")+"")){//父节点 report.put("children", childreList); //解决父节点无孩子是图标显示为叶子问题 if(childreList.isEmpty()) report.put("state", "closed"); } return report; }
数据