Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a missing record into the arraylist if it is not in it

Tags:

java

arraylist

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.

like image 412
vlina Avatar asked Jan 26 '23 06:01

vlina


1 Answers

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);
like image 76
Joakim Danielson Avatar answered Feb 02 '23 10:02

Joakim Danielson