Tdictionary resembles a hash table.
This example effect chart:
Code files:Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
TForm1 = Class (Tform)
Memo1:tmemo;
Edit1:tedit;
Edit2:tedit;
Button1:tbutton;
Button2:tbutton;
Button3:tbutton;
Button4:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Procedure Button4click (Sender:tobject);
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Uses generics.collections; {Delphi 2009 New generic Container Unit}
Var
dictionary:tdictionary<cardinal,string>;
{Defines a generic tdictionary class, specifying cardinal, string composition}
Establish
Procedure Tform1.formcreate (Sender:tobject);
Begin
Dictionary: = Tdictionary<cardinal,string>. Create;
Memo1.clear;
Button1.caption: = button1.caption + ' add ';
Button2.caption: = button2.caption + ' delete ';
Button3.caption: = button3.caption + ' Try to take value ';
Button4.caption: = button4.caption + ' empty ';
Edit1.clear;
Edit2.clear;
Edit1.numbersonly: = True;
End
Release
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Dictionary.free;
End
Add
Procedure Tform1.button1click (Sender:tobject);
Var
key:cardinal;
value:string;
str:string;
K,v:boolean;
Begin
Key: = Strtointdef (edit1.text, 0);
Value: = Edit2.text;
If value = ' then value: = ' Null ';
K: = Dictionary.containskey (key); {Key exists}
V: = dictionary.containsvalue (value); {Value exists}
If not K then
Begin
Dictionary.add (key, value);
MEMO1.LINES.ADD (Format ('%d=%s ', [key, value]); {Sync Display}
End
If k and not v then
Begin
str: = Format (' Key already exists:%d=%s; modify its value? ', [Key, Dictionary[key]]);
If MessageBox (0, Pchar (str), ' hint ', mb_okcancel or mb_iconquestion) = Mrok Then
Begin
Dictionary[key]: = value; {Dictionary[key] = Dictionary.item[key]}
Dictionary.addorsetvalue (key, value); {You can also use the previous sentence}
MEMO1.LINES.VALUES[INTTOSTR (key)]: = value; {Sync Display}
End
End
If K and V then
Begin
str: = Format ('%d=%s already exists, cannot be repeatedly added ', [key, value]);
MessageBox (0, Pchar (str), ' ERROR ', MB_OK + Mb_iconhand);
End
Text: = IntToStr (Dictionary.count);
End
{Delete: Remove}
Procedure Tform1.button2click (Sender:tobject);
Var
Key:integer;
I:integer;
Begin
Key: = Strtointdef (edit1.text, 0);
If not Dictionary.containskey (key) Then
Begin
SHOWMESSAGEFMT (' key:%d does not exist ', [key]);
Exit;
End
Dictionary.remove (key);
Text: = IntToStr (Dictionary.count);
{Sync Display}
I: = Memo1.Lines.IndexOfName (IntToStr (key));
If i >-1 then Memo1.Lines.Delete (i);
End
{Trying to value: TryGetValue}
Procedure Tform1.button3click (Sender:tobject);
Var
Key:integer;
value:string;
Begin
Key: = Strtointdef (edit1.text, 0);
If Dictionary.trygetvalue (key, value) then
SHOWMESSAGEFMT (' key:%d already exists, its value is:%s ', [key, value])
Else
SHOWMESSAGEFMT (' key:%d does not exist ', [key])
End
{Empty: Clear}
Procedure Tform1.button4click (Sender:tobject);
Begin
Dictionary.clear;
Text: = IntToStr (Dictionary.count);
Memo1.clear; {Sync Display}
End
End.