On Mon, 27 Sep 1999, Chris Gray wrote:

> 
> On Mon, 27 Sep 1999, Moses DeJong wrote:
> 
> > 
> > > However, this exception indicates that your program potentially
> > > has a bug.. it's trying to iterate through the elements while
> > > changing the contents of the Hashtable at the same time.
> > >
> > > -Archie
> > 
> > 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.
> > 
> > [...]      
> >     for (search = h.keys(); search.hasMoreElements() ; ) {
> >         elem = (String) search.nextElement();
> > [...]
> >         h.remove(elem);
> >         System.out.println("removed");
> >     }


This code has to work, there is no other way to iterate over the
elements in a Hashtable with the 1.1 API. JDK 1.2 introduces new
iterator APIs, but the only way to iterate over the elements in
a Hashtable is to use the elements() or keys() methods. Besides, the
Enumeration that is returned from the Hashtable.keys() method is
a different object, it should not break if the internals of
a Hashtable change.

later
mo

> According to my reading of the specs, this code does not necessarily work:
> an enumeration is not a snapshot, and deleting stuff as you iterate
> through the enumeration is a bit like sawing off the branch you are
> sitting on.  Some implementations of keys() would fail in such
> circumstances - especially if resizing of the hashtable occurred as a
> result of all those removals - and still be correct implemetations.
> Someone please correct me if I'm wrong here.
> 
>  -- 
> 
>   Chris Gray            [EMAIL PROTECTED]    [EMAIL PROTECTED]
> 

Reply via email to