How to create and use a hash table

Source: Internet
Author: User
How to create and use a hash table

This example illustrates how to create and use a hash table. A hash table is a set of key-value combinations and is organized into a bucket for quick search. You can use keys or associated values to search for a hash table. However, if quick search is your target, key search is usually faster.

 

C # hashtable. aspx

[Running example] | [View Source Code]

As mentioned in the introduction, a hash table is a set. When filling in a hash table, provide it with a key to be added to the table and a value that is accompanied by the key. In the following example, the employee ID is used for the key and then the employee name to help you understand the types of combinations that can be used. Note that keys are not numeric values: Any object is acceptable.

Why is a hash table useful? When these key-value pairs are added to the hash table, the hash table searches for the key and gives it a hashCode, This code is the number that identifies the key (each key added will get a hash code ). Put the hash code into a bucket to help organize items in the table. This will be helpful when you try to search in the table in the future.

These buckets are not used for the moment. To search for an element in an array, you must traverse each element of the array to find the element. You do not know where it is, so there is no quick way to search for items. On the contrary, a hash table can quickly search for a specific key, because once a key is provided for it, it knows which bucket to search. This means that it only needs to find a subset of all elements.

It is important to remember that this fast search mechanism is only available when you search by key. You can search by value, but it may be slower than key search. Therefore, if possible, perform a key search.

To add elements to a hash table, callAddTo add a key-value pair.


 // here we'll create a hashtable of employee numbers and employee nameshashtable table = new hashtable (); // now, we will add elements to the hashtable, as key-value pairstable. add (5123, "Jay"); table. add (1829, "Tom"); table. add (2882, "Matt"); 
 'Here we'll create a hashtable of employee numbers and employee namesdim table as hashtable = new hashtable () 'Now, We will add elements to the hashtable, as key-value pairstable. add (5123, "Jay") table. add (1829, "Tom") table. add (2882, "Matt") 
C # VB

Now that the hash table has been filled in, you can search for elements in it. The key and value are used for search. Note that in the actual example, you will accept the value or key to be searched from the user, so the implementation is slightly different, but the concept is the same.

 // searching by value. use the method containsvaluestring valuetofind = "Jay"; if (table. containsvalue (valuetofind) {console. writeline ("found {0} in the list. ", valuetofind);} // searching by key. use the method containsint keytofinds = 5123; If (table. contains (keytofind) {console. writeline ("found {0} in the list. ", keytofind) ;}
 'searching by value. use the method containsvaluedim valuetofind as string = "Jay" If table. containsvalue (valuetofind) Then console. writeline ("found {0} in the list. ", valuetofind) end if 'searching by key. use the method containsdim keytofind as integer = 5123if table. contains (keytofind) Then console. writeline ("found {0} in the list. ", keytofind) end if 
C # VB  

Of course, you can do more than search. AvailableRemoveMethod to remove items from the hash table (pass the key that identifies the elements to be removed to this method ). You can also use the "key" (KEYS) set or "value" (values) set in the hash table instance to enumerate the hash table.

 // using the Remove Method to delete a participant item... table. remove (5123); // enumerating over the collection with a foreach statement // note the alternate of iterating through by value is demonstrated // foreach (Object o in table. values) {foreach (Object o in table. keys) {console. writeline (O. tostring () ;}
 'Using the Remove Method to delete a participant item... table. remove (5123); dim o as object 'enumerating over the collection with a foreach statement 'Note the alterate of iterating through value is demonstrated' for each o in table. valuesfor each o in table. keys console. writeline (O. tostring () Next 
C # VB  

These are the basic operations for using a hash table. After the hash table is filled with items, it is important to understand how to iterate through the hash table. This important topic is described in how to iterate through the Integration Section. The code below shows you how to use the foreach (for each in VB) statement to iterate through the list and output each key-value combination to the screen. To do this, you also need to referenceKeyAndValueAttribute. The following example shows how an iteration passes through a set.

 // if you are wondering where dictionaryentry comes from, it is // The generic object type of each element in the hashtableforeach (dictionaryentry D in table) {// use the key and value properties console. writeline ("{0} \ t {1}", D. key, D. value) ;}
 'if you are wondering where dictionaryentry comes from, it is 'the generic object type of each element in the hashtabledim D as dictionaryentryfor each d in table' use the key and value properties. note CHR (9) is a tab console. writeline ("{0}" & CHR (9) & "{1}", D. key, D. value) next D 
C # VB

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.