Java 查询数据后进行递归操作

java的递归方法记录:

 

private List> generateOrgMapToTree(List> orgMaps, Integer pid) {
        if (null == orgMaps || orgMaps.size() == 0) {
            List list = statusResponseCodeRepository.findAll();
            String json_list = JSONObject.toJSONString(list);
            orgMaps = (List>) JSONObject.parse(json_list);
        }
        List> orgList = new ArrayList<>();
        if (orgMaps != null && orgMaps.size() > 0) {
            for (Map item : orgMaps) {
                //比较传入pid与当前对象pid是否相等
                if (pid.equals(item.get("pid"))) {
                    //将当前对象id做为pid递归调用当前方法,获取下级结果
                    List> children = generateOrgMapToTree(orgMaps, Integer.valueOf(item.get("id").toString()));
                    //将子结果集存入当前对象的children字段中
                    item.put("children", children);
                    //添加当前对象到主结果集中
                    orgList.add(item);
                }
            }
        }
        return orgList;
    }

 

 

调用时操作:

 

public String ResponseCode() {
        List> list = generateOrgMapToTree(new ArrayList<>(), 0);
        String html = "";
        if (list.size() == 0) {
            html += "暂无信息";
            return html;
        }
        for (Map item : list) {
            List> list_child = (List>) item.get("children");
            html += "
"; html += "

"+ item.get("message") +"

"; if (list_child.size() == 0) { html += "暂无信息"; continue; } //输出返回信息 for (Map child : list_child) { html += child.get("message"); } } return html; }

 

转载于:https://www.cnblogs.com/betx/p/8669687.html

你可能感兴趣的:(Java 查询数据后进行递归操作)