SUPPORTERS:

FlashComponents
  Flash Templates
  Flash Gallery
  Flash Slideshow
  Flash Menu
  Flash Design
  Flash Video
  User Interface

  

 

 

 

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

There are many ways of storing and retrieving data. A common approach is to use an array to store all of the elements, and you can access the elements in the array by using an index value that points to the data you are looking for. In many scenarios, especially those involving large amounts of data, storing and finding values in an array is not very efficient. Faster solutions exist.

In this tutorial, I will explain how to use a hashtable derivative called a Dictionary to efficiently store and quickly retrieve large amounts of data.

Note - Hashtable == Dictionary?

A Dictionary is closely related to a HashTable. There are many subtle differences between them, but one important difference is that a Dictionary is generally faster than a Hashtable for storing data.

The reason is that a Dictionary takes strongly-typed values as its input, so you do not suffer the performance impact of storing generic Objects and boxing/unboxing them into the proper types during use.

I have divided this tutorial into several sections. I will first describe and provide examples on how to use Dictionary objects in C#. Then, I will explain what makes hashtable-like objects such as the Dictionary more efficient than traditional array-like data structures for managing data.

Using a Dictionary
For this tutorial, I am not picky on which type of a .NET project you create. This tutorial is heavy on code and less reliant on any IDE or project-specific features, so as long as you are in a View where you can type and test C# code, you are all set!

The following example describes how to use a Dictionary object to store and display values:

//Declaring a Dictionary object
Dictionary<String, double> coffeeStat = new Dictionary<String, double>();
 
//Adding values
coffeeStat.Add("Falls Church, VA", 7.7);
coffeeStat.Add("Katy, TX", 6.8);
coffeeStat.Add("Greenwood Village, CO", 6.3);
coffeeStat.Add("Issaquah, WA", 5.4);
coffeeStat.Add("Palm Beach, FL", 4.8);
coffeeStat.Add("Littleton, CO", 4.5);
coffeeStat.Add("Destin, FL", 3.6);
coffeeStat.Add("Lincoln, CA", 3.6);
coffeeStat.Add("Sherwood, OR", 3.4);
coffeeStat.Add("Naples, FL", 3.3);
coffeeStat.Add("Williamsburg, VA", 3.3);
coffeeStat.Add("Lynnwood, WA", 3.2);
coffeeStat.Add("Spring, TX", 3.0);
coffeeStat.Add("Bel Air, MD", 3.0);
coffeeStat.Add("Alpharetta, GA", 2.9);
coffeeStat.Add("Fairfax, VA", 2.8);
coffeeStat.Add("Vienna, VA", 2.8);
coffeeStat.Add("Freehold, NJ", 2.7);
coffeeStat.Add("Duluth, GA", 2.7);
coffeeStat.Add("Grand Haven, MI", 2.7);
coffeeStat.Add("Brentwood, CA", 2.6);
coffeeStat.Add("Lake Oswego, OR", 2.6);
coffeeStat.Add("Silverdale, WA", 2.5);
coffeeStat.Add("Auburn, CA", 2.4);
coffeeStat.Add("Tumwater, WA", 2.4);
 
//Displaying a Result
Console.WriteLine("Density of Starbucks per 10,000 people in Palm Beach is: " + coffeeStat["Palm Beach, FL"]);

For those curious and/or fans of coffee, the above data shows the cities with the most number of Starbucks coffee shops per 10,000 people. The original data can be found here: http://www.epodunk.com/top10/coffeee/index.html

Onwards to the next page!

1 | 2 | 3 | 4 | 5

SHARE:

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Everythingfla - HD video school membership starts as low as 4.99 a month for the first 3 months. New Videos  updated weekly. current focus is Learning how to develop in AS3.
Learn how to advertise on kirupa.com

cdn
content delivery network (cdn)