Use of a hash table (hashtable) with Instances

Source: Internet
Author: User

Hashtable is usually called a hash table, which represents a set of key/value pairs.

1. hashtable constructor:

Hashtable () // empty instance initialized to 0 Elements
Hashtable (int32) // a new instance initialized to int32 Elements

Example:
Hashtable myhashtable1 = new hashtable ();
Hashtable myhashtable2 = new hashtable (5 );

2. Features of The hashtable Element

The hashtable method to obtain elements is to use"Key"Value corresponding to the access key, that is, hashtable [Key]. The following is an example:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. collections;

Namespace example9_22
{
Class Program
{
Static void main (string [] ARGs)
{
Hashtable myhashtable = new hashtable ();
// Insert the corresponding key and Value Elements
Myhashtable. Add (1, "H ");
Myhashtable. Add (2, "e ");
Myhashtable. Add (3, "L ");
Myhashtable. Add (4, "L ");
Myhashtable. Add (5, "O ");
Myhashtable. Add ("int", 1 );
Myhashtable. Add ("double", 2.3 );
Myhashtable. Add ("bool", true );
// Modify the elements composed of keys and values
Myhashtable ["int"] = 23;
Myhashtable ["double"] = 1.5;
Myhashtable ["bool"] = false;
// Outputs the elements composed of the corresponding keys and values.
Console. writeline (myhashtable [1]);
Console. writeline (myhashtable [2]);
Console. writeline (myhashtable [3]);
Console. writeline (myhashtable [4]);
Console. writeline (myhashtable [5]);
Console. writeline (myhashtable ["int"]);
Console. writeline (myhashtable ["double"]);
Console. writeline (myhashtable ["bool"]);
Console. Readline ();
}
}
}
Program The running result is as follows:
H
E
L
L
O
23
1.5
False

3. operations on the hashtable Element

● The add operation is defined as follows:
Public Virtual void add (Object key, lbject value)

Note: The "key" cannot be blank, and the "value can be blank ".

● Delete operation:
Hashtable. Clear () // delete all elements without any parameters. After clearing, the number of hashtable elements is 0.
Example: myhashtable. Clear ()
Hashtable. Remove () // Delete the element where the specified "key" is located.
It is defined as follows:
Public Virtual void remove (Object key)
Example: myhashtable. Remove ("int ");

If hashtable is read-only, the above operation will be abnormal..

4. hashtable Traversal

Because the hashtable key-Value Pair belongs to the dictionaryentry type, when we use foreach to traverse hashtable, the type parameter is: dictionaryentry. See the following example:
Foreach (dictionatyentry, myde in myhashtable)
{
Console. writeline ("\ t {0} \ t {1 }",Myde. Key, myde. Value);//Note the key-value reading method.
}

5. query hashtable

You can use the following methods to query hashtable:
● Hashtable. Contains // determines whether hashtable contains the specified "key ".
● Hashtable. containskey // checks whether hashtable contains the specified "key ". Same as above
● Hashtable. containsvalue // determines whether or not hashtable contains the specified value. Good!

Example:
Console. writeline ("myhashtable contains key 1: {0}", myhashtable. Contains (1 ));
Console. writeline ("myhashtable contains key 6: {0}", myhashtable. containskey (6 ));
Console. writeline ("myhashtable include value true: {0}", myhashtable. containsvalue (true ));
Console. writeline ("myhashtable contains value 3.3: {0}", myhashtable. containsvalue (2.2 ));

6. Advantages of hashtable tables

hashtable is system. A container provided by the collections namespace. The key/value in hashtable is of the object type, hashtable supports key/value pairs of any type .
the advantage of hashtable lies in its indexing method. the indexing speed is very fast .

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.