gpt4 book ai didi

java - 如何在具有不同键的外部 hashMap 中传递具有相同键的内部 hashMap?

转载 作者:行者123 更新时间:2023-12-01 19:31:24 24 4
gpt4 key购买 nike

public static HashMap<String, HashMap<String,Object>> getDataDictionary() {
// Virtual Data Creation - Simulating The 9 Fields HAR value
HashMap<String,Object> valueFromEachOfThoseNineFields = new HashMap<String,Object>();
HashMap<String, HashMap<String,Object>> fileNameConstrains = new HashMap<String,HashMap<String,Object>>();
valueFromEachOfThoseNineFields.put("A", 10);
valueFromEachOfThoseNineFields.put("B", 20);
valueFromEachOfThoseNineFields.put("C", 30);
valueFromEachOfThoseNineFields.put("D", 10);
valueFromEachOfThoseNineFields.put("E", 5);
valueFromEachOfThoseNineFields.put("F", 20);
valueFromEachOfThoseNineFields.put("G", 1);
valueFromEachOfThoseNineFields.put("H", 100);
valueFromEachOfThoseNineFields.put("I", 66);
fileNameConstrains.put("File1", valueFromEachOfThoseNineFields);
valueFromEachOfThoseNineFields.put("A", 10);
valueFromEachOfThoseNineFields.put("B", 50);
valueFromEachOfThoseNineFields.put("C", 30);
valueFromEachOfThoseNineFields.put("D", 90);
valueFromEachOfThoseNineFields.put("E", 5);
valueFromEachOfThoseNineFields.put("F", 20);
valueFromEachOfThoseNineFields.put("G", 10);
valueFromEachOfThoseNineFields.put("H", 90);
valueFromEachOfThoseNineFields.put("I", 61);
fileNameConstrains.put("File2", valueFromEachOfThoseNineFields);
valueFromEachOfThoseNineFields.put("A", 10);
valueFromEachOfThoseNineFields.put("B", 60);
valueFromEachOfThoseNineFields.put("C", 30);
valueFromEachOfThoseNineFields.put("D", 2);
valueFromEachOfThoseNineFields.put("E", 13);
valueFromEachOfThoseNineFields.put("F", 9);
valueFromEachOfThoseNineFields.put("G", 15);
valueFromEachOfThoseNineFields.put("H", 88);
valueFromEachOfThoseNineFields.put("I", 69);
fileNameConstrains.put("File3", valueFromEachOfThoseNineFields);

return fileNameConstrains;
}

在这种情况下,我只得到最后一组,所有数据都被覆盖,任何人都可以帮助我弄清楚如何以如下格式传递数据?

{Key="File1", value={'a'=10,'b'=5}}

等等。

最佳答案

每次您想要将值存储到其中时,都需要将新的 HashMap 实例分配给 valueFromEachOfThoseNineFields ,然后将 valueFromEachOfThoseNineFields 存储到 >fileNameConstrains 如下:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {
System.out.println(getDataDictionary());
}

public static HashMap<String, HashMap<String, Object>> getDataDictionary() {
// Virtual Data Creation - Simulating The 9 Fields HAR value
HashMap<String, Object> valueFromEachOfThoseNineFields;
HashMap<String, HashMap<String, Object>> fileNameConstrains = new HashMap<String, HashMap<String, Object>>();

valueFromEachOfThoseNineFields = new HashMap<String, Object>();
valueFromEachOfThoseNineFields.put("A", 10);
valueFromEachOfThoseNineFields.put("B", 20);
valueFromEachOfThoseNineFields.put("C", 30);
valueFromEachOfThoseNineFields.put("D", 10);
valueFromEachOfThoseNineFields.put("E", 5);
valueFromEachOfThoseNineFields.put("F", 20);
valueFromEachOfThoseNineFields.put("G", 1);
valueFromEachOfThoseNineFields.put("H", 100);
valueFromEachOfThoseNineFields.put("I", 66);
fileNameConstrains.put("File1", valueFromEachOfThoseNineFields);

valueFromEachOfThoseNineFields = new HashMap<String, Object>();
valueFromEachOfThoseNineFields.put("A", 10);
valueFromEachOfThoseNineFields.put("B", 50);
valueFromEachOfThoseNineFields.put("C", 30);
valueFromEachOfThoseNineFields.put("D", 90);
valueFromEachOfThoseNineFields.put("E", 5);
valueFromEachOfThoseNineFields.put("F", 20);
valueFromEachOfThoseNineFields.put("G", 10);
valueFromEachOfThoseNineFields.put("H", 90);
valueFromEachOfThoseNineFields.put("I", 61);
fileNameConstrains.put("File2", valueFromEachOfThoseNineFields);

valueFromEachOfThoseNineFields = new HashMap<String, Object>();
valueFromEachOfThoseNineFields.put("A", 10);
valueFromEachOfThoseNineFields.put("B", 60);
valueFromEachOfThoseNineFields.put("C", 30);
valueFromEachOfThoseNineFields.put("D", 2);
valueFromEachOfThoseNineFields.put("E", 13);
valueFromEachOfThoseNineFields.put("F", 9);
valueFromEachOfThoseNineFields.put("G", 15);
valueFromEachOfThoseNineFields.put("H", 88);
valueFromEachOfThoseNineFields.put("I", 69);
fileNameConstrains.put("File3", valueFromEachOfThoseNineFields);

return fileNameConstrains;
}
}

输出:

{File3={A=10, B=60, C=30, D=2, E=13, F=9, G=15, H=88, I=69}, File2={A=10, B=50, C=30, D=90, E=5, F=20, G=10, H=90, I=61}, File1={A=10, B=20, C=30, D=10, E=5, F=20, G=1, H=100, I=66}}

关于java - 如何在具有不同键的外部 hashMap 中传递具有相同键的内部 hashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com