Every fail fast collection has a modCount field, to represent how many times the collection has changed/modified. So at every modification of this collection we increment the modCount value. For example the modCount is incremented in below cases:
So everytime there is some change in the collection structure, the mod count is incremented.
The iterator stores the modCount value in the initialization as below:
int expectedModCount = modCount;
While the iteration is going on, expectedModCount will have old value of modCount. If there is any change made in the collection, the modCOunt will change and then an exception is thrown using:
This code is used in most of the iterator methods:
Hope you have enjoyed reading How fail fast iterator comes to know that the internal structure is modified?. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!
Share this page on WhatsApp