|
Built-In Collection Classes: Hash Tables |
|
|
Introduction |
|
If you have been playing a little deeper with Microsoft Windows for a while, you are probably familiar with objects referred to as initialization files or ini files. These are files whose contents are made of lines that each displays a combination of a right value assigned to a left value. Here is an example: PROJECT=AIOCPE MANUFACTURER=HP Wrapper=1 DEFAULT=1 LOG_DISABLED=0 LOG_ERROR=1 LOG_WARNING=2 LOG_MAINTRACE=4 LOG_DETAILEDTRACE=8 FILESIZE=524288 A hash table is list of items that each (item) is made of a right object assigned to a left object. The object on the left side is called a key. The object on the right side is called a value. Based on this, an item of a hash table is in the form: |
Key=Value Most hash tables are made of string keys and string values but the key or the value can also be a number or a more complex item. There is no strict rule as to what you use a hash table for. When you create one, it is up to you to decide what you want to do with it.
The .NET Framework supports hash tables at different levels but probably the most common class used is called Hashtable. The Hashtable class implements the ISerializable interface that makes it possible for the list to be serialized, the ICollection interface used to keep track of the number of items in the list, the IEnumerable interface that would allow you to use the foreach loop. Before using a hash table, you can declare a variable of type Hashtable using one of its constructors such as the default one. Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
return 0;
}
}
}
In this case, the primary list would be empty. To add an item to the table, you can call the Add() method. Its syntax is: public virtual void Add(object Key, object Value); As you can see, you must provide the key and the value as the first and second arguments to the method. Here are examples of calling the Add() method: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
return 0;
}
}
}
When calling the Add() method, you must provide a valid Key argument: it can be null. For example, the following code would produce an error: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007.Add(null, "Essien");
return 0;
}
}
}
This would produce: Unhandled Exception: System.ArgumentNullException: Key cannot be null. When adding the items to the list, each key must be unique: you cannot have two exact keys. If you try adding a key that exists already in the list, the computer would throw an ArgumentException exception. Based on this, the following code would not work because, on the third call, a "Michael" key exists already: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007.Add("Michael", "Essien");
return 0;
}
}
}
This would produce: Unhandled Exception: System.ArgumentException: Item has already been added. Key in dictionary: 'Michael' Key being added: 'Michael' Besides the Add() method, you can use the indexed property of the Hashtable class to add an item to the list. To do this, enter the Key in the square brackets of the property and assign it the desired Value. Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
return 0;
}
}
}
After adding one or more items to the list, they are stored in two collections. The keys are stored in a collection represented by a property named Keys. The values are stored in the Values property. Each of these properties is of type ICollection.
Although, or because, the key and value are distinct, to consider their combination as a single object, the .NET Framework provides the DictionaryEntry structure. To access an item, you can use the foreach loop to visit each item. To support the foreach loop, the Hashtable class implements the IEnumerable.GetEnumerator() method. In this case, the item is of type DictionaryEntry: it contains a Key and a Value in combination. The DictionaryEntry structure contains two properties named Key and Value to identify the components of a combination. Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
Console.WriteLine();
return 0;
}
}
}
This would produce: Didier Drogba Michael Ballack Claude Makelele Press any key to continue . . .
Locating an item in a hash table consists of looking for either a key, a value, or a combination of Key=Value. The Hashtable class is equipped to handle these operations with little effort on your part. If you know the key of an item but want to find a value, you can use the index property because it produces it. Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
string Value = (string)Chelsea20062007["Didier"];
Console.WriteLine("The value of the Didier key is {0}",
Value);
Console.WriteLine();
return 0;
}
}
}
This would produce: The value of the Didier key is Drogba Press any key to continue . . . To find out whether a Key=Value item exists in the list, you can call the Contains() method. Its syntax is: public virtual bool Contains(object key); To look for an item, you pass its key as argument to this method. Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
Console.WriteLine("List of items");
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
bool found = Chelsea20062007.Contains("Claude");
if (found == true)
Console.WriteLine("\nThe list contains an item " +
"whose key is Claude");
else
Console.WriteLine("\nThe list doesn't contain an " +
"item whose key is Claude");
found = Chelsea20062007.Contains("James");
if (found == true)
Console.WriteLine("\nThe list contains an item " +
"whose key is Claude");
else
Console.WriteLine("\nThe list doesn't contain an " +
"item whose key is James");
Console.WriteLine();
return 0;
}
}
}
This would produce: List of items Didier Drogba Michael Ballack Claude Makelele The list contains an item whose key is Claude The list doesn't contain an item whose key is James Press any key to continue . . . To find out whether a particular key exists in the list, you can call the ContainsKey() method whose syntax is: public virtual bool ContainsKey(object key); To find out whether a particular key exists in the list, you can call the ContainsKey() method whose syntax is: public virtual bool ContainsValue(object key); When calling this method, pass a Value value as argument. Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
Console.WriteLine("List of items");
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
bool found = Chelsea20062007.ContainsValue("Makelele");
if (found == true)
Console.WriteLine("\nMakelele plays for Chelsea");
else
Console.WriteLine("\nMakelele doesn't plays for Chelsea");
found = Chelsea20062007.ContainsValue("Fortune");
if (found == true)
Console.WriteLine("\nRonaldinho plays for Chelsea");
else
Console.WriteLine("\nRonaldinho doesn't plays for Chelsea");
Console.WriteLine();
return 0;
}
}
}
This would produce: List of items Didier Drogba Michael Ballack Claude Makelele Makele plays for Chelsea Ronaldinho doesn't plays for Chelsea Press any key to continue . . .
To delete one item from the list, you can call the Remove() method. Its syntax is: public virtual void Remove(object key); Here is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
Console.WriteLine("List of items");
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
Chelsea20062007.Remove("Didier");
Console.WriteLine("\nThe item that contains " +
"Didier has been removed\n");
Console.WriteLine("List of items");
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
Console.WriteLine();
return 0;
}
}
}
This would produce: List of items Didier Drogba Michael Ballack Claude Makelele The item that contains Didier has been removed List of items Michael Ballack Claude Makelele Press any key to continue . . . To delete all items from the list, you can call the Clear() method. Its syntax is: public virtual void Clear(); This is an example: using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Hashtable Chelsea20062007 = new Hashtable();
Chelsea20062007.Add("Michael", "Ballack");
Chelsea20062007.Add("Didier", "Drogba");
Chelsea20062007["Claude"] = "Makelele";
Console.WriteLine("List of items");
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
Chelsea20062007.Clear();
Console.WriteLine("\nThe list is now empty!");
Console.WriteLine("List of items");
foreach (DictionaryEntry entry in Chelsea20062007)
Console.WriteLine("{0} {1}", entry.Key, entry.Value);
Console.WriteLine();
return 0;
}
}
}
This would produce: List of items Didier Drogba Michael Ballack Claude Makelele The list is now empty! List of items Press any key to continue . . . |
|
|
||
| Home | Copyright © 2006 Yevol | |
|
|
||