For XXFor XX

作为一个高手
我习惯了孤独

权限菜单数据无限级遍历返回json结构数据

权限菜单数据无限级遍历返回json结构数据,我这里整理2种方法,一种循环方式的,一种递归方式的

循环方式遍历

        @Test
    public void test2(){

        Long time1 = System.currentTimeMillis();

        Map map1 = new HashMap();
        map1.put("id",1);
        map1.put("pid","");

        Map map2 = new HashMap();
        map2.put("id",2);
        map2.put("pid",1);
        Map map21 = new HashMap();
        map21.put("id",21);
        map21.put("pid",1);

        Map map3 = new HashMap();
        map3.put("id",3);
        map3.put("pid","");

        Map map4 = new HashMap();
        map4.put("id",4);
        map4.put("pid",2);

        Map map5 = new HashMap();
        map5.put("id",5);
        map5.put("pid",4);
        Map map6 = new HashMap();
        map6.put("id",6);
        map6.put("pid",3);
        Map map7 = new HashMap();
        map7.put("id",7);
        map7.put("pid","");

        List<Map> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        list.add(map21);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        list.add(map6);
        list.add(map7);

        //取得数据
        List<Map> resultMap = list;
        //定义一个Map集合 存储按指定顺序排列好的菜单
        Map<String, List<Map<Object,Object>>> temp = new HashMap<String, List<Map<Object,Object>>>();

        for (Map map : resultMap) {
            //如果temp的键包含这个parentid
            if(temp.containsKey(map.get("pid").toString())){
                //那么把所有相同parentid的数据全部添加到该parentid键下
                temp.get(map.get("pid").toString()).add(map);
            }else{
                //初始化temp  第一次用
                List<Map<Object,Object>> lists = new ArrayList<Map<Object,Object>>();
                lists.add(map);
                temp.put(map.get("pid").toString(), lists);
            }
        }

        //定义一个完整菜单列表
        ArrayList<Map<Object,Object>> array = new ArrayList<Map<Object,Object>>();
        for (Map hashMap : resultMap) {
            //如果temp中的键与当前id一致
            if(temp.containsKey(hashMap.get("id").toString())){
                //说明temp是当前id菜单的子菜单
                hashMap.put("children", temp.get(hashMap.get("id").toString()));
            }
            //遇到顶级菜单就添加进完整菜单列表
            if("".equals(hashMap.get("pid"))){
                array.add(hashMap);
            }

        }

        System.out.println(JSON.toJSONString(array));

        Long time2 = System.currentTimeMillis();

        System.out.println(time2-time1);

    }

递归方式遍历

    @Test
    public void test3(){
        Long time1 = System.currentTimeMillis();

        Map map1 = new HashMap();
        map1.put("id",1);
        map1.put("pid","");

        Map map2 = new HashMap();
        map2.put("id",2);
        map2.put("pid",1);
        Map map21 = new HashMap();
        map21.put("id",21);
        map21.put("pid",1);

        Map map3 = new HashMap();
        map3.put("id",3);
        map3.put("pid","");

        Map map4 = new HashMap();
        map4.put("id",4);
        map4.put("pid",2);

        Map map5 = new HashMap();
        map5.put("id",5);
        map5.put("pid",4);
        Map map6 = new HashMap();
        map6.put("id",6);
        map6.put("pid",3);
        Map map7 = new HashMap();
        map7.put("id",7);
        map7.put("pid","");

        List<Map> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        list.add(map21);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        list.add(map6);
        list.add(map7);

        List<Map> treeMenuList = this.treeMenuList(list,"");
        System.out.println(JSON.toJSONString(treeMenuList));

        Long time2 = System.currentTimeMillis();

        System.out.println(time2-time1);

    }

    public List<Map> treeMenuList(List<Map> menuList, String parentId) {
        List<Map> childMenu = new ArrayList<>();
        for (Map jsonMenu : menuList) {
            String menuId = jsonMenu.get("id")+"";
            String pid = jsonMenu.get("pid")+"";
            if (parentId.equals(pid)) {
                List<Map> c_node = treeMenuList(menuList, menuId);
                if (c_node.size()>0){
                    jsonMenu.put("childNode", c_node);
                }
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }

经过测试,循环的速度更快,递归的稍慢一些

转载请注明出处:For XX » 权限菜单数据无限级遍历返回json结构数据
标签:java 递归
分享到: 更多