Hash Table Application

Source: Internet
Author: User

applying a hash table in C # (Hashtable)2006-05-24 15:16 491 people read Comments (0) favorite reports C#stringobjectsystemclass.net a hash Table (Hashtable) Summary

In the. NET framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like Key/value key-value pairs, where key is typically used for quick lookups, while key is case-sensitive ; value is used to store the value corresponding to key. Key/value Key-value pairs in Hashtable are all object types, so hashtable can support any type of Key/value key-value pair.

Second, the simple operation of the hash table

Add a Key/value key value pair to the hash table: Hashtableobject.add (Key,value);
Remove a Key/value key value pair in the hash table: Hashtableobject.remove (key);
Remove all elements from the hash table: Hashtableobject.clear ();
Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);
The following console program will contain all of the above actions:
Using System;
Using System.Collections; This namespace must be introduced when using Hashtable
Class Hashtable
{ 
  public static void main ()  
  { 
  hashtable ht= New hashtable ();  //creates a Hashtable instance  
  ht. Add ("E", "E");//Adds Key/value key value pair  
  ht. ADD ("A", "a");  
  ht. ADD ("C", "C");  
  ht. ADD ("B", "B");  
  string s= (String) ht["A"]; 
  if (ht. Contains ("E"))  //determines whether the hash table contains a specific key, whose return value is true or false 
    console.writeline ("the  e key:exist ");  
  ht. Remove ("C");//Remove a Key/value key value pair  
  console.writeline (ht["A"]);//output a  here;
  ht . Clear ();//Remove all elements  
  console.writeline (ht["A"]);  //there will be no output  
    } 
  } 


Third, traverse the hash table

Traversing a hash table requires the use of DictionaryEntry Object, the code is as follows:
foreach (DictionaryEntry de in HT)//HT as a Hashtable instance
{
Console.WriteLine (DE. Key);//de. Key corresponds to the Key/value key value pair key
Console.WriteLine (De.value);//de. Key corresponds to the value of the Key/value key


Four, sort the hash table

Sorting the hash table here is defined as the key in the Key/value key-value pair is rearranged by certain rules, but in practice this definition is not possible because we cannot rearrange the key directly in Hashtable, If you need hashtable to provide output for some sort of rule, you can use a workaround:
ArrayList akeys=new ArrayList (ht. Keys); Don't forget to import the System.Collections
Akeys. Sort (); Sort by alphabetical order
foreach (string skey in Akeys)
{
Console.Write (Skey + ":");
Console.WriteLine (Ht[skey]);//post-order output
}

Hash Table Application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.