type
TCity = class
Country: String;
Latitude: Double;
Longitude: Double;
end; const
EPSILON = 0.0000001; var
Dictionary: TDictionary<String, TCity>;
City, Value: TCity;
Key: String; begin
{ Create the dictionary. }
Dictionary := TDictionary<String, TCity>.Create;
City := TCity.Create;
{ Add some key-value pairs to the dictionary. }
City.Country := 'Romania';
City.Latitude := 47.16;
City.Longitude := 27.58;
Dictionary.Add('Iasi', City); City := TCity.Create;
City.Country := 'United Kingdom';
City.Latitude := 51.5;
City.Longitude := -0.17;
Dictionary.Add('London', City); City := TCity.Create;
City.Country := 'Argentina';
{ Notice the wrong coordinates }
City.Latitude := 0;
City.Longitude := 0;
Dictionary.Add('Buenos Aires', City); { Display the current number of key-value entries. }
writeln('Number of pairs in the dictionary: ' +
IntToStr(Dictionary.Count)); // Try looking up "Iasi".
if (Dictionary.TryGetValue('Iasi', City) = True) then
begin
writeln(
'Iasi is located in ' + City.Country +
' with latitude = ' + FloatToStrF(City.Latitude, ffFixed, 4, 2) +
' and longitude = ' + FloatToStrF(City.Longitude, ffFixed, 4, 2)
);
end
else
writeln('Could not find Iasi in the dictionary'); { Remove the "Iasi" key from dictionary. }
Dictionary.Remove('Iasi'); { Make sure the dictionary's capacity is set to the number of entries. }
Dictionary.TrimExcess; { Test if "Iasi" is a key in the dictionary. }
if Dictionary.ContainsKey('Iasi') then
writeln('The key "Iasi" is in the dictionary.')
else
writeln('The key "Iasi" is not in the dictionary.'); { Test how (United Kingdom, 51.5, -0.17) is a value in the dictionary but
ContainsValue returns False if passed a different instance of TCity with the
same data, as different instances have different references. }
if Dictionary.ContainsKey('London') then
begin
Dictionary.TryGetValue('London', City);
if (City.Country = 'United Kingdom') and (CompareValue(City.Latitude, 51.5, EPSILON) = EqualsValue) and (CompareValue(City.Longitude, -0.17, EPSILON) = EqualsValue) then
writeln('The value (United Kingdom, 51.5, -0.17) is in the dictionary.')
else
writeln('Error: The value (United Kingdom, 51.5, -0.17) is not in the dictionary.');
City := TCity.Create;
City.Country := 'United Kingdom';
City.Latitude := 51.5;
City.Longitude := -0.17;
if Dictionary.ContainsValue(City) then
writeln('Error: A new instance of TCity with values (United Kingdom, 51.5, -0.17) matches an existing instance in the dictionary.')
else
writeln('A new instance of TCity with values (United Kingdom, 51.5, -0.17) does not match any existing instance in the dictionary.');
City.Free;
end
else
writeln('Error: The key "London" is not in the dictionary.'); { Update the coordinates to the correct ones. }
City := TCity.Create;
City.Country := 'Argentina';
City.Latitude := -34.6;
City.Longitude := -58.45;
Dictionary.AddOrSetValue('Buenos Aires', City); { Generate the exception "Duplicates not allowed". }
try
Dictionary.Add('Buenos Aires', City);
except
on Exception do
writeln('Could not add entry. Duplicates are not allowed.');
end; { Display all countries. }
writeln('All countries:');
for Value in Dictionary.Values do
writeln(Value.Country); { Iterate through all keys in the dictionary and display their coordinates. }
writeln('All cities and their coordinates:');
for Key in Dictionary.Keys do
begin
writeln(Key + ': ' + FloatToStrF(Dictionary.Items[Key].Latitude, ffFixed, 4, 2) + ', ' +
FloatToStrF(Dictionary.Items[Key].Longitude, ffFixed, 4, 2));
end; { Clear all entries in the dictionary. }
Dictionary.Clear; { There should be no entries at this point. }
writeln('Number of key-value pairs in the dictionary after cleaning: ' + IntToStr(Dictionary.Count)); { Free the memory allocated for the dictionary. }
Dictionary.Free;
City.Free;
readln;
end.

nbhh的泛型:TDictionary的更多相关文章

  1. Delphi 2010 中的泛型

    Delphi 2010 中的泛型 2010已发布很长时间了,口碑还不错,准备用它开发下一项目,但对泛型等新东西的认识还不够,就搜了一下,发现下面这篇文章,还不错,大家一起补补课吧! C++中的模板.C ...

  2. TStringList 与 泛型字典TDictionary 的 哈希功能效率PK

    结论: 做HashMap 映射 功能的时候 ,字典TDictionary 功能更强大,且效率更高,比如不仅仅可以存String,还可以存结构和类. TDictionary类是一个name,value容 ...

  3. Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

    Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...

  4. delphi 泛型 c++builder 泛型

    delphi 泛型 System.Generics.Collections.pas TList<T> http://docwiki.embarcadero.com/Libraries/Be ...

  5. 泛型 for to/in 遍历 PK 效率;TEnumerator、TEnumerable

    再使用泛型的时候,经常需要用到遍历功能: 只要继承了 TEnumerator 或 TEnumerable 这两个抽象类的 都具有遍历功能. 当然没有继承这两个抽象类的 也具有使用 for in 来遍历 ...

  6. Dephi泛型

    TArray TEnumerator(抽象) TEnumerable(抽象) 实际使用:TList TQueue TStack TPair TDictionary ,内部都包含 TValueEnume ...

  7. Dephi泛型generic的应用

    Dephi泛型generic的应用   泛型在C++, C#中已有广泛应用,Delphi自2009版本也引入泛型,典型的应用如TList,TDictionary.如果你熟悉C#,其用法十分类似. 比如 ...

  8. Delphi 新语法:泛型

      这里的新语法一般指Delphi7不支持的语法,高版本中会经常遇到,所以花一点时间学会使用它.泛型是一种特殊的类型.你不用一开始就指明参数的具体类型,而是先定义一个类型变量,在使用的时候再确定参数的 ...

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

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

  10. .NET面试题系列[8] - 泛型

    “可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...

随机推荐

  1. CDS标准视图:应收账龄表 I_ARJrnlEntrItmAgingGrid

    视图名称:应收账龄表 I_ARJrnlEntrItmAgingGrid 视图类型:参数 视图代码: 点击查看代码 @AbapCatalog.sqlViewName: 'IARJEITMAGGRID' ...

  2. 第五章 非对称加密算法--DH--RSA

    13.1.DH 非对称算法的基石 仅能用于密钥分配,不能用于加解密数据,一般加密数据用AES 密钥长度:512~1024中的64的整数倍 双方各有自己的密钥对 13.2.RSA 最经典的非对称加密算法 ...

  3. Golang-接口7

    http://c.biancheng.net/golang/interface/ Go语言接口声明(定义) Go语言不是一种 "传统" 的面向对象编程语言:它里面没有类和继承的概念 ...

  4. 【YashanDB知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string

    本文内容来自YashanDB官网,原文内容请见 https://www.yashandb.com/newsinfo/7901522.html?templateId=1718516 现象 将数据通过SQ ...

  5. Docker与联合文件系统

    1. 联合文件系统 概念 UnionFS(联合文件系统)是一种分层,轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次次的提交来一层一层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(u ...

  6. Kotlin:【Map集合】集合创建、集合遍历、元素增加

    to本身是一个函数

  7. VLDB来啦!企业上云“搭子”天翼云TeleDB数据库有话说

    近日,VLDB 2024(International Conference on Very Large Data Bases)在广州隆重举行,全球数据库领域顶尖学者汇聚一堂,围绕数据库行业前沿议题展开 ...

  8. 异常:java.lang.reflect.InvocationTargetException

    在产生这个问题的时候,以为是代码某个类又改动了.但是对比了git,发现并没有改动,测试环境代码照跑,故又重新拉了一份重新部署,仍然报同样的错.解决过程中,试了以下网上各种方法,均无效果. 1.包重复. ...

  9. 1. Docker 的简介概述

    1. Docker 的简介概述 @ 目录 1. Docker 的简介概述 2. Docker 的理念: 3. 容器与虚拟机比较 4. Docker应用场景 5. 最后: 为什么会有 Docker 出现 ...

  10. Mac安装Flume

    一.下载Flume(在SoftWare目录下) wget http://mirrors.tuna.tsinghua.edu.cn/apache/flume/1.8.0/apache-flume-1.8 ...