Delphi XE System.Generics.Collections[2].TObjectDictionary - 键和/或值对象的集合

Delphi XE System.Generics.Collections[2].TObjectDictionary - 键和/或值对象的集合

TDictionaryOwnerships = set of (doOwnsKeys, doOwnsValues);

  TObjectDictionary<K,V> = class(TDictionary<K,V>)
  private
    FOwnerships: TDictionaryOwnerships;
  protected
    procedure KeyNotify(const Key: K; Action: TCollectionNotification); override;
    procedure ValueNotify(const Value: V; Action: TCollectionNotification); override;
  public
    constructor Create(Ownerships: TDictionaryOwnerships; ACapacity: Integer = 0); overload;
    constructor Create(Ownerships: TDictionaryOwnerships;
      const AComparer: IEqualityComparer<K>); overload;
    constructor Create(Ownerships: TDictionaryOwnerships; ACapacity: Integer;
      const AComparer: IEqualityComparer<K>); overload;
  end;

描述:

  • TObjectDictionary表示键和/或值对象的通用集合。它可以将关键点处理为对象,将值处理为对象或将两者都处理为对象。
  • TObjectDictionary能够自动处理对象的生存期。实例化TObjectDictionary时,Ownerships参数指定字典是否拥有键、值或两者。在Create方法中,动态检查类型参数。如果未指定所有权(空集),则TObjectDictionary的功能与TDictionary的相同。

以下读写TDictionary或TObjectDictionary的方法,包括写入时重复键或读取时找不到键的影响。

Write 写入

MethodIndex typeValue typeIf duplicate key方法功能描述
Add TKey TValue Exception  
AddOrSetValue TKey TValue Overwrite 添加键值对,即使键值已经存在。
Items TKey TValue Overwrite  

Read 读取

MethodIndex/input typeResult typeIf key not foundNotes
ContainsKey TKey Boolean false

测试字典中的键。

如果给定的键在字典中,则ContainsKey返回true,否则返回false。这是O(1)操作。

ContainsValue TValue Boolean n/a

检查字典中的值。如果给定值在字典中,则ContainsValue返回true,否则返回false。

这是一个O(n)运算,其中n是字典中的条目数。

ExtractPair TKey TPair Default pair

Returns TPair, removes item from dictionary

返回TPair,从字典中删除项

Items TKey TValue Exception

Use TryGetValue to avoid exception

使用TryGetValue避免异常

operator [] TKey TValue Exc C++ only
Keys n/a TKeyCollection n/a  
ToArray n/a TArray<TPair<TKey,TValue>> n/a  
TryGetValue TKey TValue, Boolean default,false Like Items[] without exception
Values n/a TValueCollection n/a

 

 

 

 

创建时间:2022.11.03  更新时间:

posted on 2022-11-03 10:44  滔Roy  阅读(119)  评论(0编辑  收藏  举报

导航