Jacek Laskowski 57 Posted December 19, 2019 (edited) I have a spring collection, like a IList<T>. I need to remove all items from it that meet the condition: type TFoo = interface function Value : Integer; end; var List : IList<TFoo>; begin i := 0; while i < List.Count do begin if List[i].Value < 100 then List.Delete(i) else Inc(i); end; end; Can it be done faster, better? And how can I do that for IDictionary<K, V>? Edited December 19, 2019 by Jacek Laskowski Share this post Link to post
Der schöne Günther 316 Posted December 19, 2019 I would do it like this: procedure p(); var itemsToDelete: IEnumerable<TFoo>; begin itemsToDelete := List.Where(valueIsSmallerThan100); List.RemoveRange(itemsToDelete); end; function valueIsSmallerThan100(const item: TFoo): boolean; begin Result := (item.getValue < 100); end; Share this post Link to post
pietrt 1 Posted December 19, 2019 (edited) You can also use the RemoveAll method: list.RemoveAll( function(const foo: TFoo): boolean begin result := foo.Value < 100; end); dict.RemoveAll( function(const aPair: TPair<string,TFoo>): boolean begin result := aPair.Value.Value < 100; end); Edited December 19, 2019 by pietrt 1 Share this post Link to post
Der schöne Günther 316 Posted December 19, 2019 Personally, I like to store those things in a local variable as it makes it easier to debug, but that's probably just personal taste. Share this post Link to post
Stefan Glienke 2002 Posted December 19, 2019 (edited) RemoveAll is the way to go - it is optimized internally for lists (at least since 2.0/develop) - not yet for dictionaries but there it more depends on what is the criteria for deleting items as remove by key is already O(1) The approach with Where/Remove does not work because the result of Where is lazy evaluated and will raise an exception once the source gets modified (after the first deleted item). Even when calling ToArray to manifest the collection of to removed items the Remove will be a waste of time because items would be searched all over. Edited December 19, 2019 by Stefan Glienke Share this post Link to post