Hashtable: remove elements while Enumerating  
Author Message
Harald Hein





PostPosted: 2003-9-1 0:15:00 Top

java-programmer, Hashtable: remove elements while Enumerating "Chris Berg" wrote:

> 1) Make a new Hashtable, copy all the 'good' entries into the new
> one, then replace the old one with the new one. This MAY be bad if
> another class still holds a reference to the old table.
>
> 2) make a new Vector in which you remember all keys of entries
> that shall be removed, and then remove them AFTER having finished
> the iteraton.

3) Copy (clone()) the Hashtable. Iterate over the copy. If you find an
entry in the copy that should be removed, remove it from the original.
Make sure that no other thread works the original Hashtable while you
enumerate over the copy and manipulate the original.
 
Chris Berg





PostPosted: 2003-9-1 1:00:00 Top

java-programmer >> Hashtable: remove elements while Enumerating On 31 Aug 2003 16:14:34 GMT, Harald Hein <email***@***.com> wrote:

>"Chris Berg" wrote:
>
>> 1) Make a new Hashtable, copy all the 'good' entries into the new
>> one, then replace the old one with the new one. This MAY be bad if
>> another class still holds a reference to the old table.
>>
>> 2) make a new Vector in which you remember all keys of entries
>> that shall be removed, and then remove them AFTER having finished
>> the iteraton.
>
>3) Copy (clone()) the Hashtable. Iterate over the copy. If you find an
>entry in the copy that should be removed, remove it from the original.
>Make sure that no other thread works the original Hashtable while you
>enumerate over the copy and manipulate the original.

That would work, I see that. Except for the problem in 1) which will
be the same in 3).

Chris


 
Harald Hein





PostPosted: 2003-9-1 2:29:00 Top

java-programmer >> Hashtable: remove elements while Enumerating "Chris Berg" wrote:

> That would work, I see that. Except for the problem in 1) which will
> be the same in 3).

No, because you don't exchange the Hashtables. You just use a copy for
iteration.