Showing posts with label TObjectList. Show all posts
Showing posts with label TObjectList. Show all posts

Thursday, 17 October 2019

Will the generic TObjectList bug with serialising to JSON get fixed?

When I try to serialise a generic TObjectList to JSON doing something like this:

Response.Content  := TJSON.ObjectToJsonString(MyObjectList);

I get the error Type tkPointer is not currently supported. I am using 10.3 and I believe this bug (21685) has been around since XE8. I am currently having to think of workarounds and trying to find the best solution. It would be really useful if they would fix this bug, or give some idea of when it will be fixed, does anyone know?

Thursday, 21 September 2017

TObjectList vs TObjectDictionary

When I need to store a list of objects I usually use a TObjectList (Generics.Collections), but I was speaking to another developer who uses TObjectDictionary to store a list of objects. I searched the net for when to use a TObjectDictionary instead of a TObjectList and vice-versa, but could not find any definitive answers. I decided to write a little test app to understand the difference, this simply did the following:

  • Created 1 million objects for each list and added them to the list.
  • Cleared the list of the 1 million objects.
  • Populated the list with 1 million newly created objects.
  • Found 100 objects in the list.
Below are the results, time is in ms:


ObjectList Clear - 47
ObjectList Populate - 344
ObjectList Find - 78
ObjectList Find - 78
ObjectList Find - 78
ObjectList Find - 79

ObjectDictionary Clear - 265
ObjectDictionary Populate - 579
ObjectDictionary Find - 0
ObjectDictionary Find - 0
ObjectDictionary Find - 0
ObjectDictionary Find - 0

As you can see the TObjectDictionary was slower to clear and populate the list, but was faster finding the 100 objects. This is most likely due to the TObjectDictionary being derived from TDictionary which is a hash table and is optimised for lookups.

So, generally from these results I am happy sticking with TObjectList, but if I need to store a lot of objects and also need to find a lot of objects then using a TObjectDictionary is a better choice. 

Monday, 3 February 2014

Using TObjectList in Generics.Collections

In 2009 a new TObjectList was introduced in Generics.Collections, the old TOjectList was still available in the Contnrs unit, but the new one was more strongly typed and you need to specify the class of object the the list will be populated with. This means a reduction in type casting. Here is an example of how to use it:

TMyListClass = class(TObject)
   private
        FEntries: TObjectList<TMyEntry>;
   public
        property Entries: TObjectList<TMyEntry> read FEntries                write FEntries;

        function GetEntry(aIndex: integer):TMyEntry; 

        constructor Create;
        destructor Destroy; override;         
   end;

implementation

constructor TMyListClass.Create;
begin
    inherited Create;
    FEntries := TObjectList<TMyEntry>.Create();
end;

destructor TMyListClass.Destroy;
begin
    FEntries.Free;
    inherited Destroy;
end;

function TMyListClass.GetEntry(aIndex: integer):TMyEntry;
begin
   // You don't need to typecast
   // Old way 
   // if (FEntries.Items[aIndex] is TMyEntry) then
   //    Result (FEntries.Items[aIndex] as TMyEntry; 
   Result := FEntries.Items[aIndex];
end;