Brief introduction and application of. Net hashtable

Source: Internet
Author: User

Hashtable
1. Brief description of the hash table (hashtable)

In. in net work, hashtable is system. A container provided by the collections namespace is used to process and present key-value pairs similar to key/. The key is usually used for quick search, and the key is case sensitive; store the value corresponding to the key. In hashtable, key/key-value pairs are of the object type, so hashtable can support any type of key/key-value pairs.

Ii. Simple operations on Hash Tables

Add a key/key-value pair to the hash table: hashtableobject. Add (Key ,);
Remove a key/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 console contains all the preceding operations:
Using system;
Using system. collections; // This namespace must be introduced when hashtable is used.

Class hashtable {public static void main () {hashtable ht = new hashtable (); // create a hashtable instance ht. add ("e", "E"); // Add key/key value pair ht. add ("A", "A"); ht. add ("C", "C"); ht. add ("B", "B"); string S = (string) HT ["A"]; // obtain the value if (HT. contains ("e") // determines whether the hash table contains a specific key. The return value is true or false console. writeline ("the e key: exist"); ht. remove ("C"); // remove a key/key-value pair from the console. writeline (HT ["A"]); // output a HT here. clear (); // remove all elements from the console. writeline (HT ["A"]); // there will be no output here }}

3. traverse the hash table

Dictionaryentry object is required to traverse a hash table. The Code is as follows:

For (dictionaryentry de in HT) // HT is a hashtable instance {console. writeline (de. key); // de. the key corresponds to the key/key-Value Pair key console. writeline (De .); // de. key corresponds to key/key-Value Pair}

4. Sort hash tables

The definition of sorting hash tables here is to re-arrange the keys in key/key-value pairs 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); // do not forget to import system. collections // This improves the query speed. sort (); // sort in alphabetical order for (string skey in akeys) {console. write (skey ":"); console. writeline (HT [skey]); // output after sorting}

5. access other objects in the hash table
// Class Object

Public class person {private string firstname; private string lastname; Public Person (string first, string last) {firstname = first; lastname = last ;} public String fullname {get {return firstname + "" + lastname ;}}// the access process protected void page_load (Object sender, eventargs E) {person Scott = new person ("Scott", "hanselman"); person bill = new person ("bill", "evjen "); person srini = new person ("Srinivasa", "sivakumar"); hashtable peoplehashtable = new hashtable (); peoplehashtable. add ("sh", Scott); peoplehashtable. add ("be", Bill); peoplehashtable. add ("SS", srini); person found = (person) lelehashtable ["sh"]; response. write (found. fullname + "<br/>"); found = (person) lelehashtable ["be"]; response. write (found. fullname + "<br/>"); found = (person) lelehashtable ["sh"]; response. write (found. fullname + "<br/>"); // write Method 1: // foreach (dictionaryentry de in lelehashtable) // {// response. write (de. key. tostring () + ":" + (person) de. value ). fullname + // "<br/>"); //} // write Method 2: // foreach (string s in peoplehashtable. keys) // {// response. write (S + "<br/>"); // foreach (person P in peoplehashtable. values) // {// response. write (P. fullname + "<br/> ");//}}

6. How to read the key based on value in hashtable

Hashtable t = new Hashtable();foreach( DictionaryEntry i in t ){if(  i.Value == "value1" ) return i.Key ;} 

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.