Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterators from Vector are fail-safe?

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Arek Sokolowski
Greenhorn
Posts: 8
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Arek Sokolowski
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic