uses System.Generics.Collections;
var
Dic: TDictionary<Integer, string>;
CharCounts: TDictionary<Char, Integer>;
Value: string;
C: Char;
Count: Integer;
begin
Dic := TDictionary<Integer, string>.Create;
Dic.Add(1, 'A');
Dic.Add(2, 'B');
if not Dic.TryGetValue(3, Value) then
Value := '-';
//value is '-'
WriteLn('value is ', Value);
//counting with a default of zero:
//on a miss TryGetValue returns 0
CharCounts := TDictionary<Char, Integer>.Create;
for C in 'ABCBA' do
begin
CharCounts.TryGetValue(C, Count);
CharCounts.AddOrSetValue(C, Count + 1);
end;
//charCounts is [A: 2, B: 2, C: 1]
WriteLn('countA is ', CharCounts['A']);
WriteLn('countC is ', CharCounts['C']);
end.