0

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.

4
  • 1
    Using a HashMap to group by id, year, name, and rank is the most efficient way to aggregate marks, with O(N) complexity. You can build a composite key for grouping and sum the marks in a single pass through the list. Commented May 12 at 23:47
  • @Vijay. Can you help with a sample code snippet please Commented May 12 at 23:50
  • 1
    List.stream().collect(Collectors.groupingBy(Bean::getField)) returns a Map<ClassOfField, List<ClassOfBean>> object. List.forEach(obj -> {}) have save effect with for-each statement. Hope this can help you. Commented May 12 at 23:53
  • 2
    usually it is better to let the database do the data manipulation - data aggregation is one of its (main) functionality Commented May 13 at 6:14

1 Answer 1

1

You can use a HashMap with a composite key (e.g., String key or a custom class) to accumulate marks efficiently.

Sample Solution in Java:

class Result {
    int id;
    int year;
    String name;
    String rank;
    int totalMarks;

    // Constructor
    public Result(int id, int year, String name, String rank, int totalMarks) {
        this.id = id;
        this.year = year;
        this.name = name;
        this.rank = rank;
        this.totalMarks = totalMarks;
    }

    @Override
    public String toString() {
        return id + "\t" + year + "\t" + name + "\t" + rank + "\tSubject\t" + totalMarks;
    }
}

Aggregation Code:

Map<String, Result> aggregateMap = new HashMap<>();

for (ABCDTO obj : list) {
    String key = obj.getId() + "-" + obj.getYear() + "-" + obj.getName() + "-" + obj.getRank();

    if (aggregateMap.containsKey(key)) {
        Result result = aggregateMap.get(key);
        result.totalMarks += obj.getMarks();
    } else {
        Result result = new Result(
            obj.getId(),
            obj.getYear(),
            obj.getName(),
            obj.getRank(),
            obj.getMarks()
        );
        aggregateMap.put(key, result);
    }
}

// Final Output
for (Result res : aggregateMap.values()) {
    System.out.println(res);
}
Sign up to request clarification or add additional context in comments.

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.