5

Pardon me as I'm quite a beginner in coding. I have tried researching for ways to add some missing record into the lists but still can't seem to fit it correctly into my code.

I have two ArrayLists with different resultsets. Say, the first one is derived in other method and stored in abcList. This list is then used in my current fixChartStats method as a param.

In my code, I will check for the corresponding record in abcList with the second list I derive from the hql query in fixChartStats method.

If the record corresponds, then I'll do the necessary action as shown below to update the ApprovedCount number etc, else i set it to 0.

How do I go about adding the records that are missing in second list I got into the first arraylist (i.e. abcList)? Can anyone here shed some light? Do let me know if my questions are unclear. Thanks in advance, guys!

private void fixChartStats(List<TAbcModel> abcList, Map<String, Object> param, List<IssueModel> issueList, List<DestModel> destList) throws Exception {

    //initialize the hql query
    //translate all fields from Object[] into individual variable

    firstRow = true;
    for (TAbcModel abc : abcList) {
        if (abc.getId().getAbcYear() = abcYear &&
                abc.getId().getAbcMonthId() = abcMonthId &&
                abc.getId().getAbcApplAccnId().getAccnId().equalsIgnoreCase(abcApplAccnId.getAccnId()) {

            if (firstRow) {
                abc.setApprovedCount(abcApprovedCount);
                abc.setCancelledCount(abcCancelledCount);
                firstRow = false;
            } else {
                abc.setApprovedCount(0);
                abc.setCancelledCount(0);
            }
        }else{
            // How to do the necessary here
            // Below is what I've tried
            abcList.add(abc);
        }
    }
}

When I debug, I noticed that it was added into the list. But soon after it was added, ConcurrentModificationException was thrown.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vlina
  • 81
  • 1
  • 9
  • 3
    You can't modify a list while you are iterating over it, see https://stackoverflow.com/questions/993025/adding-elements-to-a-collection-during-iteration – second May 29 '19 at 09:37

1 Answers1

9

Create a local list and add missing records to it then add all elements from the local list to the abcList

List<TAbcModel> temp = new ArrayList<>();

in your loop:

} else { 
    temp.add(abc);
}

after loop

abcList.addAll(temp);
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52