[Kaffe] I am getting a ConcurrentModificationException

Moses DeJong dejong at cs.umn.edu
Tue Sep 28 07:04:00 PDT 1999


On Mon, 27 Sep 1999, Archie Cobbs wrote:

> 
> Moses DeJong writes:
> > I wrote a little test program to double check that what I was doing
> > was correct. I was using the Hashtable.keys() method to iterate a
> > table and remove each key like the following example.
> > 
> > import java.util.*;
> > 
> > public class HashTest {
> >     public static void main(String[] argv) {
> > 	Hashtable h = new Hashtable();
> > 	Enumeration search;
> > 	String elem;
> > 
> > 	h.put("one", new Integer(1));
> > 	h.put("two", new Integer(2));
> > 	h.put("three", new Integer(3));
> > 
> > 	for (search = h.keys(); search.hasMoreElements() ; ) {
> > 	    elem = (String) search.nextElement();
> > 
> > 	    System.out.print("elem is \"" + elem + "\" value is " +
> > h.get(elem) + " ... ");
> > 	    h.remove(elem);
> > 	    System.out.println("removed");
> > 	}
> >     }
> > }

This seems to be documented in the 1.2 HTML pages, but that does not
mean it is well documented.

FROM:
http://java.sun.com/products/jdk/1.2/docs/api/java/util/Hashtable.html

The Iterators returned by the iterator and listIterator methods of the
Collections returned by all of Hashtable's
"collection view methods" are fail-fast: if the Hashtable 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. Thus, in the face of concurrent
modification, the Iterator fails quickly and
cleanly, rather than risking arbitrary, non-deterministic behavior at an
undetermined time in the future. The
Enumerations returned by Hashtable's keys and values methods are not
fail-fast.


later
mo

> FYI-
> This works because the legacy routines (Hashtable.keys() and
> Hashtable.elements()) take a "snapshot" of the hashtable (or
> so it seems; this is not documented anywhere).
> 
> A more efficient way to do the above loop, using the Collections
> framework, would be:
> 
>     import java.util.*;
>     public class HashTest {
> 	public static void main(String[] argv) {
> 	    Hashtable h = new Hashtable();
> 
> 	    h.put("one", new Integer(1));
> 	    h.put("two", new Integer(2));
> 	    h.put("three", new Integer(3));
> 
> 	    for (Iterator i = h.entrySet().iterator(); i.hasNext() ; ) {
> 		Map.Entry entry = (Map.Entry)i.next();
> 		Object key = entry.getKey();
> 		Object value = entry.getValue();
> 
> 		System.out.print("key=" +key+ " value=" +value);
> 		i.remove();
> 		System.out.println(" removed");
> 	    }
> 	}
>     }
> 
> This avoids two ineffiencies:
> 
>   - Copying all of the keys into a separate list in Hashtable.keys()
>   - Looking up each value for every key
> 
> -Archie
> 
> ___________________________________________________________________________
> Archie Cobbs   *   Whistle Communications, Inc.  *   http://www.whistle.com
> 



More information about the kaffe mailing list