. NET Framework class library hashtable class

Source: Internet
Author: User
Tags mscorlib

Indicates the set of key/value pairs. These key/value pairs are based on the hash of the key.Code.

For a list of all members of this type, see hashtable members.

System. Object
System. Collections. hashtable
System. Data. propertycollection

[Visual Basic]<Serializable>Public class hashtableImplements idictionary, icollection, ienumerable, iserializable ,__Ideserializationcallback, icloneable[C #][Serializable]Public class hashtable: idictionary, icollection, ienumerable,Iserializable, ideserializationcallback, icloneable[C ++][Serializable]Public _ GC class hashtable: Public idictionary, icollection,Ienumerable, iserializable, ideserializationcallback, icloneable[JScript]PublicSerializableClass hashtable implements idictionary, icollection,Ienumerable, iserializable, ideserializationcallback, icloneable
Thread Security

To support one or more writers,HashtableAll operations on must be executed through the packaging returned by the synchronized method.

Enumeration through a set is essentially not a thread-safe process. Other threads can modify the set even when synchronizing the set, which causes an exception in the number of enumerations. To ensure thread security during enumeration, you can lock the set during the enumeration process or catch exceptions caused by changes made by other threads.

Remarks

Each element is a key/value pair stored in the dictionaryentry object. The key cannot be a null reference (in Visual BasicNothing), But the value is acceptable.

UsedHashtableThe key object in must implement or inherit the object. gethashcode and object. Equals methods. If the key equality only references equality, the inheritance implementation of these methods will meet the needs. In addition, if the key exists inHashtableWhen the same parameters are used to call these methods, these methods must generate the same results. As long as the key object is usedHashtableAnd they must remain unchanged.

When an element is addedHashtableAccording to the hash code of the key, this element is put into the bucket. Subsequent searches for this key will use the hash code of the key to only search in a specific bucket, which will greatly reduce the number of key comparisons required to search for an element.

HashtableDetermine the maximum ratio of elements to buckets. The smaller the load factor, the faster the average search speed, but the increased memory consumption. The default load factor 1.0 usually provides the optimal balance between speed and size. WhenHashtableYou can also specify other loading factors.

WhenHashtableWhen adding elements,HashtableThe actual loading Factor of is increased. When the actual loading factor reaches this loading factor,HashtableThe number of buckets in is automatically increased to greater than the currentHashtableMinimum number of buckets that are twice the number of buckets.

HashtableEach key object in must provide its own hash function, which can be accessed by calling gethash. However, any object implementing ihashcodeprovider can be passedHashtableConstructor. the hash function is used for all objects in the table.

[Visual Basic, C #]C #ForeachStatement (in Visual BasicFor each) The type of each element in the collection. BecauseHashtableEach element of is a key/value pair, so the element type is neither the key type nor the value type. InsteadDictionaryentryType. For example:

 
[C #]Foreach (dictionaryentry myde in myhashtable ){...}[Visual Basic]Dim myde as dictionaryentryfor each myde in myhashtable... Next myde

[Visual Basic, C #] ForeachA statement is a packaging of enumerative numbers. It can only be read from the set and cannot be written to the set.

Example

The following example shows how to create and initializeHashtableAnd how to print out its key and value.

 [Visual Basic] Imports systemimports system. collectionsimports Microsoft. visualbasicpublic class sampleshashtable public shared sub main () 'creates and initializes a new hashtable. dim myht as new hashtable () myht. add ("first", "hello") myht. add ("second", "world") myht. add ("third ","! ") 'Displays the properties and values of the hashtable. console. writeline ("myht") console. writeline ("count: {0}", myht. count) console. writeline ("keys and values:") printkeysandvalues (myht) end sub public shared sub printkeysandvalues (mylist as hashtable) dim myenumerator as idictionaryenumerator = mylist. getenumerator () console. writeline (controlchars. tab + "-key-" + controlchars. tab _ + "-Value -") While myenumerator. movenext () console. writeline (controlchars. tab + "{0}:" + controlchars. tab _ + "{1}", myenumerator. key, myenumerator. value) end while console. writeline () end subend class 'this code produces the following output. ''myht 'count: 3' keys and values: '-key--value-'third :! 'Second: world' first: Hello [C #] Using system; using system. collections; public class sampleshashtable {public static void main () {// creates and initializes a new hashtable. hashtable myht = new hashtable (); myht. add ("first", "hello"); myht. add ("second", "world"); myht. add ("third ","! "); // Displays the properties and values of the hashtable. console. writeline ("myht"); console. writeline ("count: {0}", myht. count); console. writeline ("keys and values:"); printkeysandvalues (myht);} public static void printkeysandvalues (hashtable mylist) {idictionaryenumerator myenumerator = mylist. getenumerator (); console. writeline ("\ t-key-\ t-value-"); While (myenumerator. movenext () Console. writeline ("\ t {0 }:\ t {1}", myenumerator. key, myenumerator. value); console. writeline () ;}/ * This code produces the following output. myht count: 3 keys and values:-key--value-Third :! Second: World First: HELLO */ [C ++] # Using <mscorlib. DLL> # using <system. DLL> using namespace system; using namespace system: collections; Public _ GC class sampleshashtable {public: static void printkeysandvalues (hashtable _ GC * mylist) {idictionaryenumerator _ GC * myenumerator = mylist-> getenumerator (); Console: writeline (S "\ t-key-\ t-value -"); while (myenumerator-> movenext () console: writeline (S "\ t {0 }:\ t {1}", myenumerator-> key, myen Umerator-> value); Console: writeline () ;};}; int main () {// creates and initializes a new hashtable. hashtable _ GC * myht = new hashtable (); myht-> Add (S "first", s "hello"); myht-> Add (S "second ", s "world"); myht-> Add (S "third", s "! "); // Displays the properties and values of the hashtable. console: writeline (S "myht"); Console: writeline (S "count: {0}", _ box (myht-> count); Console :: writeline (S "keys and values:"); sampleshashtable: printkeysandvalues (myht);}/* This code produces the following output. myhtcount: 3 keys and values:-key--value-Third :! Second: worldfirst: HELLO */[JScript] Import systemimport system. collections // creates and initializes a new hashtable. vaR myht: hashtable = new hashtable () myht. add ("first", "hello") myht. add ("second", "world") myht. add ("third ","! ") // Displays the properties and values of the hashtable. console. writeline ("myht") console. writeline ("count: {0}", myht. count) console. writeline ("keys and values:") printkeysandvalues (myht) function printkeysandvalues (mylist: hashtable) {var myenumerator: idictionaryenumerator = mylist. getenumerator () console. writeline ("\ t-key-\ t-value-") while (myenumerator. movenext () console. writeline ("\ t {0 }:\ t { 1} ", myenumerator. key, myenumerator. value) console. writeline ()} // This code produces the following output. /// myht // count: 3 // keys and values: //-key--value-// third :! // Second: World // first: Hello
Requirements

Namespace:System. Collections

Platform:Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 series,. NET Framework Lite version

ProgramSet:Mscorlib (in mscorlib. dll)

See

Hashtable member | system. Collections namespace | idictionary | ihashcodeprovider | object. gethashcode | object. Equals | dictionaryentry

Syntax Based on. NET Framework 1.1
Document version 1.1.1

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.