Hashtable (MSDN)

xiaoxiao2021-03-06  108

Represents a collection of key / value pairs, these keys / values ​​organize the hash code according to the key.

For a list of all members of this type, see the HashTable member.

System.Object System.Collections.Hashtable System.Data.propertyCollection, SYSTEMTION

[Visual Basic]

Public Class Hashtable, PUBLIC CLASHTABLE

Implements iDictionary, Icollection, IEnumerable, iSerializable ,_

_

IdeSerializationCallback, Icloneable

[C #]

[Serializable]

Public Class Hashtable: iDictionary, Icollection, Ienumerable,

ISerializable, IDSerializationCallback, iCloneable

[C ]

[Serializable]

Public __gc class hashtable: public iDictionary, iCollection,

Ienumerable, iSerializable, IDSerializationCallback, iCloneable

[Jscript]

public

Serializable

Class HashTable Implements iDictionary, Icollection,

Ienumerable, iSerializable, IDSerializationCallback, iCloneable

Thread security

To support one or more writers, all operations on the HashTable must be executed by the package returned by the SYNCHRONIZED method.

By collecting enumerations in nature is not a thread safe process. Even when synchronizing the collection, other threads can still modify the collection, which will cause an enumeration number to trigger an exception. To ensure thread security during enumeration, you can lock a collection throughout the enumeration process or capture an exception that caused by other threads.

Note

Each element is a key / value pair stored in the DictionaryEntry object. The key cannot be empty (Nothing in Visual Basic), but the value can be.

Objects used as keys in havehtable must be implemented or inherited to Object.gethashcode and Object.Equals methods. If the key is just references, the inheritance implementation of these methods will meet the needs. Further, if the key is present in the HashTable, these methods must be generated by using the same parameters to call these methods. As long as the key object is used as a key in the HashTable, they must never change.

When an element is added to the HashTable, the element is placed in the bucket according to the hash code according to the key. The subsequent lookup of this button will search using the hash code of the key only in a specific bucket, which will greatly reduce the number of times compared to the key required to find an element.

The loading factor of HashTable determines the maximum ratio of the element and the bucket. The smaller the loading factor, the faster the average lookup speed, but the memory consumes also increases. The default load factor 1.0 typically provides the best balance between speeds and sizes. When you create a HashTable, you can also specify other loading factors.

When an element is added to the HashTable, the actual load factor of HashTable will increase. When the actual load factor reaches this loading factor, the number of storage buckets in the HashTable is automatically added to the minimum number of large numbers greater than the current HashTable storage barrel.

Each key object in HashTable must provide its own hash function to access the function by calling GetHash. However, any object that implements iHashCodeProvider can be passed to the HashTable constructor, and the hash function is used in all objects in the table. [Visual Basic, C #] The Foreach statement in the C # language (for veaching in Visual Basic) requires the type of each element in the collection. Since each element of HashTable is a key / value pair, the element type is neither the type of key, nor is the type of value. It is DictionaryEntry type. E.g:

[C #]

Foreach (DictionaryTry myde in myhashtable) {...}

[Visual Basic]

Dim Myde As Dictionaryentry

For Each Myde in MyHashtable

...

Next myde

[Visual Basic, C #] Foreach statement is a package for the enumerated number, which only allows you to read from a collection and not allowed to write a collection.

Example

The following example shows how to create and initialize HashTable, and how to print out their key and value.

[Visual Basic]

Imports system

Imports system.collections

Imports Microsoft.visualBasic

Public 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 property and value of the.comle.

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 ()

Control.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 Sub

END 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 proferties and value 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}", 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

* /

转载请注明原文地址:https://www.9cbs.com/read-96291.html

New Post(0)