C # hashtable

Source: Internet
Author: User

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 the keyValue. Keys are usually used for quick search, but sorting is slow. Keys are case sensitive. values are used to store values corresponding to keys. In hashtable, keyValue pairs are of the object type, so hashtable can support any type of keyValue pairs.
Ii. Usage of hashtable in C #

1. Before using hashtable, you must add a reference to system. Collections.

Using system. collections;

2.Add Element

Hashtable.Add(Key, value); // key, value can be of any type

If the key is repeated, an exception occurs during running. You can handle it like this.

If (hashtable. Contains (key) = false ){

Hashtable. Add (Key, value); // if not, add

}

It can also be processed in this way, which is more efficient.

Try {

Hashtable. Add (Key, value );

}

Catch {

// Do not handle repeated exceptions

}

3.DeleteElement

Hashtable.Remove(Key );

4.DeleteAll

Hashtable.Clear();

5.Determine whether the key already exists

Hashtable.Contains(Key) // This is already used

6.TraversalKey

Foreach (Object key in hashtable. Keys){

}

7. traversal Value

Foreach (object Value in hashtable. Values){

}

8.Traverse Key-value pairs at the same time

Foreach (dictionaryentry de in HT)
{
Console. writeline (De. Key); // Obtain the key

Console. writeline (De. Value); // Obtain the value

}

9.Sort key output(The same is true for values)

Arraylist = new arraylist (hashtable. Keys);
Arraylist.Sort();

Iii. Sample Code

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

Namespace hashtablehelper // The namespace name cannot use the class name, for example, namespace hashtable
{
Class hashtablehelper
{
Private hashtable list;
Private hashtable _ list
{
Set {list = value ;}
Get {return list ;}
}
Public hashtablehelper ()
{
List = new hashtable ();
Console. writeline ("----------- hashtable -------------");
}
// Add a key-Value Pair
Public void add (Object key, object value)
{
Console. writeline ("adding key/value pairs to hashtable ");
List. Add (Key, value );
Console. writeline ("Add/T key: {0}/tvalue: {1}", key, value );
}
// Remove a key-Value Pair
Public void remove (Object key)
{
Console. writeline ("remove key/value pairs from hashtable ");
List. Remove (key );
Console. writeline ("delete/T key: {0}", key );
}
// Obtain a key-Value Pair Based on the key.
Public void getvalue (Object key)
{
Console. writeline ("get a key-Value Pair by key ");
Console. writeline (string. Format ("value/T key {0} value: {1}", key, list [Key]);
}
// Determine whether hashtable contains a specific key
Public void contains (object I)
{
Console. writeline ("determining whether hashtable contains a specific key ");
If (list. Contains (I ))
Console. writeline ("hashtable contains the key: {0}", I );
Else
Console. writeline ("hashtable does not contain keys: {0}", I );

}
// Traverse Key
Public void getkeys ()
{
Console. writeline ("traversing the hashtable key ");
Foreach (Object de in list. Keys)
{
Console. writeline ("/T key: {0}", de );
}
}
// Traverse Value
Public void getvalues ()
{
Console. writeline ("traversing hashtable values ");
Foreach (Object de in list. values)
{
Console. writeline ("/tvalue: {0}", de );
}
}
// Use foreach for traversal. Each key/value pair is of the dictionaryentry type. Note: reverse Traversal
Public void getkeyvalues ()
{
Console. writeline ("traversing key/value pairs ");
Foreach (dictionaryentry de in list) // difference between dictionary and dictionaryentry
{
Console. writeline ("/T key: {0}/tvalue: {1}", de. key, de. value); // C # strictly case sensitive, such as de. key, de. the value is incorrect.
}
}
// Sort the hashtable key
Public void sort ()
{
Console. writeline ("sorting hashtable keys ");
Arraylist hlist = new arraylist (list. Keys );
Hlist. Sort ();
For (INT I = 0; I {
Console. writeline ("/T key: {0}", hlist [I]);
}
}
// Clear hashtable
Public void clear ()
{
Console. writeline ("Clear hashtable ");
List. Clear ();
Console. writeline ("hashtable cleared! ");
}
// Obtain hashtable Information
Public void getinfo ()
{
Console. writeline ("Get hashtable information ");
Console. writeline (string. Format ("Information/total number of telements: {0}", list. Count ));
}
}
}

 

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

Namespace hashtablehelper
{
Class Program
{
Static void main (string [] ARGs)
{
Hashtablehelper htb = new hashtablehelper ();
Htb. Add (0, "Tang yufang ");
Htb. Add (1, "Tang yufang ");
Htb. Add (2, "Tang yufang ");
Htb. Add (3, "Tang yufang ");
Htb. Add (4, "Tang yufang ");
Htb. Add (5, "Tang yufang ");
Htb. Add (6, "Tang yufang ");
Htb. Add (7, "Tang yufang ");
Htb. Add (8, "Tang yufang ");
Htb. Add (9, "Tang yufang ");
Htb. Add (10, "Tang yufang ");
Htb. Add (11, "Tang yufang ");
// Htb. Add (0, "Tang yufang ");
Htb. getvalue (1 );
Htb. getvalue (0 );
Htb. getkeys ();
Htb. getvalues ();
Console. writeline ("start to sort hashtable keys ");
Htb. Sort ();
Console. writeline ("sorting ends with the hashtable key ");
Htb. getvalues ();
Htb. getkeyvalues ();
Htb. Contains (111 );
Htb. getinfo ();
Htb. Remove (0 );
Htb. Remove (0 );
Htb. getvalues ();
Htb. getinfo ();
Htb. Clear ();
Htb. getinfo ();
}
}
}

Note: Sorting hash tables is defined here to re-arrange keys in 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 use the above work ing method.

 

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.