(Teaching Idea C # set II) hash table

Source: Internet
Author: User

In this section we're going to learn the second set, because its features can provide a fairly efficient search method, so it's very useful in real projects, and it's a hash table. The hash inherits the IDictionary interface, and the IDictionary interface provides a key (key)/value set design pattern in which each object in the collection contains a key corresponding to it. The corresponding object (value) in the collection can be found by the key specified, which is most important in defining the public property item, values, keys, where item returns the corresponding value in the collection according to the specified key. Values are used to return all the object elements in the collection, and the keys return the key in the collection, and now you think of the key as the "indexed value" of the value, which can be found by the index value. In addition to the hash table has this key-value pairs of access mechanism, the next section we will also learn a sortlist class, it is a key/value entry and a ArrayList set of the combination of two-yuan search, the performance is more powerful. Next, we'll take a look at what is the access mechanism for key-value pairs, while using instances to understand hashes and Hashtable classes.

Hashing provides a dictionary-like data structure, which is based on the idea that a hash function is an image that maps a collection of keywords to an address set, and its settings are flexible, as long as the address set is not larger than the allowable range. Converts the key value of an element in the collection through a hash function to the index value in the corresponding table. The table of this class is called a hash table, which, by using the hash function, corresponds to a value called a hash code, and then through the index address of the hash code, the specified object element, the value, is found. Now let's take a look at the knowledge points of the hash:

A hash table is a collection of key/value pairs that are organized according to the hash code of the key, and can be found by the key.

This point I will not explain, you have to remember that the operation of the hash of the internal mechanism is essentially the operation of the key.

Key cannot be empty or duplicate, value can be. The key, value can be any data type.

Because it is a key operation, it requires the uniqueness of the key value, while any data type can be stored in the hash table, and the operation of the key value pair is strictly based on its data type. For example, the key value is the integer 1 and the string type of 1, the value of the lookup value of the index address is different, this must be strictly enforced.

Dynamic storage of key/value pairs, capacity automatically increases according to actual needs.

Hashtable the collection object created by the class, the capacity is also automatically increased according to the need, its expansion will be more scientific than ArrayList, not multiply, but based on the load factor to determine, this factor determines how many elements to fill the collection, to continue to expand its capacity.

The syntax for defining a hash Table object is Hashtable hstb = new Hashtable ();

Commonly used properties

Hstb. The number of elements of a key value pair in the Count hash table is the same as ArrayList, and we are focusing on the properties of the following two Hashtable classes:

Hstb.keys gets the set of key values in the collection; Hstb. Values returns the relative Value collection object. Both of these are based on the elements of the object and are read-only and cannot modify their results.

Common methods

Add elements-There are two ways to add a hash table element, the first of which is HSTB. Add (Key,value) must be the form of key-value pairs in addition to "," separated; the second is added by the form of a key to get the value, which can also modify the value in the specified key, then stored in the hash object HSTB, the format is hstb[key]=value; The data type of the key and value.

Delete element-delete element with two kinds of hstb. Clear (); Delete all, and the second is to delete element Hstb based on the key. Remove (key);

Find element-Hstb. Contains (key), Hstb. ContainsKey (key) to see if the specified key is included.

Hstb. Containsvalue (value) contains the specified value and returns TRUE or false, reflecting the query result.

Traversal elements-Traversal has two forms, the first: the hash element is a key-value pair, and the DictionaryEntry type is a set of key-value pairs that use this type of object to traverse the Hstb object

For example: foreach (DictionaryEntry jzd in Hstb) traverses the object that is a hash of the key value pair, and then uses the JZD. Key to get keys, Jzd. Value is obtained.

The second traversal is each key or value that traverses the collection of keys or values, such as foreach (Object K in Hstb). Keys) or foreach (Objectv in Hstb. Values)

Note: Because the data type of the key or value can be of any type, the data type of the variable that holds the key or value is best for the object, which contains any type, after the collection is traversed.

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.