java递归树

    @Override
    public List<Map<String, Object>> findDicById(Map<String, Object> map) {
        List<Map<String, Object>> tree = new ArrayList<>();
        map.put("pid","0");
        List<Map<String,Object>> list = riskDao.findDicById(map);
        for(Map<String,Object> map1:list){
            map.put("pid",map1.get("id").toString());
            Map<String,Object> map2 = new HashMap<>();
            map2.put("label",map1.get("dname").toString());
            map2.put("id",map1.get("id").toString());
            map2.put("children",this.findTree(map));
            tree.add(map2);
        }
        return tree;
    }

    public List<Map<String,Object>> findTree(Map<String,Object> map){
        List<Map<String,Object>> list = riskDao.findDicById(map);
        List<Map<String, Object>> tree = new ArrayList<>();
        for(Map<String,Object> map1:list){
            Map<String,Object> map2 = new HashMap<>();
            map2.put("label",map1.get("dname").toString());
            map2.put("id",map1.get("id").toString());
            map.put("pid",map1.get("id").toString());
            map2.put("children",findTree(map));
            tree.add(map2);
        }
        return tree;
    }

 

posted @ 2021-06-25 10:42  _Lawrence  阅读(257)  评论(0编辑  收藏  举报