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

摘要:
Java递归方法记录:privateList<Map<String,Object>>generateOrgMapToTree(列表<Map>String,对象>>orgMaps,整数){if(null==orgMaps | | orgMaps.size()==0){List<StatusResponseCodeEntity>List=statusResponeCodeRe

java的递归方法记录:

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

调用时操作:

publicString ResponseCode() {
        List<Map<String, Object>> list = generateOrgMapToTree(new ArrayList<>(), 0);
        String html = "";
        if (list.size() == 0) {
            html += "暂无信息";
            returnhtml;
        }
        for (Map<String, Object>item : list) {
            List<Map<String, Object>> list_child = (List<Map<String,Object>>) item.get("children");
            html += "<br/>";
            html += "<h3>"+ item.get("message") +"</h3>";
            if (list_child.size() == 0) {
                html += "暂无信息";
                continue;
            }
            //输出返回信息
            for (Map<String, Object>child : list_child) {
                html += child.get("message");
            }
        }

        returnhtml;
    }

免责声明:文章转载自《Java 查询数据后进行递归操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue项目性能优化整理jquery圆角插件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ExcelTools使用

using NPOI.SS.Formula.Functions; using NPOI.SS.UserModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; nam...

[大数据技术]datax的安装以及使用

1、datax简述 DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL、Oracle、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、DRDS 等各种异构数据源之间高效的数据同步功能。  datax的开源地址:https://g...

NeatUpload 的使用

1 <httpModules> 2 <add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule, Brettle.Web.NeatUpload" /> 3 </httpModules> 来自 <http://www....

C#操作word模板

    string newDocFileName = Guid.NewGuid().ToString().Replace("-", "");        string strServerPath = Server.MapPath("") + "\\Model.doc";  //模板路径        string strSavePath = Serve...

Java二维码的制作

二维码现在已经到处都是了,下面是二维码的介绍 :二维码 ,又称 二维条码 , 二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构 成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备...

poi提取docx中的文字和图片

package com.fry.poiDemo.dao; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import...