Use of a hash table (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 Program All of the above operations will be included:

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

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

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

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

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

If (HT. Contains ("e") // checks 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/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 object is required to traverse a hash table,CodeAs follows:

For (dictionaryentry de in HT) // HT is a hashtable instance.

{

Console. writeline (De. Key); // de. Key corresponds to key/key-Value Pair

Console. writeline (De.); // de. Key corresponds to key/key-Value Pair

}

4, sort the hash table


sort the hash table. The definition here is sort keys in key/key-value pairs in a specified rule and, 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. collections

akeys. sort (); // sort by letter

for (string skey in akeys)

{< Br _ extended = "true">
console. write (skey ":");

console. writeline (HT [skey]); // output after sorting

}< Br _ extended = "true">

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.