TDictionary 与 TObjectDictionary 的区别是 : TObjectDictionary 可以做到 free的时候 里面的对象 一并free,从而不会出现内存 泄露。

用途:

TDictionary 适合 内存自管理的东西 如:integer int64 word string 结构体  与 动态数组(基本类型与结构体)。如下用法:

TObjectDictionary<string, RPerson>.create();

TObjectDictionary 适合 对象 等内存 手动管理的东西,一般就是 对象。如下用法:

可以发现它有三个构造函数;ACapacity 的意思是 构造的时候 首先创建几个对象。就是说 静态的创建映射类。可以做到映射类创建的时候 事先就内置几个 对象。

Ownerships 的意思是 key或value 跟随 字典一并释放,是个集合参数,那么可以集合为空 或 key 或 key+value 或 value 4种情况。

空集合的时候 表示 key 和 value 都不跟随字典一并释放,需要手工释放。

doOwnsKeys ---- 表示key 跟随字典一并释放。

doOwnsValues --- 表示value 跟随字典一并释放。

demo如下,可以参见:

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; type
TForm5 = class(TForm)
Memo1: TMemo;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TPerson = class
public
name: string;
age: Integer;
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button2Click(Sender: TObject);
var
map: TObjectDictionary<string, TPerson>;
map2: TObjectDictionary<TObject, TPerson>;
keyStr: string;
tempButton: TObject;
begin
//这么写会报错,因为key是string类型的内存释放自管理的, 不能交由字典来管理, 值是object类型的可以
//map := TObjectDictionary<string, TPerson>.Create([doOwnsKeys,doOwnsValues]);
map := TObjectDictionary<string, TPerson>.Create([doOwnsValues]); //注意这里就得写 doOwnsKeys,doOwnsValues] 了,因为key 与 value 都是 对象, 要想map2释放的时候 key与value连同释放就得如此
map2 := TObjectDictionary<TObject, TPerson>.Create([doOwnsKeys, doOwnsValues]);
try
map.Add('', TPerson.Create);
map.Add('', TPerson.Create);
map[''].name := '小李飞刀';
map[''].name := '火云邪神'; for keyStr in map.Keys do
begin
Memo1.Lines.Add(keyStr + '------' + map[keyStr].name );
end; map2.Add(TButton.Create(nil), TPerson.Create);
map2.Add(TButton.Create(nil), TPerson.Create);
for tempButton in map2.Keys do
begin
TButton(tempButton).Top := Random();
TButton(tempButton).Left := Random();
TButton(tempButton).Caption := '测试';
TButton(tempButton).Parent := Self; end;
Application.ProcessMessages;
Sleep() ; // 注意观察 button 会 4秒后 消失。
finally
map.Free;
map2.Free;
end;
end; procedure TForm5.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
end; end.

TDictionary 与 TObjectDictionary的更多相关文章

  1. TDictionary字典 对象的释放。。。

    type TRen = record name: string; age: Integer; end; type TPeople = class private Fname: string; Fage ...

  2. 使用 Delphi Xe 的 TDictionary

    原本一直使用 TList, 将定义的一个个 Record 保存在TList 里面, 为了能把某些对象管理起来, 例如一个类的 n 多实例,可以进行索引.查找.释放等 今天刚看到原来已经有了一个叫 TD ...

  3. 第一部分实现功能:使用一个TabControl和一个Memo和TDictionary类实现文本临时存储

    效果图: 一期功能概要: a.双击tab关闭tab,双击tab右边空白添加tab(标题为以hhnnsszzz的时间格式命名) b.切换tab将数据存入dictionary,key为标题,value为m ...

  4. TDictionary 是delphi用的,c++builder用起来太吃力。

    TDictionary 是delphi用的,c++builder用起来太吃力.c++还是用std::map代替.c++d map很好用啊.https://blog.csdn.net/ddkxddkx/ ...

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

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

  6. TDictionary字典 记录 的赋值。

    type TRen = record age: Integer; //把name定义成结构的属性. private Fname: string; procedure Setname(const Val ...

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

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

  8. delphi 泛型 c++builder 泛型

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

  9. Dephi泛型

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

随机推荐

  1. Android实现动态改变屏幕方向(Landscape & Portrait)

    1.AndroidManifest.xml: <activity> android:screenOrientation="portrait" ... 2.xx.java ...

  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. laravel DB listen 回调追踪产生 sql 语句的代码

    \DB::listen(function (QueryExecuted $sql) { \Log::info($sql->sql); \Log::info((new \Exception())- ...

  4. 浏览器json数据格式化

    在浏览器上作接口测试的时候看到json 格式的数据是密密麻麻的一片,眼睛都花了..  如: 设置方法:  chrome  的右上角选择,然后---  更多工具---  扩展程序  ----   JSO ...

  5. array_merge 优化调整

    function dealed_array_merge($a,$b){ if ($a && !$b){ return $a; } if (!$a && $b){ ret ...

  6. Dockerfile编写注意事项

    转载自:https://blog.fundebug.com/2017/05/15/write-excellent-dockerfile/ 一.目标 更快的构建速度 更小的Docker镜像大小 更少的D ...

  7. GO_00:Mac之Item2的配置安装

    申明:Mac 本上的终端感觉不是那么多好用,以前在 Windows 上操作 Linux 都是通过 XShell 来操作的,界面美观大方.操作使用比较简单.故在 Mac 上也需要有这样一款类似的工具,那 ...

  8. 动态引用外部的Javascript脚本文件[转]

    你可以参考下面方法,进行动态为网页引用外部的Javascript脚本文件.代码写在Page_Init方法内. VB.NET: 下图是运行时,查看HTML的源代码: C#:

  9. Spring 3.2 @Autowired异常:expected single matching bean but found 2

    在使用Sping做单元测试时候,对RequestMappingHandlerAdapter(从处理器包装过来的适配器)进行自动装配, 发现报:expected single matching bean ...

  10. Linux基础-简单的进程操作

    任务:查找一个名为firewall的进程,并且将其强制杀死 首先要使用ps -aux来查询firewall的进程ID(|grep firewall) 这样我们就得到了firewall的进程ID是653 ...