Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

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.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 

1 | 2 | 3 | 4 | 5


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com