Delphi自己定义的string类型的Map,类似java的HashMap

//自己定义的string类型的Map,类似java的HashMap<String,String>
unit StrMap;

interface
uses
  SysUtils, Classes,System.Contnrs;

type
  TStrMap = class(TPersistent)
    FKeyList: TStrings;
    FStrList: TStrings;
  private
    //procedure SetItems(Key: string; const Value: string);
  public
    constructor Create;
    //记得用完之后,MyMap.Free;
    destructor Destroy; override;
    //返回总个数
    function GetCount: Integer;
    //返回键对应的值,没有返回空字符串
    function Get(Key: string): string;
    //属性,元素
    //通过MyMap['key']这个方法用
    property Items[Key: string]: string read Get ; default;
    //属性,总数
    property Count: Integer read GetCount;
    //放入
    procedure Put(Key, Str: string);
    //移除
    procedure Remove(Key: string);
    //移除全部
    procedure RemoveAll();
  end;

implementation

{ TStringHashedTable }

procedure TStrMap.Put(Key, Str: string);
var KeyIndex:Integer;
begin
  KeyIndex := FKeyList.IndexOf(Key);
  if KeyIndex <> -1 then  //如果找到了相同KEY,设值。
  begin
    FStrList[KeyIndex] := Str;
    Exit;
  end;
  //如果没找到了相同KEY,添加
  FKeyList.Add(Key);
  FStrList.Add(Str);
end;

constructor TStrMap.Create;
begin

  FKeyList := TStringList.Create;
  FStrList := TStringList.Create;
end;

procedure TStrMap.Remove(Key: string);
var
  KeyIndex: Integer;
begin
  KeyIndex := FKeyList.IndexOf(Key);
  FKeyList.Delete(KeyIndex);
  FStrList.Delete(KeyIndex);
end;

procedure TStrMap.RemoveAll;
begin
  FKeyList.Clear;
  FStrList.Clear;
end;

destructor TStrMap.Destroy;
begin
  FStrList.Free;
  FKeyList.Free;
  inherited Destroy;
end;

function TStrMap.GetCount: Integer;
begin
  Result := FKeyList.Count;
end;

function TStrMap.Get(Key: string): string;
var
  KeyIndex: Integer;
begin
  KeyIndex := FKeyList.IndexOf(Key);
  if KeyIndex = -1 then
    Result := ''
  else
    Result := FStrList[KeyIndex];
end;


end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值