Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDictionary strange behaviour in delphi [duplicate]

Tags:

delphi

I have the following issue:

I'm populating a Dictionary with some values and would like to get them back in the same exact order I populated them.

Somehow it doesn't seem to be working, when I iterate through items they are sorted by a non logic order (IMDO).

Once the following program is run:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.Generics.Collections;
var
  Dictionary: TDictionary<LongWord, string>;
  aPair: TPair<LongWord, string>;
begin
  Dictionary := TDictionary<LongWord, string>.Create;
  Dictionary.add(1, 'First Item');
  Dictionary.add(2, 'Second Item');
  Dictionary.add(3, 'Third Item');
  Dictionary.add(4, 'Forth Item');
  Dictionary.add(5, 'Fifth Item');
  Dictionary.add($FFFFFFFF, 'Longword Item');

  for aPair in Dictionary do
    writeln(aPair.Value);

  readln;
end.

I got the following results:

Forth Item
Longword Item
First Item
Third Item
Second Item
Fifth Item

Am I doing something wrong?

Tested on XE5 and Rad Studio Berlin, same results.

Thanks for help.

like image 326
Marco Carboni Avatar asked Jan 01 '17 10:01

Marco Carboni


People also ask

What is a tdictionary class in Delphi?

Introduced in Delphi 2009, the TDictionary class, defined in the Generics.Collections unit, represents a generic hash table type collection of key-value pairs. Generic types, also introduced in Delphi 2009, allow you to define classes that don't specifically define the type of data members. A dictionary is, in a way, similar to an array.

What is tdictionary generics collection in Delphi?

How to use TDictionary Generics Collection in Delphi? TDictionary is used to store key-value pair in Delphi. TDictionary is an example of Generic Collections in Delphi. Below is the simple Delphi program to illustrate the concept of TDictionary in Delphi.

What is thashedstringlist in Delphi?

a TDictionary <string,Integer> (available since a Delphi 2009) Just in case you did not know about THashedStringList: It is a TStringList descendant declared in System.IniFiles. It’s used to speed up access to TMemIniFile. (EDIT: As Uwe Raabe pointet out, that’s no longer true.

What is tdictionary in SQL Server?

Zarko Gajic is experienced in SQL and has working knowledge of DB systems such as MS SQL Server, Firebird, Interbase, and Oracle. He is also proficient in XML, DHTML, and JavaScript. Introduced in Delphi 2009, the TDictionary class, defined in the Generics.Collections unit, represents a generic hash table type collection of key-value pairs.


1 Answers

The Delphi dictionary class is unordered. It behaves as designed. If you wish to maintain the order then you need to use an ordered data structure. For instance an array or a list.

If you wish to have ordered access as well as O(1) lookup then you would need to maintain two collections in tandem. One an array or list for ordered access, and in tandem a dictionary for O(1) lookup. Or, alternatively, find a library that offers an ordered dictionary implementation.

like image 87
David Heffernan Avatar answered Oct 12 '22 13:10

David Heffernan