DEV Community

devanshi2806
devanshi2806

Posted on

4 1

Java-arraylist-object-looping-through-multiple-values-and-keys

I'm new to Java and I'm making Following json by using a single arraylist with for loop and if statement.But I'm stuck with my for loop logic part.

Here is my java program.

main.java

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

    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(19.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 - 6)); k++) {
                dataProviderValueMap.put("expenses", yearList.get(k));

                // for year 2014
                if (k == (yearList.size() - 2)) {

                    dataProviderValueMap.put("dashLengthLine", 0);
                    dataProviderValueMap.put("dashLengthColumn", 5);
                    dataProviderValueMap.put("alpha", 0.5);
                    dataProviderValueMap.put("additional", "(projection)");
                }

                // for year 2013
                if (k == (yearList.size() - 3)) {

                    dataProviderValueMap.put("dashLengthLine", 5);
                }
            }
        }
        dataProviderList.add(dataProviderValueMap);
    }


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

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

}

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

java for-loop if-statement arraylist dynamic

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay