-2

Some weird behaviour here as I'm not iterating through the list.

public boolean deleteEvents(ArrayList<EventsModel> list) {
    boolean success = false;
    synchronized (lock) {
        ArrayList<EventsModel> listClone = (ArrayList<EventsModel>) list.clone();
        success = processDelete(listClone);
    }
    return success;
}

private boolean processDelete(List<EventsModel> list) {
    boolean success = false;
    if (list.size() > 999) {
        List<EventsModel> subList = list.subList(0, 998);
        list.removeAll(subList); // blows up with ConcurrentModificationException
        // 
    } else {
        //
    }
    return success;
}

Am I not using removeAll correctly?

vkislicins
  • 3,331
  • 3
  • 32
  • 62
  • 2
    Side note on code quality: using booleans as "return code", thats like really bad practice. You should either throw exception to indicate errors (then all your methods are void; and passing means: all fine); or you should return some "real" return-code like objects. Everything else is like 1995-low-level-C-programming. Possible; but not what you do in 2016. – GhostCat Aug 11 '16 at 14:33
  • 1
    http://stackoverflow.com/questions/35294351/potential-bug-using-removeall-called-by-a-collection-on-itself `subList` returns a view of the list, changes "write through". See the linked question: calling `removeAll` on the collection itself can lead to this exception. – Tunaki Aug 11 '16 at 14:35
  • @GhostCat thanks, I'll take it into consideration. – vkislicins Aug 11 '16 at 14:36
  • Not sure how it's duplicate as I'm not calling `list.removeAll(list)` but `list.removeAll(subList)` – vkislicins Aug 11 '16 at 14:38
  • ah ok thanks for the explanation. I didn't realise sublist was backed by my original list. – vkislicins Aug 11 '16 at 14:39

1 Answers1

0

Your usage of subList() in conjunction with removeAll is what is causing this exception. you can read up on the javadoc for subList to understand more.

Kiran K
  • 703
  • 4
  • 11