详测 Generics Collections TDictionary(2): ContainsKey、ContainsValue、AddOrSetValue、TryGetValue
2009-10-11 17:13
537 查看
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses Generics.Collections; procedure TForm1.Button1Click(Sender: TObject); var Dictionary: TDictionary<string,Integer>; b: Boolean; T: Integer; begin Dictionary := TDictionary<string,Integer>.Create(); {添加} Dictionary.Add('n1', 111); Dictionary.Add('n2', 222); Dictionary.Add('n3', 333); {判断指定的 Key 是否存在} b := Dictionary.ContainsKey('n1'); ShowMessage(BoolToStr(b, True)); {True} b := Dictionary.ContainsKey('n4'); ShowMessage(BoolToStr(b, True)); {False} {判断指定的 Value 是否存在} b := Dictionary.ContainsValue(111); ShowMessage(BoolToStr(b, True)); {True} b := Dictionary.ContainsValue(999); ShowMessage(BoolToStr(b, True)); {False} {使用 AddOrSetValue 时, 如果 Key 存在则替换值; 此时如果用 Add 将发生异常} Dictionary.AddOrSetValue('n1', 123); ShowMessage(IntToStr(Dictionary['n1'])); {123} {使用 AddOrSetValue 时, 如果 Key 不存在则同 Add} Dictionary.AddOrSetValue('n4', 444); ShowMessage(IntToStr(Dictionary['n4'])); {444} {尝试取值} if Dictionary.TryGetValue('n2', T) then ShowMessage(IntToStr(T)); {222} Dictionary.Free; end; end.
相关文章推荐
- 二十六、详测 Generics Collections TDictionary(2): ContainsKey、ContainsValue、AddOrSetValue、TryGetValue
- Dictionary 快速查找---TryGetValue(string key,out string value)
- 详测 Generics Collections TDictionary(3): TPairEnumerator、TKeyEnumerator、TValueEnumerator、ExtractPair
- Dictionary.TryGetValue 方法 试用记 Mark
- 详测 Generics Collections TDictionary(4): OnKeyNotify、OnValueNotify
- 二十七、详测 Generics Collections TDictionary(3): TPairEnumerator、TKeyEnumerator、TValueEnumerator、ExtractPair
- Dictionary.TryGetValue 方法
- 二十八、详测 Generics Collections TDictionary(4): OnKeyNotify、OnValueNotify
- Map:containsKey、containsValue 获取Map集合的键值的 值
- 调用js报错:try { document.getElementById("").SetReturnValue(__flash__toXML(MY_FUNCTION(
- 详测 Generics Collections TDictionary(5): 多种 Create 手段
- 二十九、详测 Generics Collections TDictionary(5): 多种 Create 手段
- Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>
- mybatis Result Maps collection already contains value for com.ebways.dictionary.dao.impl.PtInfoDaoImpl.beanMap
- Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>
- hashtable ,hashmap的containsValue与containsKey
- 详测 Generics Collections TDictionary(1): Add、Items、Keys、Values、Remove
- Java集合框架(下)之Map的containsKey()与containsValue()方法
- 十六、详测 Generics Collections TList (7): Items、Contains
- 二十五、详测 Generics Collections TDictionary(1): Add、Items、Keys、Values、Remove