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 Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses Generics.Collections, Generics.Defaults; //Create 可以指定元素数, 这样可以提前分配空间以加快速度 procedure TForm1.Button1Click(Sender: TObject); var Dictionary: TDictionary<string,Integer>; begin Dictionary := TDictionary<string,Integer>.Create(3); Dictionary.Add('n1', 111); Dictionary.Add('n2', 222); Dictionary.Add('n3', 333); Dictionary.Free; end; //Create 可以有一个 IEqualityComparer 参数, 用于判断 Key 怎样才是相同 procedure TForm1.Button2Click(Sender: TObject); var Dictionary: TDictionary<string,Integer>; EqualityComparer: IEqualityComparer<string>; {相等对比器} begin {通过 IEqualityComparer 让 TDictionary 的 Key 忽略大小写} EqualityComparer := TEqualityComparer<string>.Construct( function(const Left, Right: string): Boolean begin Result := LowerCase(Left) = LowerCase(Right); end, function(const Value: string): Integer begin Result := StrToIntDef(Value, 0); {我暂时不知道这个函数的作用, 随便写的} end ); Dictionary := TDictionary<string,Integer>.Create(EqualityComparer); Dictionary.Add('n1', 111); Dictionary.Add('n2', 222); Dictionary.Add('n3', 333); {如果不是如上建立, 下面这句将会产生一个新元素, 而不是更新 n1 的值} Dictionary.AddOrSetValue('N1', 123); ShowMessage(IntToStr(Dictionary['n1'])); {123} Dictionary.Free; end; //Create 可以同时指定上面两个参数 procedure TForm1.Button3Click(Sender: TObject); var Dictionary: TDictionary<string,Integer>; EqualityComparer: IEqualityComparer<string>; begin EqualityComparer := TEqualityComparer<string>.Construct( function(const Left, Right: string): Boolean begin Result := LowerCase(Left) = LowerCase(Right); end, function(const Value: string): Integer begin Result := StrToIntDef(Value, 0); end ); Dictionary := TDictionary<string,Integer>.Create(9, EqualityComparer); Dictionary.Add('n1', 111); Dictionary.AddOrSetValue('N1', 123); ShowMessage(IntToStr(Dictionary['n1'])); {123} {指定元素数后, 用不了的可以释放} Dictionary.TrimExcess; Dictionary.Free; end; //可通过另一个 TDictionary 建立一个新的 TDictionary procedure TForm1.Button4Click(Sender: TObject); var Dictionary, DictionaryTmp: TDictionary<string,Integer>; pair: TPair<string,Integer>; begin DictionaryTmp := TDictionary<string,Integer>.Create(); DictionaryTmp.Add('n1', 111); DictionaryTmp.Add('n2', 222); DictionaryTmp.Add('n3', 333); {通过另一个 TDictionary 建立} Dictionary := TDictionary<string,Integer>.Create(DictionaryTmp); {遍历看看} for pair in Dictionary do ShowMessage(IntToStr(Dictionary[pair.Key])); {222 333 111} DictionaryTmp.Free; Dictionary.Free; end; //通过另一个 TDictionary 建立时, 可同时再指定 "相等对比器" procedure TForm1.Button5Click(Sender: TObject); var Dictionary, DictionaryTmp: TDictionary<string,Integer>; pair: TPair<string,Integer>; EqualityComparer: IEqualityComparer<string>; begin DictionaryTmp := TDictionary<string,Integer>.Create(); DictionaryTmp.Add('n1', 111); DictionaryTmp.Add('n2', 222); DictionaryTmp.Add('n3', 333); {再做个对比器} EqualityComparer := TEqualityComparer<string>.Construct( function(const Left, Right: string): Boolean begin Result := LowerCase(Left) = LowerCase(Right); end, function(const Value: string): Integer begin Result := StrToIntDef(Value, 0); end ); {通过另一个 TDictionary 建立, 同时更换对比器} Dictionary := TDictionary<string,Integer>.Create(DictionaryTmp, EqualityComparer); {测试对比器} Dictionary.AddOrSetValue('N1', 999); {遍历看看} for pair in Dictionary do ShowMessage(IntToStr(Dictionary[pair.Key])); {222 333 999} DictionaryTmp.Free; Dictionary.Free; end; end.