JDK-8185622 : Able to delete a element from list using list remove() while iteration the same list.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 8u77
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2017-07-28
  • Updated: 2017-08-01
  • Resolved: 2017-08-01
Related Reports
Duplicate :  
Description
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.