JDK-8228354 : Expecting ConcurrentModificationException while iterating & calling list.remove
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 8u202
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2019-07-17
  • Updated: 2021-01-27
  • Resolved: 2019-07-18
Related Reports
Duplicate :  
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