zoukankan      html  css  js  c++  java
  • 递归拼装Tree结构数据

        @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;
        }

    数据

  • 相关阅读:
    Servlet3.0-使用注解定义Servlet
    poj 1256 Anagram—next_permutation的神奇应用
    poj 1664 放苹果 (划分数)
    poj 1011 Sticks
    poj和hdu部分基础算法分类及难度排序
    Notepad++支持jQuery、html5、css3
    Codeforces Round #395 (Div. 2) C. Timofey and a tree
    Codeforces Round #390 (Div. 2) D. Fedor and coupons
    bazel-编译动态库
    bazel-编译多目标
  • 原文地址:https://www.cnblogs.com/caoyajun33-blog/p/7886432.html
Copyright © 2011-2022 走看看