Delphi 2009泛型容器单元(Generics.Collections)[4]: TDictionary<T>

TDictionary 类似哈希表.

本例效果图:

代码文件: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 新增的泛型容器单元}
var
 Dictionary: TDictionary<Cardinal,string>; 
 {定义一个泛型 TDictionary 类, 指定有 Cardinal、string 构成}
{建立}
procedure TForm1.FormCreate(Sender: TObject);
begin
 Dictionary := TDictionary<Cardinal,string>.Create;
 Memo1.Clear;
 Button1.Caption := Button1.Caption + ' 添加';
 Button2.Caption := Button2.Caption + ' 删除';
 Button3.Caption := Button3.Caption + ' 尝试取值';
 Button4.Caption := Button4.Caption + ' 清空';
 Edit1.Clear;
 Edit2.Clear;
 Edit1.NumbersOnly := True;
end;
{释放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
 Dictionary.Free;
end;
{添加}
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 是否存在}
 v := Dictionary.ContainsValue(value); {Value 是否存在}
 
 if not k then
 begin
  Dictionary.Add(key, value);
  Memo1.Lines.Add(Format('%d=%s', [key, value])); {同步显示}
 end;
 if k and not v then
 begin
  str := Format('key 已存在: %d=%s; 是否修改其值?', [key, Dictionary[key]]);
  if MessageBox(0, PChar(str), '提示', MB_OKCANCEL or MB_ICONQUESTION) = mrOk then
  begin
   //Dictionary[key] := value; {Dictionary[key] = Dictionary.Item[key]}
   Dictionary.AddOrSetValue(key, value);    {也可使用上一句}
   Memo1.Lines.Values[IntToStr(key)] := value; {同步显示}
  end;
 end;
 if k and v then
 begin
  str := Format('%d=%s 已存在, 不能重复添加', [key, value]);
  MessageBox(0, PChar(str), '错误', MB_OK + MB_ICONHAND);
 end;
 Text := IntToStr(Dictionary.Count);
end;
{删除: 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 不存在', [key]);
  Exit;
 end;
  
 Dictionary.Remove(key);
 Text := IntToStr(Dictionary.Count);
 
 {同步显示}
 i := Memo1.Lines.IndexOfName(IntToStr(key));
 if i > -1 then Memo1.Lines.Delete(i);
end;
{尝试取值: 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 已存在, 其值是: %s', [key, value])
 else
  ShowMessageFmt('key: %d 不存在', [key])
end;
{清空: Clear}
procedure TForm1.Button4Click(Sender: TObject);
begin
 Dictionary.Clear;
 Text := IntToStr(Dictionary.Count);
 Memo1.Clear; {同步显示}
end;
end.

时间: 2024-10-31 20:13:51

Delphi 2009泛型容器单元(Generics.Collections)[4]: TDictionary&lt;T&gt;的相关文章

Delphi 2009泛型容器单元(Generics.Collections)[1]: TList

Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含了以下实用类: TList<T> TQueue<T> TStack<T> TDictionary<TKey,TValue> TObjectList<T> TObjectQueue<T> TObjectStack<T> TObj

Delphi 2009泛型容器单元(Generics.Collections)[3]: TStack&amp;lt;T&amp;gt;

TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性: Push(压栈).Pop(出栈).Peek(查看下一个要出栈的元素); Count(元素总数). 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdC

Delphi 2009泛型容器单元(Generics.Collections)[2]: TQueue&amp;lt;T&amp;gt;

TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TQueue 主要有三个方法.一个属性: Enqueue(入列).Dequeue(出列).Peek(查看下一个要出列的元素); Count(元素总数). 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialog

Delphi 2009泛型容器单元(Generics.Collections)[5]: TObject...&amp;lt;T&amp;gt; 系列

已经有了: TList<T>.TQueue<T>.TStack<T>.TDictionary<TKey,TValue> 为什么还有: TObjectList<T>.TObjectQueue<T>.TObjectStack<T>.TObjectDictionary<TKey,TValue> ? 还记得 Classes.TList 和 Contnrs.TObjectList 的主要区别吗? 如果元素是对象, Con

Delphi 2009的反射单元(ObjAuto)

ObjAuto 单元应该算是对 TypInfo 单元的功能扩展吧? 它提供了 5 个方法: GetMethods.GetMethodInfo.CreateMethodPointer.ReleaseMethodPointer.ObjectInvoke 通过 GetMethods.GetMethodInfo 可以获取类公用成员的详细信息. 通过 TypInfo 只能获取 published 区中成员的信息(例子); 通过 ObjAuto 也能获取 public 区的成员信息. 本例效果图: 本例有两

Delphi 2009之TStringBuilder类[5]: Chars[]属性与CopyTo方法

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Se

Delphi 2009之TStringBuilder类[4]: Insert与Remove

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R

Delphi 2009之TStringBuilder类[3]: Replace

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender:

Delphi 2009之TStringBuilder类[2]: Append与AppendFormat

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; procedure Bu