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