Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concepts of modifying the element in collection while iterating?

Tags:

java

I found this statement if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception. at http://download.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html.

I found the concurrent modification is even thrown in below code

List<Employee> lista= new ArrayList();
Employee emp1=new Employee();
Employee emp2=new Employee();
Employee emp3=new Employee();
lista.add(emp1);
lista.add(emp2);
lista.add(emp3);
for(Employee emp:lista)
{
emp2.setEmpId(2);
lista.remove(emp2);
}

Question1:- So can i say enhance for loop also uses the fail-fast iterator internally?Though when i execute below code it works fine

for(int i=0;i<lista.size();i++)
{
Employee empTemp=lista.get(i);
lista.remove(emp2);         
}

Question2:- Another question is when statement say

if a thread modifies a a collection

My guess is that modification means here removal or addition not for update of element inside collection for list interface while it also includes modification of element for set interface . Right? At least the programmes i tried it is the case with them.

Edit

Regarding set i doubt my above statement i.e it throws concurrent modification exception when we try to modify set while iterating.I tried below code but it did not throw any exception

HashSet<Employee> set1= new HashSet();
Employee emp4=new Employee();
Employee emp5=new Employee();
Employee emp6=new Employee();
set1.add(emp4);
set1.add(emp5);
set1.add(emp6);


Iterator iter1=set1.iterator();
while(iter1.hasNext())
{
Employee emp12=(Employee)iter1.next();
System.out.println("");
emp5.setEmpId(2);

}

Ideally as per statement if the set is modified at any time after the iterator is created at http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html it should throw the concurrent modification exception but it did not . Not sure why?

like image 981
M Sach Avatar asked Sep 10 '11 14:09

M Sach


2 Answers

Question1:- So can i say enhance for loop also uses the fail-fast iterator internally?Though when i execute below code it works fine

Yes, thats right. Have a look at the compiled code, with the javap command to verify this if you like.

My guess is that modification means here removal or addition not for updation of element inside collection for list interface while it also includes modification of element for set interface . Right? Atleast the programmes i tried it is the case with them.

Thats right, if you do emp1.setEmpId(2) or something similar, the iteration will not fail.

...it should throw the concurrent modification exception but it did not . Not sure why?

It only throws the exception if you modify the list. Keep in mind that the list contains references to objects. If you modify the objects, the references does not change, thus the list does not change.

like image 183
aioobe Avatar answered Oct 12 '22 05:10

aioobe


Unless it is access by two different threads, there is no danger in modifying the elements within the list. your for does not throw concurrent modification exception because you are modifying the elements of the set, not the set itself.

like image 35
fastcodejava Avatar answered Oct 12 '22 04:10

fastcodejava