Hash table (Hashtable)

Source: Internet
Author: User
Tags definition contains hash key string sort
A hash table (Hashtable) in the. NET framework, Hashtable is a container provided by the System.Collections namespace for handling and performing key-value pairs that resemble Key/value, Where key is usually used to quickly find, while key is case-sensitive; value is used to store values corresponding to key. Key/value key value pairs in Hashtable are of type object, so hashtable can support any type of Key/value key-value pairs. Second, the simple operation of a hash table adds a Key/value key value pair: Hashtableobject.add (Key,value); Removes a Key/value key value pair in a 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 operations: using System; Using System.Collections; When using Hashtable, you must introduce this namespace class Hashtable {public static void Main () {hashtable ht=new hashtable ();///Create a Hashtable instance HT . Add ("E", "E");//Adds Key/value key value to HT. ADD ("A", "a"); Ht. ADD ("C", "C"); Ht. ADD ("B", "B"); String s= (String) ht["A"]; if (HT. Contains ("E"))//To determine whether a hash table contains a particular key, its return value is True or False Console.WriteLine ("The E key:exist"); Ht. Remove ("C");/Removes a Key/value key value pair Console.WriteLine (ht["A"]);//This outputs a HT. Clear ();/Remove all elements Console.WriteLine (ht["A"]); There will be no output}} Three, traversal hash table traversal hash table requires the use of DictionaryEntry Object, the code is as follows: for (DictionaryEntry de in HT)//ht is 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 Key/value key value of four, sorting the hash table to sort the hash table here the definition is to rearrange the keys in the Key/value key value pairs by a certain rule, but in reality this definition is not possible. Because we can't rearrange keys directly in Hashtable, if you need Hashtable to provide some sort of rule output, you can use a workaround: ArrayList akeys=new ArrayList (ht. Keys); Don't forget to import the System.Collections Akeys. Sort (); Sort for alphabetically (string Skey in Akeys) {Console.Write (Skey + ":"); Console.WriteLine (Ht[skey]);//sorted after output}

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.