Creating HashTables


A HashTable is an enhanced dynamic list in which you can assign to each item in the list a unique key. The key lets you retrieve or remove the items from the list. In other lists, such as ArrayLists, items can only be located using a numeric index. The problem with an index is that it represents a position within the list, and the position in the list can change at any given time as a result of adding or removing items from the list. With a HashTable, the keys remain fixed even when items are added or removed.

To create a HashTable:

  1. Type System.Collections.Hashtable .

  2. Type the name of the HashTable variable, for example: list .

  3. Type = .

  4. Type new System.Collections.Hashtable(); .

  5. Type list.Add .

  6. Type an open parenthesis ( .

  7. Type the key for the item, for example: "Jose Mojica" .

  8. Type the data for the item, for example: "1111 Wellknown St."

  9. Type a close parenthesis ) .

  10. Type a semicolon ; ( Figure 9.58 ).

    Figure 9.58 The first parameter in the Add function is the key that you wish to assign to each element; the second parameter is the value. You can use any type of object for the key or for the value. For the key, however, if you use a custom class you need to make sure the class overrides the Equals function so that the HashTable can find it when requested .
     void Task() {    System.Collections.Hashtable ht = new    System.Collections.Hashtable();    ht.  Add  ("111-11-1111","Bill Cosby");    ht.  Add  ("222-22-2222","Robin Williams");    ht.  Add  ("333-33-3333","Chevy Chase"); } 

graphics/tick.gif Tips

  • You can address each item in the HashTable using the key as a parameter to the class's indexer ( Figure 9.59 ).

    Figure 9.59 Notice that even though we haven't added an item with a key of "444-44-4444" when we set the element through the indexer, if the HashTable doesn't find a matching key, it will simply add the value as a new element. Thus, setting a key, either overwrites the old value or adds the value if not there.
     void Task() {    System.Collections.Hashtable ht = new    System.Collections.Hashtable();    ht.Add("111-11-1111","Bill Cosby");    ht.Add("222-22-2222","Robin Williams");    ht.Add("333-33-3333","Chevy Chase");  string nameBC = (string)ht["111-11-1111"];   ht["444-44-4444"] = "Eddie Murphy";  } 
  • Remove items from the HashTable using the Remove function passing the key as an item ( Figure 9.60 ).

    Figure 9.60 HashTables are like ArrayLists in the sense that it they can grow and shrink as needed. However, HashTables have the advantage that you can remove an item based on a key. With ArrayLists you have to specify either an index (a position that may change) or the actual value, which means that the ArrayList class has to seek through all of its elements looking for a match.
     void Task() {    System.Collections.Hashtable ht = new    System.Collections.Hashtable();    ht.Add("111-11-1111","Bill Cosby");    ht.Add("222-22-2222","Robin Williams");    ht.Add("333-33-3333","Chevy Chase");  ht.Remove("111-11-1111");  } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net