I have read an article regarding removing elements from Collections from this link
As per my understanding iterator remove method prevents Concurrent modification exception then remove method of Collection.But when i try to run the below codde i am unable to get concurrentmoficationexception
List dayList= new ArrayList();
dayList.add("Sunday");
dayList.add("Monday");
dayList.add("Tuesday");
dayList.add("Wednesday");
dayList.remove("Tuesday");
Iterator itr=dayList.iterator();
while(itr.hasNext())
{
Object testList=itr.next();
if(testList.equals("Monday"))
{
dayList.remove(testList);
}
}
System.out.println(dayList);
}
- As per javadoc the ConcurrentModicationException is thrown when we try to do any modification during iteartion.I am using collections remove method,but still there is no exception.But if I comment line dayList.remove("Tuesday");,exception is thrown.
Can anyone explain what is happening behind the scene in this code?