You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HashMap循环迭代存储多行数据异常问题排查

问题分析与修复方案

咱们来逐个解决你遇到的这两个问题,先从代码里的根源说起:

问题1:仅保留最后一条记录的原因及修复

你现在只用了单个Map<String, String>(代码里还写错了变量名,定义的是rows,循环里写成了row),每次处理一条测试用例输入时,都是往同一个Map里塞键值对。HashMap的特性是键重复时新值覆盖旧值,就算键不重复,你也没有把每条处理完的记录单独保存,而是一直复用同一个Map,最后自然只留下最后一次迭代的结果。

修复方案:
改用列表存储每条记录对应的Map,这样每条记录都会被独立保存:

  • 把单个Map的定义改成List<Map<String, String>> allRows = new ArrayList<>();
  • 每次处理一条测试用例时,新建一个空Map,处理完后将这个Map加入列表,避免复用覆盖

问题2:重复读取记录的原因及修复

你代码里嵌套了两层循环遍历testCaseInputs:外层用ListIterator的while循环,内层又套了for-each循环,这就相当于把整个测试用例列表遍历了N遍(N是列表的长度),所以才会出现重复读取记录的情况。完全不需要外层的ListIterator循环,直接用for-each遍历就足够了。

额外需要修正的语法小错误

代码里还有几个Java大小写/拼写问题,不修复会编译失败:

  • Hashmap → HashMap(首字母大写)
  • Map.Enry → Map.Entry(拼写错误)
  • testRecords → testrecords(大小写不一致,Java大小写敏感)
  • exception → Exception(首字母大写)
  • 循环里的row.put统一为对应变量名

修复后的完整代码

public void addinputtosc() {
    try {
        Map<String, List<JsonNode>> testrecords = null;
        // 用列表存储所有记录,每个元素对应一条测试用例的键值对
        List<Map<String, String>> allRows = new ArrayList<>();
        
        // 获取测试场景数据
        testrecords = fetchScenariosData("C:\\testData.xlsx", "input", "inputParam");
        Iterator<Map.Entry<String, List<JsonNode>>> entries = testrecords.entrySet().iterator();
        
        while (entries.hasNext()) {
            Map.Entry<String, List<JsonNode>> entry = entries.next();
            String scenarioName = entry.getKey();
            List<JsonNode> testcaseInputs = entry.getValue();
            
            if (scenarioName.equalsIgnoreCase("TestCase1")) {
                // 直接遍历测试用例列表,无需嵌套迭代器循环
                for (JsonNode tcinputs : testcaseInputs) {
                    // 每条测试用例新建独立的Map,避免覆盖之前的记录
                    Map<String, String> row = new HashMap<>();
                    
                    String keyValue = tcinputs.toString();
                    String newKeyValue = keyValue.replaceAll("[{}]", "");
                    String[] keyValue1 = newKeyValue.split(",");
                    
                    for (String j : keyValue1) {
                        // 先去除前后空格,避免分割出空字符串
                        String trimmedPair = j.trim();
                        String[] keyValueorg = trimmedPair.split(":");
                        // 加判断避免格式错误导致数组越界
                        if (keyValueorg.length == 2) {
                            String key = keyValueorg[0].trim().substring(1, keyValueorg[0].trim().length() - 1);
                            String value = keyValueorg[1].trim().substring(1, keyValueorg[1].trim().length() - 1);
                            row.put(key, value);
                        }
                    }
                    // 将当前测试用例的Map加入总列表
                    allRows.add(row);
                }
            }
        }
        
        // 这里可以添加对allRows的后续操作,比如打印验证
        for (Map<String, String> record : allRows) {
            System.out.println("测试用例记录:" + record);
        }
        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

内容的提问来源于stack exchange,提问作者Inspiron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 10:07:28