aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ConcurrrentModificationErro Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ConcurrrentModificationErro" Watch "ConcurrrentModificationErro" New topic
Author

ConcurrrentModificationErro

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

Line 1 is producing concurrentacesserror. How can I avoid. My requirement is that while iterating through a collection, I need to add elements to collection.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Barkat, this may be not the most elegant solution, but anyway:

[ October 14, 2003: Message edited by: Vad Fogel ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hello Vad,
Thanks for your input. I was browsing through the API documentation very briefly. My understanding is that LinkHashSet does not allow modification of base set while iterating though the set. However, listIterator of LinkedList does allow that. So I modified my program as follows and it worked:
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Just be careful because the output may be inconsistent:

For example, it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Barkat, here is an idea. You might add all the new elements to a new LinkedHashSet. Then when your iterator gets to the end, do an addAll from the new set to the old one. (That does seem inefficient, 2 adds for every element.)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Here is an excript from Sun's tutorial:
Iterators
As you'd expect, the Iterator returned by List's iterator operation returns the elements of the list in proper sequence. Additionally, List provides a richer iterator, called a ListIterator, that allows you to traverse the list in either direction, modify the list during iteration, and obtain the current position of the iterator. The ListIterator interface is summarized below (including the three methods it inherits from Iterator):
public interface ListIterator extends Iterator {
boolean hasNext();
Object next();
boolean hasPrevious();
Object previous();
int nextIndex();
int previousIndex();
void remove(); // Optional
void set(Object o); // Optional
void add(Object o); // Optional
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ConcurrrentModificationErro
 
Similar Threads
Interface Set's get() method>>>>
Collection VS ArrayList.
what's the error?
Whizlab mock: ClassCasting
HashSet and sorting