Jump to content
Sign in to follow this  
Jacek Laskowski

[Spring4D] Remove collection elements

Recommended Posts

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 by Jacek Laskowski

Share this post


Link to post

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

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 by pietrt
  • Like 1

Share this post


Link to post

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 by Stefan Glienke

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×