详测 Generics Collections TDictionary(5): 多种 Create 手段
详测 Generics Collections TDictionary(5): 多种 Create 手段
涂孟超 发表于3年前
详测 Generics Collections TDictionary(5): 多种 Create 手段
  • 发表于 3年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

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.

 
 
 
 
 

 

 

  
共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: