有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用hashmap和arraylist的循环逻辑?

我有以下代码:

public static void main(String args[]) throws JsonProcessingException {

    List<Map<Object, Object>> dataProviderList = new LinkedList<>();

    List<String> X_AxesList = new LinkedList<>();
    List<String> Y_AxesList = new LinkedList<>();
    List<Integer> ValueList = new LinkedList<>();

    X_AxesList.add("Arizona");
    X_AxesList.add("Arkansas");
    X_AxesList.add("Florida");
    X_AxesList.add("Ohio");
    X_AxesList.add("WashingtonDC");

    Y_AxesList.add("2012");
    Y_AxesList.add("2013");
    Y_AxesList.add("2014");
    Y_AxesList.add("2015");

    ValueList.add(20);
    ValueList.add(30);
    ValueList.add(10);
    ValueList.add(40);
    ValueList.add(50);
    ValueList.add(15);
    ValueList.add(35);
    ValueList.add(42);
    ValueList.add(85);
    ValueList.add(19);
    ValueList.add(16);
    ValueList.add(65);
    ValueList.add(92);
    ValueList.add(18);
    ValueList.add(23);
    ValueList.add(26);
    ValueList.add(97);
    ValueList.add(67);
    ValueList.add(54);
    ValueList.add(34);

    Map<Object, Object> map1 = new LinkedHashMap<>();

    for (int i = 0; i < X_AxesList.size(); i++) {

        for (int j = 0; j < Y_AxesList.size(); j++) {

            for (int k = 0; k <ValueList.size(); k++) {

                map1.put("Country", X_AxesList.get(i));
                map1.put(Y_AxesList.get(j), ValueList.get(k));





                // System.out.println(X_AxesList.get(i));
                dataProviderList.add(map1);
            }
        }
    }
    Map<String, Object> responseMap = new LinkedHashMap<>();
    responseMap.put("dataProvider", dataProviderList);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

    String mapToJson = objectMapper.writeValueAsString(responseMap);
    System.out.println(mapToJson);
}

}

我希望输出如下:

"dataProvider": [{
  "country": "Arizona",
  "2012": 20,
  "2013": 30,
  "2014": 10,
  "2015": 40
}, {
  "country": "Arkansas",
  "2012": 50,
  "2013": 15,
  "2014": 35,
  "2015": 42
}, {
  "country": "Florida",
  "2012": 85,
  "2013": 19,
  "2014": 16,
  "2015": 65
}, {
  "country": "Ohio",
  "2012": 92,
  "2013": 18,
  "2014": 23,
  "2015": 26
}, {
  "Country": "WashingtonDC",
  "2012": 34,
  "2013": 54,
  "2014": 67,
  "2015": 21
}]

当我尝试在for循环中应用上述逻辑时,它给出了400次迭代的输出:

 [{
    "Country" : "WashingtonDC",
    "2012" : 34,
    "2013" : 34,
    "2014" : 34,
    "2015" : 34
  }, {
    "Country" : "WashingtonDC",
    "2012" : 34,
    "2013" : 34,
    "2014" : 34,
    "2015" : 34
  }, {
    "Country" : "WashingtonDC",
    "2012" : 34,
    "2013" : 34,
    "2014" : 34,
    "2015" : 34
  } ,{},{},{}............400]

但是我只需要5次迭代,如上面代码所示,通过使用arrayList和hashmap以及DYNAMICfor循环

请帮助我完成for循环逻辑部分。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    您必须解决这些问题,不断向相同的Map添加值,以便最后一个键覆盖前面的键,并且您迭代值列表,而不是从中获取切片

    用2替换3个嵌套的for循环,并使用索引跟踪ValueList

    int valuesIndex = 0;
    for (String country: X_AxesList) {
        Map<Object, Object> map1 = new LinkedHashMap<>();
        map1.put("Country", country);
        for (String year: Y_AxesList) {
            map1.put(year, ValueList.get(valuesIndex++));   
        }
        dataProviderList.add(map1);
    }
    

    输出

    Country Arizona
    2012 20
    2013 30
    2014 10
    2015 40
    Country Arkansas
    2012 50
    2013 15
    2014 35
    2015 42
    Country Florida
    2012 85
    2013 19
    2014 16
    2015 65
    Country Ohio
    2012 92
    2013 18
    2014 23
    2015 26
    Country WashingtonDC
    2012 97
    2013 67
    2014 54
    2015 34