Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collections remove method doesn't give Concurrent Modification Exception

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?

like image 778
coder25 Avatar asked May 07 '14 06:05

coder25


People also ask

How do I get rid of concurrent modification exception?

To Avoid ConcurrentModificationException in single-threaded environment. You can use the iterator remove() function to remove the object from underlying collection object. But in this case, you can remove the same object and not any other object from the list.

Why does iterator remove Do not throw ConcurrentModificationException?

Notice that iterator. remove() doesn't throw an exception by itself because it is able to update both the internal state of itself and the collection. Calling remove() on two iterators of the same instance collection would throw, however, because it would leave one of the iterators in an inconsistent state.

What is concurrent modification exception?

The ConcurrentModificationException occurs when an object is tried to be modified concurrently when it is not permissible. This exception usually comes when one is working with Java Collection classes. For Example - It is not permissible for a thread to modify a Collection when some other thread is iterating over it.

What is ConcurrentHashMap exception?

The exception means "the structure of the map was modified while I was iterating on it, so I don't know what to do now". To allow concurrent modification and iteration on the map inside the sender object, that map should be a ConcurrentHashMap .


1 Answers

The fail-fast behavior of an iterator cannot be guaranteed. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. The iterator may or may not detect the invalid usage. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

like image 118
Suren Raju Avatar answered Oct 30 '22 11:10

Suren Raju