{"id":304,"date":"2008-10-02T14:06:42","date_gmt":"2008-10-02T04:06:42","guid":{"rendered":"http:\/\/www.malcolmgroves.com\/blog\/?p=304"},"modified":"2015-03-13T10:39:41","modified_gmt":"2015-03-12T23:39:41","slug":"a-tdictionary-explanation-2","status":"publish","type":"post","link":"http:\/\/www.malcolmgroves.com\/blog\/?p=304","title":{"rendered":"A TDictionary explanation"},"content":{"rendered":"<p>On the recent Delphi 2009 roadshow in Australia, I had a few people ask me about the new TDictionary&lt;TKey, TValue&gt; container in Generics.Collections. If you haven&#8217;t played with it yet,\u00a0 <a href=\"http:\/\/beensoft.blogspot.com\/2008\/09\/simple-generic-dictionary-tdictionary.html\" target=\"_blank\">Roland Beenhakker<\/a> has a nice write up on using it, but most of the questions I was getting were not about how to use it, but about what it&#8217;s for.<\/p>\n<p>The way I try and explain it is by stepping back and looking at collections that people already are used to. Leaving implementation details aside, a lot of the Delphi collections store values and let you retrieve those values using a key.<\/p>\n<p>For example, a TStringList lets you store strings indexed by an integer value. So in this case, the value would be a string and the key would be an integer. A TList lets you store pointers and retrieve them using an integer. Again, the key is an Integer, the value is a pointer.<\/p>\n<p>The next step is to think about TList&lt;T&gt;. It&#8217;s not that different, the key is still an integer, just the value can be parameterised using generics.<\/p>\n<p>If you&#8217;re ok with that, then a TDictionary&lt;TKey, TValue&gt; is pretty easy to understand. It&#8217;s a generic container that lets you parameterise not only the value, but the key also. So, to get somewhat similar behaviour to a TStringList, you could use a TDictionary&lt;integer, string&gt;. Now, of course the implementation details of how TDictionary would store those strings is wildly different to a TStringList, but in terms of understanding it, I think it helps.<\/p>\n<p>Equally, a TList&lt;T&gt; might be replaced in broad terms with a TDictionary&lt;Integer, T&gt;, with T being whatever type you like.<\/p>\n<p>The really cool thing though, is that the key part does not have to be an Integer. You could instead of TDictionary&lt;Integer, String&gt; do something like TDictionary&lt;string, string&gt;, so a string could be retrieved by using another string as a key. If you&#8217;re trying to think of an example of that, what about the TDictionary&#8217;s real world namesake, a dictionary. The key string would be the word, the definition would be the Value.<\/p>\n<p>Further, it could be TDictionary&lt;TGuid, TState&gt;, where you might be using a GUID as a sessionID to retrieve an object that stores your session state in some sort of distributed system.<\/p>\n<p>An interesting take on this is to use a TDictionary to store code in the form of anonymous methods. Anonymous methods are defined as types, so no reason they can&#8217;t be stored in a collection just like data. I find this really interesting. If you&#8217;re a fan of using table-driven development to replace convoluted if..then&#8230;else or case statements, this might spark some ideas. In fact in <a href=\"http:\/\/www.malcolmgroves.com\/blog\/?p=331\">my next blog post<\/a> I&#8217;ll show an example of a generic Factory implementation that uses a TDictionary internally to store anonymous factory methods.<\/p>\n<p>Anyway, there is a lot more to know about TDictionary&#8217;s, such as hashing functions, comparers, etc, but hopefully this has at least given you a high-level understanding of where you might look at using them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On the recent Delphi 2009 roadshow in Australia, I had a few people ask me about the new TDictionary&lt;TKey, TValue&gt; container in Generics.Collections. If you haven&#8217;t played with it yet,\u00a0 Roland Beenhakker has a nice write up on using it, but most of the questions I was getting were not about how to use it, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[98],"tags":[24,19,48,20,21],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-coding","tag-anonymous-methods","tag-delphi","tag-embarcadero","tag-generics","tag-tdictionary"],"_links":{"self":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":9,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":1763,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions\/1763"}],"wp:attachment":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}