-2

I have following code:

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);
}

}

I want output as follows:

"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
}]

As i'm trying to apply the above logic in for loop it gives an output of 400 iterations of this:

 [{
    "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]

But i want only 5 iterations as shown in above code by using arrayList and hashmap and DYNAMIC for loop.

Please help me out with my for loop logic part. Thanks in advance!

DEVANSHI
  • 21
  • 6

1 Answers1

1

You have to issues, you keep adding values to the same Map so the last key overrides the previous keys, and you iterate over the values list instead of taking slice from it.

Replace the 3 nested for loops with 2 and use index to keep track over 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);
}

Output

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
Guy
  • 46,488
  • 10
  • 44
  • 88