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

Able to delete a element from list using list remove() while iteration the same list.

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_77"
      Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
      Java HotSpot(TM) Client VM (build 25.77-b03, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      windows 7. This issue is exist in all the environments.

      A DESCRIPTION OF THE PROBLEM :
      Able to delete a element from list usring list remove() while iteration the same list.

      public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {

      List<String> list = new ArrayList<String>();

      list.add("Thiru");
      list.add("Thiru1");
      list.add("Thiru2");

      for(String str : list){
      if("Thiru1".equals(str)){
      list.remove(str);
      }
      }

      System.out.println("Size After:"+list.size());

      }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {

      List<String> list = new ArrayList<String>();

      list.add("Thiru");
      list.add("Thiru1");
      list.add("Thiru2");

      for(String str : list){
      if("Thiru1".equals(str)){
      list.remove(str);
      }
      }

      System.out.println("Size After:"+list.size());

      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      we should get ConcurrentModificationExceptoin.
      ACTUAL -
      element should not get deleted from list & it should throw ConcurrentModificationExceptoin

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.thiru;

      import java.util.ArrayList;
      import java.util.List;

      public class JavaBug {

      public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {

      List<String> list = new ArrayList<String>();

      list.add("Thiru");
      list.add("Thiru1");
      list.add("Thiru2");

      for(String str : list){
      if("Thiru1".equals(str)){
      list.remove(str);
      }
      }

      System.out.println("Size After:"+list.size());

      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No work around.

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

              Created:
              Updated:
              Resolved: