Operation hash table

Source: Internet
Author: User

Items in the hash table are stored in the system. Collections. dictionaryentry object. You can use foreach to traverse the values in the hash table, as shown below:

Hashtable myhashtable =   New Hashtable;

Foreach (Dictionaryentry entry In Myhashtable)

{

StringStrkey=Entry. Key;

StringStrvalue=Entry. value;

}

You can also use the Count attribute to retrieve the number of items in hashtable.

1. Brief description of the hash table (hashtable)

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

Ii. Simple operations on Hash Tables

In the hash table Add A key/Value Pair: hashtableobject. Add (Key, value );

In the hash table Remove A key/Value Pair: hashtableobject. Remove (Key );

Remove from a hash table Except all elements : Hashtableobject. Clear ();

Judge a hash table Whether the specified key is included : Hashtableobject. Contains (Key );

The following Console Program All of the above operations will be included:
Using System;

UsingSystem. 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/value pairs

Ht. Add ( " A " , " A " );

Ht. Add ( " C " , " C " );

Ht. Add ( " B " , " B " );

String S = ( String ) HT [ " A " ];

If (HT. Contains ( " E " )) // Checks whether a 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/value pair

Console. writeline (HT [ " A " ]); // Output

Ht. Clear (); // Remove all elements

Console. writeline (HT [ " A " ]); // There will be no output here

}

}  

3. traverse the hash table



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

 

For (Dictionaryentry de In HT) // HT is a hashtable instance.

  {< br>
console. writeline (de. key); // de. key corresponds to key/value pairs
console. writeline (de. value); // de. key corresponds to the key/value Key-Value Pair value
}

4, sort the hash table

sort the hash table. The definition here is the key in the key/value Key-value pair. rearrange by certain rules, but in fact, this definition cannot be implemented, because we cannot directly rearrange keys 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. colleys
akeys. sort (); // sort by letter
for (string skey in akeys)

{< br>
console. write (skey + ":");
console. writeline (HT [skey]); // output after sorting

}< br>

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.