1. Technology

Using And Understanding The TDictionary (Hash Table) Class In Delphi

By July 13, 2012

Follow me on:

in Delphi Generics :: Introduced in Delphi 2009, the TDictionary class, defined in Generics.Collections unit, represents a generic hash table type collection of key-value pairs.

In Delphi, the TDictionary is defined as a hash table. Hash tables represents a collection of key-and-value pairs that are organized based on the hash code of the key. Hash tables are optimized for lookups (speed). When a key-value pair is added to a hash table, the hash of the key is computed and stored along with the added pair.

Read the full article to learn about Using TDictionary - Hash Tables in Delphi


July 13, 2012 at 6:21 am
(1) Noisrev says:

“Of course, you can have complex dictionaries where both the key and the value would be “complex” types like records”
this is definitly FALSE, it won’t work (or at least not out of box).

For records it will work correctly *ONLY* if record is not dynamic (ie. doesn’t have any dynamic type, like strings, dynamic arrays, widestrings, interfaces, etc). Of course it might not work correctly for pointer types too, but it’s obvious.

It’s because hash will be calculated for record “content”. In your case (assuming compiled for 32bit) it will be 8 bytes, because each string is a 4 byte pointer (GetHashCode_Binary -> Inst^.Size=8) .

Dynamic record as a key would work only if you provide custom IEqualityComparer (which handles correctly Equals/GetHashCode methods)

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.