## Question of the Week

Using a Dictionary (Hashtable) - Page 5
by kirupa  |  13 October 2006

In the previous page, I explained how hashtables work and the advantages they have over an array/list. In this page I will provide some real world examples that will give you a brief idea of the various uses dictionaries have.

Real World Example
A real world example that simulates the differences between arrays/lists and dictionaries/hashtables is finding information in a reference book. In the linear array/list based search, you go through each page or sections of pages in the book to find out where the information may be located. That works for small books, but for a large reference book, this approach may not work as quickly as you would like.

On the other hand, for the dictionary/hashtable approach, you simply look in the index of the book to find the page number where your topic is discussed. With the page number in hand, you immediately go to the page where the information is stored. You do not shuffle through many off-topic pages before hitting your information, and this near-instantaneous speed at returning information holds for small as well as large reference books.

A Few Practical Uses of Dictionaries
The following is a short list of scenarios where a dictionary can be used:

• Avoiding Duplicates
If you are only interested in adding values to a list that are unique, you can check whether the value you are about to add to the list exists in your dictionary.

If the value exists in the dictionary, you skip the value. If the value does not exist, you add the value both to your list as well as your dictionary for future reference. This method is certainly better than going through each element in your array and checking whether it is equal the value you are planning on adding!

• Retrieving User-Specific Information
If you have a collection of users, you can use a dictionary to have the key be your user and the value being a list containing information about the user. This allows you to, given a user's name, retrieve the information almost instantaneously without having the user wait for a long time.

• Graphs/Nodes/Edges
If you are using a graph structure to represent nodes and edges, you can use a dictionary to help you quickly find whether a pair of nodes contain an edge. For example, when adding an edge between two nodes, your key could be the string "node1-node2", and the value could be the edge itself.

When checking whether an edge exists between two nodes, you simply pass in the two nodes' names as "nodeA-nodeB" and see if a value is returned. If a value is returned, then there is an edge between those two nodes. If a value is not returned, then there is no edge between them.

• Calculating Patterns
Pattern detection is something that is quite useful. My blog post provides code and analyzes a simple example of this. Click here to view post.

The above scenarios are only a small subset of uses for a dictionary. I am sure there are countless others, but I provided them just as a way for you to think about dictionaries and what is possible with the key/value pair they take as their input. Because, if this is your first introduction to a hashtable data structure, I would be thrilled if you explored other ways of optimizing your applications using hashtables. The above examples are merely a small push in the right direction.

Conclusion
This wraps up this tutorial on how to use the Dictionary class in .NET. Besides learning about the syntax on how to use this in your applications, I also hope you learned a bit about why the Dictionary data structure is another great choice along with arrays and lists (and others!) for storing and retrieving data.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

 1 | 2 | 3 | 4 | 5