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

Expecting ConcurrentModificationException while iterating & calling list.remove

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      HP / Windows / Java-8

      A DESCRIPTION OF THE PROBLEM :
      We are expecting ConcurrentModificationException, While iterating the elements from ArrayList using advance for loop at the same time we are trying to remove the element (Removing the second last element) from the the same ArrayList, but we are not getting any ConcurrentModificationException.

      REGRESSION : Last worked in version 8u211

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      package corejava.collection;

      import java.lang.reflect.Field;
      import java.util.ArrayList;

      public class ListImplementation {
      public static void main(String[] args) throws Exception {
      ArrayList<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      System.out.println("Before removing element: "+ list);
      for (String s : list) {
      if(s.equals("C"))
      list.remove(s);
      }
      System.out.println("After removing element: "+ list);
      }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Exception in thread "main" java.util.ConcurrentModificationException
      at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
      at java.util.ArrayList$Itr.next(Unknown Source)
      at corejava.collection.ListImplementation.main(ListImplementation.java:15)

      ACTUAL -
      Running normally:
      O/P:
      Before removing element: [A, B, C, D]
      After removing element: [A, B, D]


      ---------- BEGIN SOURCE ----------
      package corejava.collection;

      import java.lang.reflect.Field;
      import java.util.ArrayList;

      public class ListImplementation {
      public static void main(String[] args) throws Exception {
      ArrayList<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      System.out.println("Before removing element: "+ list);
      for (String s : list) {
      if(s.equals("C"))
      list.remove(s);
      }
      System.out.println("After removing element: "+ list);
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              psonal Pallavi Sonal (Inactive)
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: