• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterators from Vector are fail-safe?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Arek Sokolowski
 
reply
    Bookmark Topic Watch Topic
  • New Topic