Introduction and usage of hashtable in C #

Source: Internet
Author: User

I. Introduction

Terms: A hash table is a data structure that is directly accessed based on the key value. That is to say, It maps the key value to a location in the table to access records to speed up the search. This ing function is called a hash function, and the array storing records is called a hash function.

Advantages of hashtable tables: Hashtable is a container provided by the system. Collections namespace. The key/value in hashtable is of the object type, so hashtable can support any type of key/value Key/value pairs.

The advantage of hashtable lies in its indexing method and speed.Very fast.

Ii. Simple operations on Hash Tables

Add a keyValue key-value pair in the hash table: hashtableobject. Add (Key, value );

Remove a keyValue key-value pair in the hash table: hashtableobject. Remove (key );

Remove all elements from the hash table: hashtableobject. Clear ();

Determine whether the hash table contains the specified key: hashtableobject. Contains (key );

The following ConsoleProgramAll of the above operations will be included:

123456789101112131415161718192021222324252627282930313233343536373839
 Using   System  ;  Using   System. Collections  ;  // This namespace must be introduced when file uses hashtable.  Class Hashtable {  Public   Static   Void Main(  )  { Hashtable HT =  New Hashtable (  )  ;  // File to create a hashtable instance Ht. Add  ( E, E )  ;  // Add a keyValue pair Ht. Add  ( A, )  ; Ht. Add  ( C, C )  ; Ht. Add  ( B, B )  ;  String S = (  String  ) HT [ A ]  ;  If  ( Ht. Contains  ( E )  )  // File: determines whether a hash table contains a specific key. The return value is true or false. Console. Writeline ( The E keyexist )  ; Ht. Remove  ( C )  ;  // Remove a keyValue pair Console. Writeline  ( HT [ A ]  ) ;  // Output Ht. Clear  (  )  ;  // Remove all elements Console. Writeline  ( HT [ A ]  )  ;   // File. No output is provided here.  }  } 

3. traverse the hash table

Dictionaryentry object is required to traverse a hash table,CodeAs follows:

 For  ( Dictionaryentry de In HT )  // Fileht is a hashtable instance.  { Console. Writeline  ( De. Key  )  ;  // De. Key corresponds to the key-value pair of keyValue Console. Writeline  ( De. Value  )  ;  // De. Key corresponds to the keyValue key-Value Pair Value  } 

4. Sorting hash tables

The definition of sorting hash tables here is to re-arrange the keys in the key-Value Pair according to certain rules, but in fact this definition cannot be implemented, because we cannot re-arrange keys directly in hashtable, if hashtable needs to provide output of certain rules, we can adopt a work und:

Arraylist akeys =  New Arraylist ( Ht. Keys  )  ;  // File do not forget to import system. Collections Akeys. Sort  (  )  ;  // Sort files alphabetically.  For  (  String Skey In Akeys )  { Console. Write  ( Skey +   )  ; Console. Writeline  ( HT[ Skey ]  )  ;  // Output after sorting  } 

ArticleFrom:Xiaofeng caotangHttp://www.cnitman.com/csharp-hashtable-usage-introduce/

Related Article

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.