詳細測定Generics Collections TDictionary(3):TPairEnumerator、TKeyEnumerator、TValueEnumerator、ExtractPair

1652 ワード

// 
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 *.dfm}

uses Generics.Collections;

procedure TForm1.Button1Click(Sender: TObject);
var
  Dictionary: TDictionary<string,Integer>;
  ds: TDictionary<string,Integer>.TPairEnumerator;
  ks: TDictionary<string,Integer>.TKeyEnumerator;
  vs: TDictionary<string,Integer>.TValueEnumerator;
begin
  Dictionary := TDictionary<string,Integer>.Create();

  Dictionary.Add('n1', 111);
  Dictionary.Add('n2', 222);
  Dictionary.Add('n3', 333);


  ds := Dictionary.GetEnumerator;
  while ds.MoveNext do ShowMessageFmt('%s:%d', [ds.Current.Key, ds.Current.Value]);
  {n2:222  n3:333  n1:111}

  ks := Dictionary.Keys.GetEnumerator;
  while ks.MoveNext do ShowMessageFmt('%s', [ks.Current]);
  {n2  n3  n1}

  vs := Dictionary.Values.GetEnumerator;
  while vs.MoveNext do ShowMessageFmt('%d', [vs.Current]);
  {222  333  111}    

  { ExtractPair  ,  ;   }
  Dictionary.ExtractPair('n1');
  ShowMessage(IntToStr(Dictionary.Count)); {2}
  
  Dictionary.Free;
end;

end.