结论:

做HashMap 映射 功能的时候 ,字典TDictionary 功能更强大,且效率更高,比如不仅仅可以存String,还可以存结构和类。

TDictionary类是一个name,value容器,内部是哈希索引,所以对于数据查找非常高效.

unit Unit5;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections, UListMap; type
TForm5 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; /// <summary>
/// 第一种方法:
/// 班级类,班级里有多个人,这里类里内置好每个人的英文名字和中文名字的映射
/// 通过英文名字可以找到中文,通过中文名字也可以找到英文
/// </summary>
TBanJi111 = class
const
/// <summary>
/// 这叫类常量,可以把人名 英文---->中文 定义在这里
/// </summary>
studentNameEnToCnListStr = 'XiaoLi=李飞刀,XiaoWang=王中王,XiaoZhang=张飞';
public
/// <summary>
/// 通过英文获取中文的方法
/// </summary>
function getNameCnByEn(const nameEn: string): string; /// <summary>
/// 通过中文获取应该的方法
/// </summary>
function getNameEnByCn(const nameCn: string): string;
end; /// <summary>
/// 第二种方法:
/// 用2个TStringList属性,
/// key,value做下置换
/// </summary>
TBanJi222 = class
private
FNameEnToCnList: TStringList;
FNameCnToEnList: TStringList;
procedure SetNameCnToEnList(const Value: TStringList);
procedure SetNameEnToCnList(const Value: TStringList);
public
constructor Create;
destructor Destroy; override;
property NameCnToEnList: TStringList read FNameCnToEnList write SetNameCnToEnList;
property NameEnToCnList: TStringList read FNameEnToCnList write SetNameEnToCnList;
end; /// <summary>
/// 第三种方法
/// 用泛型
/// </summary>
TBanJi333 = class
private
FNameEnToCnMap: TDictionary<string, string>;
FNameCnToEnMap: TDictionary<string, string>;
procedure SetNameCnToEnMap(const Value: TDictionary<string, string>);
procedure SetNameEnToCnMap(const Value: TDictionary<string, string>);
public
constructor Create;
destructor Destroy; override;
property NameCnToEnMap: TDictionary<string, string> read FNameCnToEnMap write SetNameCnToEnMap;
property NameEnToCnMap: TDictionary<string, string> read FNameEnToCnMap write SetNameEnToCnMap;
end; /// <summary>
/// 第四种方法
/// 用我自己封装的有序的泛型字典
/// </summary>
TBanJi444 = class
private
FNameEnToCnMap: TListMap<string, string>;
FNameCnToEnMap: TListMap<string, string>;
procedure SetNameCnToEnMap(const Value: TListMap<string, string>);
procedure SetNameEnToCnMap(const Value: TListMap<string, string>);
public
constructor Create;
destructor Destroy; override;
property NameCnToEnMap: TListMap<string, string> read FNameCnToEnMap write SetNameCnToEnMap;
property NameEnToCnMap: TListMap<string, string> read FNameEnToCnMap write SetNameEnToCnMap;
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button1Click(Sender: TObject);
var
bb: TBanJi111;
I: Integer;
start_miao: Int64;
response1, response2: string;
begin
bb := TBanJi111.Create;
try
//看下结果
Memo1.Lines.Clear;
Memo1.Lines.Add(bb.getNameCnByEn('XiaoLi'));
Memo1.Lines.Add(bb.getNameEnByCn('李飞刀')); //循环测试性能
start_miao := GetTickCount;
for I := to do
begin
//通过英文取中文
response1 := bb.getNameCnByEn('XiaoLi');
//通过中文取英文
response2 := bb.getNameEnByCn('李飞刀');
end;
Memo1.Lines.Add('总耗时(毫秒): ' + (GetTickCount - start_miao).ToString);
finally
bb.Free;
end;
end; { TBanJi } function TBanJi111.getNameCnByEn(const nameEn: string): string;
var
MyList: TStringList;
begin
MyList := TStringList.Create;
try
MyList.CommaText := studentNameEnToCnListStr;
Result := MyList.Values[nameEn];
finally
MyList.Free;
end;
end; function TBanJi111.getNameEnByCn(const nameCn: string): string;
var
MyList: TStringList;
I: Integer;
begin
MyList := TStringList.Create;
try
MyList.CommaText := studentNameEnToCnListStr;
for I := to (MyList.Count - ) do
begin
if nameCn = MyList.ValueFromIndex[I] then
begin
Result := MyList.Names[I];
Break;
end;
end;
finally
MyList.Free;
end;
end; { TBanJi2 } constructor TBanJi222.Create;
var
I: Integer;
begin
inherited Create;
Self.FNameEnToCnList := TStringList.Create;
Self.FNameCnToEnList := TStringList.Create;
//把项目逐条加载进来
Self.FNameEnToCnList.Add('XiaoLi=李飞刀');
Self.FNameEnToCnList.Add('XiaoWang=王中王');
Self.FNameEnToCnList.Add('XiaoZhang=张飞');
//key与value反转写入另一个TStringList
for I := to Self.FNameEnToCnList.Count - do
begin
Self.FNameCnToEnList.Add(Self.FNameEnToCnList.ValueFromIndex[I] + '=' + Self.FNameEnToCnList.Names[I]);
end;
end; destructor TBanJi222.Destroy;
begin
Self.FNameEnToCnList.Free;
Self.FNameCnToEnList.Free;
inherited Destroy;
end; procedure TBanJi222.SetNameCnToEnList(const Value: TStringList);
begin
FNameCnToEnList := Value;
end; procedure TBanJi222.SetNameEnToCnList(const Value: TStringList);
begin
FNameEnToCnList := Value;
end; procedure TForm5.Button2Click(Sender: TObject);
var
bb: TBanJi222;
I: Integer;
start_miao: Int64;
response1, response2: string;
begin
bb := TBanJi222.Create;
try
//看下结果
Memo1.Lines.Clear;
Memo1.Lines.Add(bb.NameEnToCnList.Values['XiaoLi']);
Memo1.Lines.Add(bb.NameCnToEnList.Values['李飞刀']); //循环测试性能
start_miao := GetTickCount;
for I := to do
begin
//通过英文取中文
response1 := bb.NameEnToCnList.Values['XiaoLi'];
//通过中文取英文
response2 := bb.NameCnToEnList.Values['李飞刀'];
end;
Memo1.Lines.Add('总耗时(毫秒): ' + (GetTickCount - start_miao).ToString);
finally
bb.Free;
end;
end; procedure TForm5.Button3Click(Sender: TObject);
var
bb: TBanJi333;
I: Integer;
start_miao: Int64;
response1, response2: string;
begin
bb := TBanJi333.Create;
try
//看下结果
Memo1.Lines.Clear;
Memo1.Lines.Add(bb.NameEnToCnMap['XiaoLi']);
Memo1.Lines.Add(bb.NameCnToEnMap['李飞刀']); //循环测试性能
start_miao := GetTickCount;
for I := to do
begin
//通过英文取中文
response1 := bb.NameEnToCnMap['XiaoLi'];
//通过中文取英文
response2 := bb.NameCnToEnMap['李飞刀'];
end;
Memo1.Lines.Add('总耗时(毫秒): ' + (GetTickCount - start_miao).ToString);
finally
bb.Free;
end;
end; procedure TForm5.Button4Click(Sender: TObject);
var
bb: TBanJi444;
I: Integer;
start_miao: Int64;
response1, response2: string;
begin
bb := TBanJi444.Create;
try
//看下结果
Memo1.Lines.Clear;
Memo1.Lines.Add(bb.NameEnToCnMap['XiaoLi']);
Memo1.Lines.Add(bb.NameCnToEnMap['李飞刀']); //循环测试性能
start_miao := GetTickCount;
for I := to do
begin
//通过英文取中文
response1 := bb.NameEnToCnMap['XiaoLi'];
//通过中文取英文
response2 := bb.NameCnToEnMap['李飞刀'];
end;
Memo1.Lines.Add('总耗时(毫秒): ' + (GetTickCount - start_miao).ToString);
finally
bb.Free;
end;
end; procedure TForm5.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
end; { TBanJi333 } constructor TBanJi333.Create;
var
myKey: string;
begin
inherited Create;
Self.FNameEnToCnMap := TDictionary<string, string>.Create();
Self.FNameCnToEnMap := TDictionary<string, string>.Create(); //把项目逐条加载进来
Self.FNameEnToCnMap.Add('XiaoLi', '李飞刀');
Self.FNameEnToCnMap.Add('XiaoWang', '王中王');
Self.FNameEnToCnMap.Add('XiaoZhang', '张飞'); //key与value反转写入另一个TDictionary
for myKey in Self.FNameEnToCnMap.Keys do
begin
Self.FNameCnToEnMap.Add(Self.FNameEnToCnMap[myKey], myKey);
end;
end; destructor TBanJi333.Destroy;
begin
Self.FNameEnToCnMap.Free;
Self.FNameCnToEnMap.Free;
inherited Destroy;
end; procedure TBanJi333.SetNameCnToEnMap(const Value: TDictionary<string, string>);
begin
FNameCnToEnMap := Value;
end; procedure TBanJi333.SetNameEnToCnMap(const Value: TDictionary<string, string>);
begin
FNameEnToCnMap := Value;
end; { TBanJi444 } constructor TBanJi444.Create;
var
myKey: string;
begin
inherited Create;
Self.FNameEnToCnMap := TListMap<string, string>.Create();
Self.FNameCnToEnMap := TListMap<string, string>.Create(); //把项目逐条加载进来
Self.FNameEnToCnMap.Add('XiaoLi', '李飞刀');
Self.FNameEnToCnMap.Add('XiaoWang', '王中王');
Self.FNameEnToCnMap.Add('XiaoZhang', '张飞'); //key与value反转写入另一个TDictionary
for myKey in Self.FNameEnToCnMap.Keys do
begin
Self.FNameCnToEnMap.Add(Self.FNameEnToCnMap[myKey], myKey);
end;
end; destructor TBanJi444.Destroy;
begin
Self.FNameEnToCnMap.Free;
Self.FNameCnToEnMap.Free;
inherited Destroy;
end; procedure TBanJi444.SetNameCnToEnMap(const Value: TListMap<string, string>);
begin
FNameCnToEnMap := Value;
end; procedure TBanJi444.SetNameEnToCnMap(const Value: TListMap<string, string>);
begin
FNameEnToCnMap := Value;
end; end.

TStringList 与 泛型字典TDictionary 的 哈希功能效率PK的更多相关文章

  1. 42 (OC)* 字典实现原理--哈希原理

    一.NSDictionary使用原理 1.NSDictionary(字典)是使用 hash表来实现key和value之间的映射和存储的,hash函数设计的好坏影响着数据的查找访问效率. - (void ...

  2. Combobox绑定泛型字典时提示“复杂的 DataBinding 接受 IList 或 IListSource 作为数据源”的解决方法

    一般情况下我们会将 DataTable 或 DataView 绑定到 Combobox 控件上,这时候进行数据绑定是没有问题的,因为DataTable 和 DataView 都继承了 IList 接口 ...

  3. GenericFactoryMethod泛型工厂模式实现简单IOC功能

    1.简介 泛型工厂理论上不算Gof23中设计模式之一,但是也算是一种非常好的设计模式,个人认为,废话不多说,先写个简单的抽象工厂,在写一个泛型工厂的例子来比较抽象和泛型的区别. 2.实战 还是房屋和道 ...

  4. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  5. Python 字典和集合基于哈希表实现

    哈希表作为基础数据结构我不多说,有兴趣的可以百度,或者等我出一篇博客来细谈哈希表.我这里就简单讲讲:哈希表不过就是一个定长数组,元素找位置,遇到哈希冲突则利用 hash 算法解决找另一个位置,如果数组 ...

  6. Python 字典是如何解决哈希冲突的

    本文主要翻译自 so 上面的问题 Why can a Python dict have multiple keys with the same hash? 下 Praveen Gollakota 的答 ...

  7. Redis原理再学习04:数据结构-哈希表hash表(dict字典)

    哈希函数简介 哈希函数(hash function),又叫散列函数,哈希算法.散列函数把数据"压缩"成摘要,有的也叫"指纹",它使数据量变小且数据格式大小也固定 ...

  8. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  9. c# 图解泛型List<T>, HashTable和Dictionary<TKey,TValue>

    前辈在代码中使用了HashTable,由于我用的比较少,不能理解,为什么不用Dictionary?看了源码以及查阅资料,总结如下: 首先看看它们的继承体系: 我把list<T>的继承体系也 ...

随机推荐

  1. Git-balabala

    想必大家都听说过且用过Github(没听说过-.-),我也一直用Github管理我的代码到现在,如果你只是将其作为自己私有的代码仓库,那么平时用得最多的就是git clone, git add以及gi ...

  2. (转)Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web mod

    背景:在sts中导入web项目,却不能通过sts中的tomcat来加载该服务,并报出如下错误. “Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4 ...

  3. Maven settings.xml配置(指定本地仓库、阿里云镜像设置)

    转: 详解Maven settings.xml配置(指定本地仓库.阿里云镜像设置) 更新时间:2018年12月18日 11:14:45   作者:AmaniZ   我要评论   一.settings. ...

  4. BTC钱包对接流程

    BTC钱包对接流程: 部署钱包节点 分析钱包的API 通过JSON-RPC访问钱包API 部署测试 1.部署钱包节点 虚拟币交易平台对接所有的虚拟币之前,都要在自己的服务器上部署一个钱包节点,首先要找 ...

  5. bzoj1874 [BeiJing2009 WinterCamp]取石子游戏

    1874: [BeiJing2009 WinterCamp]取石子游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 925  Solved: 381[ ...

  6. flask-migrate库的使用

    在使用flask-SQLAlchemy库的时候,经常苦恼于该库的creat_all()方法不能对项目中的新建库进行修改,drop_all()又会对数据进行删除,这非常的不方便,万幸,Flask-SQL ...

  7. jenkins权限管理,不同用户显示不同项目

    1.安装Role-based Authorization Strategy插件 系统管理-管理插件-可选插件中安装Role-based Authorization Strategy 安装后重启jenk ...

  8. CSS边框及常用样式

    一.CSS设置样式 1.1 边框border 作用:设置标签周围的边框,方法  board:宽度 样式 颜色,一般情况下样式使用 solid实体的,和dotted虚线的 <head> &l ...

  9. bzoj千题计划154:bzoj3343: 教主的魔法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3343 high记录原始身高 HIGH记录每块排序之后的身高 不满一块的直接对high操作,重排之后再 ...

  10. centos无法通过ssh连接的解决

    系统环境是centos7,虚拟机环境下的.在使用ssh工具连接虚拟机的时候发现连接不上,用的是root 先检查openssh-server是否安装: yum list installed | grep ...