1. hashtable Overview
Elements in the hashtable set exist as key/value. Key is used for quick search; value is used to store the value corresponding to the key. It is worth noting that both key and value are of the object type.
Ii. hashtable operations
Hashtable operations include:
1) Add a set Element
2) retrieve collection Elements
3) Traversal
4) sorting set Elements
5) modify the collection Element
6) Delete collection Elements
The following simple ConsoleProgramPerform the preceding operations (C #)
Namespace Hashtableop
{
Using System;
Using System. collections;
Class Sample
{
Static Void Main ( String [] ARGs)
{
// Create a hashtable set
Hashtable HT = New Hashtable ();
// Add a key/value pair to the Collection
Ht. Add ( " 1 " , " Apple " );
Ht. Add ( " 2 " , " Orange " );
Ht. Add ( " 3 " , " Strawberry " );
Ht. Add ( " 0 " , " Fruit " );
// Locate value using key
Console. writeline ( " Locate value using key " );
String V = ( String ) HT [ " 1 " ];
Console. writeline (v );
// Traverse a set
Console. writeline ( " \ N traversal set " );
Foreach (Dictionaryentry de In HT)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , De. Key. tostring (), De. value. tostring ());
// Sort the set
Console. writeline ( " \ N sorts the set " );
Arraylist akeys = New Arraylist (HT. Keys );
Akeys. Sort ();
Foreach ( String Key In Akeys)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , Key, HT [Key]. tostring ());
// Modifies the value of a specified key.
HT [ " 0 " ] = " Animal " ;
HT [ " 1 " ] = " Monkey " ;
HT [ " 2 " ] = " Horse " ;
HT [ " 3 " ] = " Donkey " ;
Console. writeline ( " \ N traverse the modified set " );
Foreach (Dictionaryentry de In HT)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , De. Key. tostring (), De. value. tostring ());
// Delete A set Element
Ht. Remove ( " 2 " );
Ht. Remove ( " 1 " );
Console. writeline ( " \ N traverses the set after the deleted Element " );
Foreach (Dictionaryentry de In HT)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , De. Key. tostring (), De. value. tostring ());
//Delete collection Elements
Ht. Clear ();
}
}
}