1

I am having an arraylist like below:

stackedBarChart.stackedBarChartSeries.data.add(1,2,3)
stackedBarChart.stackedBarChartSeries.data.add(3,4,5)
stackedBarChart.stackedBarChartSeries.data.add(4,5,6)
stackedBarChart.stackedBarChartSeries.data.add(6,7,8)

And Another list

stackedBarChart.XAxis.add("Jan","Feb","March")

And I wants to make Map<String, Integer> where map will be like:

Jan = 14, Feb = 18, March = 22

Where each value represent the addition of each index of arraylist from stackedBarChart.

For now I have done this using simple for loop like below:

stackedBarChart.stackedBarChartSeries.forEach(stackedBarChartTableDataList ->{
    for(int i = 0; i < stackedBarChart.getXAxis().size();i++) {
             Integer stackedBarChartTableData = stackedBarChartTableDataList.getData().get(i);
             String stackedBarChartTableCurrentColumnName =  stackedBarChart.getXAxis().get(i);
             stackedBarChartTableColumnSumMap.merge(stackedBarChartTableCurrentColumnName, stackedBarChartTableData, Integer::sum);
        }
})

I wants to have a simple solution to achieve this using stream, or any other simple way than using for loop.

Can you please help me for this?

Edit:

Here is object representation:

{
"stackedBarChart": {
    "stackedBarChartSeries": [
        {
            "data": [
                1,
                2,
                3
            ]
        },
        {
            "data": [
                3,
                4,
                5
            ]
        },
        {
            "data": [
                4,
                5,
                6
            ]
        },
        {
            "data": [
                6,
                7,
                8
            ]
        }
    ]
},
"xAxis": [
    "Jan 2021",
    "Feb 2021",
    "Mar 2021"
]

}

4
  • 1
    Provide more detail about your objects structures. Commented Jan 16, 2022 at 9:39
  • Hi @HadiJ , { "stackedBarChart": { "stackedBarChartSeries": [ { "data": [ 1, 2, 3 ] }, { "data": [ 3, 4, 5 ] }, { "data": [ 4, 5, 6 ] }, { "data": [ 6, 7, 8 ] } ] }, "xAxis": [ "Jan 2021", "Feb 2021", "Mar 2021" ] } Commented Jan 16, 2022 at 9:52
  • 1
    stackedBarChart.stackedBarChartSeries.forEach(sbcs -> IntStream.range(0, stackedBarChart.getxAxis().size()) .forEach(i -> stackedBarChartTableColumnSumMap.merge(stackedBarChart.getxAxis().get(i), sbcs.getData().get(i), Integer::sum))); your version is more readable. Commented Jan 16, 2022 at 11:19
  • Thanks you so much @HadiJ, this is the answer I was looking for. Appreciate! Commented Jan 16, 2022 at 11:41

2 Answers 2

1

This should do the trick:

public static Map<String, Integer> mergeBySum(Map<String, Integer> map1, Map<String, Integer> map2) {
    return Stream.of(map1, map2)
            .map(Map::entrySet)
            .flatMap(Set::stream)
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (u1, u2) -> u1 + u2));
}

public Map<String, Integer> sumByMonth(/*bar chart type*/ stackedBarChart) {
    List<String> xAxis = stackedBarChart.getXAxis();
    int axisLength = xAxis.size();
    return stackedBarChart.stream().map(stackedBarChartTableDataList ->
        IntStream.range(0, axisLength).boxed().collect(Collectors.toMap(xAxis::get, stackedBarChartTableDataList::get))
    ).reduce((map1, map2) -> mergeBySum(map1, map2)).orElse(Collections.EMPTY_MAP);
}
Sign up to request clarification or add additional context in comments.

Comments

0

From https://stackoverflow.com/users/3669698/hadi-j,

stackedBarChart.stackedBarChartSeries.forEach(sbcs ->
IntStream.range(0, stackedBarChart.getxAxis().size())                         
.forEach(i ->  stackedBarChartTableColumnSumMap.merge(stackedBarChart.getxAxis().get(i), sbcs.getData().get(i), 
Integer::sum)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.