I have result set data coming back from DB as follows: Input looks like this. Its an arrayList
id year name rank Subj Marks
1 2025 name1 A Science 90
1 2025 name1 A History 90
1 2025 name1 A Math 90
2 2025 name2 A Science 80
2 2025 name2 A History 80
2 2025 name2 A Math 80
Output should look like this
id year name rank Subj Marks
1 2025 name1 A Subject 270
2 2025 name2 A Subject 240
This would be having 1500 records minimum so which would be efficient. What i was thinking is looping the list and putting them to hashMap. Then iterate maps with same id’s and sum the marks property.
Is there an efficient way to do this?
HashMap<String, Object> hmDto = new HashMap<String, Object>();
for (ABCDTO obj : list)
{
ResultsBean bean = new ResultsBean();
hmDto.put(obj.getId(), bean);
}
and then iterate the hashmap and sum the marks. Finally add cummulated data to another list
HashMap<String, Object> hmCalDto = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : hmDto .entrySet()) {
List<ABCDTO> dtoTempList = new ArrayList();
ABCDTO obj = (ABCDTO) entry.getValue();
if (hmDto.containsKey(entry.getKey())) {
dtoTempList.add(obj);
} else {
continue;
}
hmCalDto.put(entry.getKey(), obj);
}
I am lost on how to do this further. Is there an efficient way to do this please.