Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8246702

ConcurrentModification Exception not thrown while removing second last element

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 11, 13, 15
    • Fix Version/s: None
    • Component/s: core-libs

      Description

      ADDITIONAL SYSTEM INFORMATION :
      The issue is OS independent.
      I have seen this issue in my personal lap with configuration : macOs Catalina version : 10.15.4 (19E287)
      java version "13.0.1" 2019-10-15

      And also in office laptop with Windows 10 and java 9.0.1 version

      A DESCRIPTION OF THE PROBLEM :
      While iterating over the arraylist and removing the element by calling list.remove(Object), we should get the ConcurrentModification exception. Which happens for all the elements except the second last element.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Instantiate an arrayList with minimum 3 elements to understand clearly.
      use for(String s: list) format to iterate over it or can directly use the iterator.next()
      inside the loop try deleting elements one by one.
      We will get ConcurrentModificationException for 1st and 3rd element.
      But not for the 2nd element (which happens to be 2nd last element).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      ConcurrentModificationException should be thrown even while deleting the 2nd last element.
      All the elements must be treated same way.

      ACTUAL -
      If instead of the 2nd last word i remove any other word it throws the exception. But in 2nd last item it modifies the list just fine, without any issue


      Based on code analysis:
      when we call the method remove, it removes the element from list, and adjusts the list to new size, ie, previousSize -1.when it tries to go to next element, itr.next() gets called and the very first line checks for the concurrentModification, which is true, so throws the exception.However in case of last-1 element deletion, when the list adjusts its size,for it hasNext() will return no other element, so next() never gets called hence, no exception

      ---------- BEGIN SOURCE ----------
      Issue can be reproduced by using the below code

      ArrayList<String> list = new ArrayList<>();
      list.add("WHY");
      list.add("WHAT");
      list.add("WHEN");
      list.add("WHERE");

      for(String word : list){
      if(“WHEN”.equals(word))
      list.remove(word);
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No WorkAround for this.
      only option is to use the iterator.remove.
      However the issue i am raising is to make sure the exception be thrown for 2nd last element.

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: