如何在 while 循环迭代中将所有行放入 HashMap 中。

huangapple go评论68阅读模式
英文:

How to put all the rows in a HashMap in while loop iteration

问题

public void addinputtosc() {
    try {
        Map<String, List<JsonNode>> testrecords = null;
        Map<String, String> rows = new Hashmap<String, String>();

        // this function takes the input sheet , sheet name and returns data in Map<String, List<JsonNode>> format.
        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")) {
                ListIterator<JsonNode> listIterator = testCaseInputs.listIterator();
                while (listIterator.hasNext()) {
                    for (JsonNode tcinputs : testCaseInputs) {
                        String keyValue = tcinputs.toString();
                        String newKeyValue = keyValue.replaceAll("[{}]", "");
                        String[] keyValue1 = newKeyValue.split(",");
                        for (String j : keyValue1) {
                            String[] keyValueorg = j.split(":");
                            row.put(keyValueorg[0].substring(1, keyValueorg[0].length() - 1), keyValueorg[1].substring(1, keyValueorg[1].length() - 1));
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文:

Issue : There will 3 records in testcaseInputs <JsonNode>. All the three records are iterated, but at end the "rows" map has only one record which is iterated in the last. I want rows map should contain all the three records.

Issue 2: The iteration takes record1, then record 2, record 3 .. again it takes record 1 or 3 for iteration. I don't know why.

public void addinputtosc() {
try {
Map&lt;String, List&lt;JsonNode&gt;&gt; testrecords = null;
Map&lt;String, String&gt; rows = new Hashmap&lt;String, String&gt;();
// this function takes the input sheet , sheet name and returns data in Map&lt;String, List&lt;JsonNode&gt;&gt; format.
testrecords = fetchScenariosData(&quot;C:\\testData.xlsx&quot;, &quot;input&quot;, &quot;inputParam&quot;);
Iterator&lt;Map.Enry&lt;String, List&lt;JsonNode&gt;&gt;&gt; entries = testRecords.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry&lt;String, List&lt;JsonNode&gt;&gt; entry = entries.next();
String scenarioName = entry.getKey();
List&lt;JsonNode&gt; testcaseInputs = entry.getValue();
if (scenarioName.equalsIgnoreCase(&quot;TestCase1&quot;)) {
ListIterator&lt;JsonNode&gt; listIterator = testCaseInputs.listIterator();
while (listIterator.hasNext()) {
for (JsonNode tcinputs :testCaseInputs) {
String keyValue = tcinputs.toString();
String newKeyValue = keyValue.replaceAll(&quot;[{}]&quot;, &quot;&quot;);
String[] keyValue1 = newKeyValue.split(&quot;,&quot;);
for (String j : keyValue1) {
String[] keyValueorg = j.split(&quot;:&quot;);
row.put(keyValueorg[0].substring(1, keyValueorg[0].length() - 1), keyValueorg[1].substring(1, keyValueorg[1].length() - 1));
}
}
}
}
}
} catch (exception e) {
e.printStackTrace();
}
}

答案1

得分: 1

> 问题:在testcaseInputs中将会有3个记录。这三个记录都会被迭代,但是最后在“rows”映射中只有一条记录是在最后被迭代的。我希望rows映射应该包含所有三条记录。

这是因为这行代码:

rows.put(keyValueorg[0].substring(1, keyValueorg[0].length() - 1), keyValueorg[1].substring(1, keyValueorg[1].length() - 1));

当您处理第一个JsonNode时,根据您的示例,假设是这样的:

{ "File Source Env.": "Unix", "TC_ID": "tc1", "File Path": "/tmp/test.dat", "Date": "20190101" }

HashMap rows 将包含如下内容:

{ Date=20190101, path=/tmp/test.dat, TC_ID=tc1, File Source Env.=Unix }

现在当再次执行这段代码处理第二个JsonNode时,假设是这样的:

{ "File Source Env.": "Unix-qa", "TC_ID": "tc2", "File Path": "/tmp/test1.dat", "Date": "20190201" }

根据您的代码,计算这个新记录的键值 (keyValueorg[0].substring(1, keyValueorg[0].length() - 1)) 与先前存储在哈希映射中的键值相同,即 Date、File Source Env、TC_ID、Path。由于这些键值已经存在于哈希映射中,它们的值会被新值更新,这是HashMap的PUT操作的特性(如果键存在,则用新值覆盖,否则将新键插入映射中)。

这个过程将继续,因此只有最后一条记录的值会在哈希映射中看到。

为了在单个哈希映射中保留所有记录的键值对,您需要为每个记录创建不同的键。或者,您可以创建一个嵌套的哈希映射。

英文:

> Issue : There will 3 records in testcaseInputs . All the three records
> are iterated, but at end the "rows" map has only one record which is
> iterated in the last. I want rows map should contain all the three
> records.

This is happening because of this line :

rows.put(keyValueorg[0].substring(1, keyValueorg[0].length() - 1), keyValueorg[1].substring(1, keyValueorg[1].length() - 1));

when you are procesing frist JsonNode suppose this as per your example

{&quot;File Source Env.&quot;:&quot;Unix&quot;,&quot;TC_ID&quot;:&quot;tc1&quot;,&quot;File Path&quot;:&quot;/tmp/test.dat&quot;,&quot;Date&quot;:&quot;20190101&quot;}

the HashMap rows will contain content as :

{Date=20190101, path=/tmp/test.dat, TC_ID=tc1, File Source Env.=Unix}

now when again this codeis executed for second JsonNode suppose this :

{&quot;File Source Env.&quot;:&quot;Unix-qa&quot;,&quot;TC_ID&quot;:&quot;tc2&quot;,&quot;File Path&quot;:&quot;/tmp/test1.dat&quot;,&quot;Date&quot;:&quot;20190201&quot;}

as per your code , keys which will be calculated for this new record (keyValueorg[0].substring(1, keyValueorg[0].length() - 1)) is same as the previous key values that are stored in hashmap i.e. Date, File Source Env, TC_ID, Path by the first record.
Since these key values are already present in hashmap there values get updated by new values which is property of PUT operation of HashMap(if key is there then it just override with new values else insert new key in map).

This process will continue and hence only last record values are seen in hashmap.

In order to keep all key-value pairs of all records in single hashmap you need to create different key for each record. Otherwise create a nested hashmap.

huangapple
  • 本文由 发表于 2020年4月9日 18:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定