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<String,Object> Map1 = new HashMap<>();
    HashMap<String, HashMap<String,Object>> fileNameConstrains = new HashMap<>();
    Map1.put("A", 10);
    Map1.put("B", 20);
    fileNameConstrains.put("File1", Map1);
    Map1 = new HashMap<String,Object>();
    Map1.put("A", 10);
    Map1.put("B", 50);
    fileNameConstrains.put("File2", Map1);
    Map1 = new HashMap<String,Object>();
    Map1.put("A", 10);
    Map1.put("B", 60);
    fileNameConstrains.put("File3", Map1);


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

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

10-10 18:32