lundi 17 février 2020

Java - ArrayList object looping through multiple values and keys

i have following code which I have tried so far.

main.java

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

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

        yearList.add(2009);
        yearList.add(2010);
        yearList.add(2011);
        yearList.add(2012);
        yearList.add(2013);
        yearList.add(2014);

        yearList.add(21.4);
        yearList.add(31.3);
        yearList.add(15.6);
        yearList.add(34.8);
        yearList.add(20.1);
        yearList.add(27.3);

        yearList.add(20.3);
        yearList.add(43.1);
        yearList.add(9.8);
        yearList.add(34.7);
        yearList.add(24.4);
        yearList.add(21.9);

        for (int i = 0; i < (yearList.size()-(i+6)); i++) {
            Map<Object, Object> dataProviderValueMap = new LinkedHashMap<>();   
            dataProviderValueMap.put("year", yearList.get(i));

            for (int j = 0; j < (yearList.size()-(i+6)); j++) {
                dataProviderValueMap.put("income", yearList.get(j));

                for (int k = 0; k < (yearList.size()-(i)); k++) {
                    dataProviderValueMap.put("expenses", yearList.get(k));
                }
            }

            int s = yearList.size();

            // for year 2014
            if (s == (yearList.size() - 1)) {
                dataProviderValueMap.put("dashLengthLine", 0);
                dataProviderValueMap.put("dashLengthColumn", 5);
                dataProviderValueMap.put("alpha", 0.5);
                dataProviderValueMap.put("additional", "(projection)");
            }

            // for year 2013
            if (s == (yearList.size() - 2)) {
                dataProviderValueMap.put("dashLengthLine", 5);
            }

            dataProviderList.add(dataProviderValueMap);
        }
    }

i want following output:

required output:

"dataProvider" : [ {
    "year" : 2009,
    "income" : 21.4,
    "expenses" : 20.3
  }, {
    "year" : 2010,
    "income" : 31.3,
    "expenses" : 43.1
  }, {
    "year" : 2011,
    "income" : 15.6,
    "expenses" : 9.8
  }, {
    "year" : 2012,
    "income" : 34.8,
    "expenses" : 34.7
  }, {
    "year" : 2013,
    "income" : 20.1,
    "expenses" : 24.4,
    "dashLengthLine" : 5
  }, {
    "year" : 2014,
    "income" : 27.3,
    "expenses" : 21.9,
    "dashLengthLine" : 0,
    "dashLengthColumn" : 5,
    "alpha" : 0.5,
    "additional" : "(projection)"
  } ]

I'm using following code to get its JSON format.

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

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

How can i make this loop more concrete and enhanced for loop ? i only want to use one arraylist and for loop with if statement having Map inside the for loop. thanks for the help!

Aucun commentaire:

Enregistrer un commentaire