File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Iterators from Vector are fail-safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterators from Vector are fail-safe?" Watch "Iterators from Vector are fail-safe?" New topic
Author

Iterators from Vector are fail-safe?

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
I read below statement on Javadoc for Vector which said
"The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException."

Here, what does it mean by structurally modifying the the Vector collection?

I am slow but sure
Arek Sokolowski
Greenhorn

Joined: Jan 12, 2011
Posts: 8

'Structural modification' is a modification, which changes set (or order) of objects in the Vector.
These are all methods from add*, remove* and set* families.
In another words: it is any modification, which modifies content of Iterator.

Note, that objects within the Vector may be mutable; their inner state modifiction is not a structural modification - in fact, it does not have anything with Vector.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Iterator has been created to iterate the Vector...

After that, If the vector is modified using its own add() / remove() methods (structurally modified), to prevent the modifications by the structural way,ConcurrentModificationException is thrown... So that Structural Modification leads to the change in Objects arrangement and now the Iteration order would become old for Iterator...

If you want to keep it updated, after each structural modification, iterator() method can be called... This is very inefficient way...

So after getting iterator() method, add()/remove() can be done with Iterator's so that iteration order is updated within itself as well as the List

So that addition/removal are done in full Iterator's control and attention...

SCJP 6 [SCJP - Old is Gold]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Arek Sokolowski wrote:'Structural modification' is a modification, which changes set (or order) of objects in the Vector.
These are all methods from add*, remove* and set* families.
In another words: it is any modification, which modifies content of Iterator.

This is incorrect. In particular, set methods do not cause structural modification, except for setLength(). You can verify this by looking at the source code for Vector and other collections - look at how the modCount variable (declared in java.util.AbstractList) is used. The methods that call modCount++ are the ones that can cause ConcurrentModificationException if called during iteration from outside the iterator. These include all add* and remove* methods, as well as ensureCapacity(), insertElementAt() (which is just an old name for add()), setSize(), and trimToSize().

Going back to the documentation though, the API tells us (under the subList() method):
Structural modifications are those that change the size of the List, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.

The first part of this is pretty clear, and accounts for why add() and remove() are structural modifications, while set() is not. The "otherwise perturb it" section is more vague, but it appears to account for ensureCapacity() and trimToSize() being on the list.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Welcome to the Ranch Arek Sokolowski
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterators from Vector are fail-safe?