Peter,

several approaches have been shown already. I'd like to add:

* Iterate over a copy and remove from the original (slower because of lookup in the original) * If possible, sort the collection and use something like [col atEnd] whileTrue:/whileFalse: with removeFirst/removeLast
* Select: a collection of items to be removed and then do a removeAll:
* Use #- to remove one collection from the other
* Iterate over the original and add only the wanted objects to a new collection which will then be used in the future - Streams may be helpful here * I have never used XStreams, but there may even be some nice options to find in it

HTH

Joachim



Am 02.04.15 um 09:33 schrieb Peter Uhnák:
On Thu, Apr 2, 2015 at 8:30 AM, Joachim Tuchel <jtuc...@objektfabrik.de <mailto:jtuc...@objektfabrik.de>> wrote:

    In general, removing from a Collection while iterating through it
    leads to undefined results. The same is true for adding.

That is a question of approach; apart from mentioned #copy I could also do something like
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
col := #(1 2 3 4 5) asOrderedCollection.
[col isNotEmpty] whileTrue: [ col remove: col first ].
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if I had an Iterator I could also reset it after each change.

What I am asking is what the best approach would be - I want do some processing for each item and remove it from the collection after it is done.

Peter

Reply via email to