System.Collections Namespace
Represents a collection of key-and-value pairs that are organized based on the hash code of the key.
Visibility |
Constructor |
Parameters |
public |
Hashtable |
( )
|
public |
Hashtable |
(
Int32
capacity
)
|
public |
Hashtable |
(
Int32
capacity
,
Single
loadFactor
)
|
public |
Hashtable |
(
Int32
capacity
,
Single
loadFactor
,
IHashCodeProvider
hcp
,
IComparer
comparer
)
|
public |
Hashtable |
(
Int32
capacity
,
Single
loadFactor
,
IEqualityComparer
equalityComparer
)
|
public |
Hashtable |
(
IHashCodeProvider
hcp
,
IComparer
comparer
)
|
public |
Hashtable |
(
IEqualityComparer
equalityComparer
)
|
public |
Hashtable |
(
Int32
capacity
,
IHashCodeProvider
hcp
,
IComparer
comparer
)
|
public |
Hashtable |
(
Int32
capacity
,
IEqualityComparer
equalityComparer
)
|
public |
Hashtable |
(
IDictionary
d
)
|
public |
Hashtable |
(
IDictionary
d
,
Single
loadFactor
)
|
public |
Hashtable |
(
IDictionary
d
,
IHashCodeProvider
hcp
,
IComparer
comparer
)
|
public |
Hashtable |
(
IDictionary
d
,
IEqualityComparer
equalityComparer
)
|
public |
Hashtable |
(
IDictionary
d
,
Single
loadFactor
,
IHashCodeProvider
hcp
,
IComparer
comparer
)
|
public |
Hashtable |
(
IDictionary
d
,
Single
loadFactor
,
IEqualityComparer
equalityComparer
)
|
|
Each element is a key-and-value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic), but a value can be.
The objects used as keys in a Hashtable must implement or inherit the Object.GetHashCode and Object.Equals methods. If key equality were simply reference equality, the inherited implementation of these methods would suffice. Furthermore, these methods must produce the same results when called with the same parameters while the key exists in the Hashtable. Key objects must be immutable as long as they are used as keys in the Hashtable.
When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element.
The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size. A different load factor can also be specified when the Hashtable is created.
As elements are added to a Hashtable, the actual load factor of the Hashtable increases. When the actual load factor reaches the load factor, the number of buckets in the Hashtable is automatically increased to the smallest prime number that is larger than twice the current number of Hashtable buckets.
Each key object in the Hashtable must provide its own hash function, which can be accessed by calling GetHash. However, any object implementing IHashCodeProvider can be passed to a Hashtable constructor, and that hash function is used for all objects in the table.
The foreach statement of the C# language requires the type of each element in the collection. Since each element of the Hashtable is a key-and-value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. For example: foreach (DictionaryEntry myEntry in myHashtable) {...}
The following example shows how to create and initialize a Hashtable and how to print out its keys and values.
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.
Response.Write ("myHT" );
Response.Write (" Count: {0}", myHT.Count );
Response.Write (" Keys and Values:" );
PrintKeysAndValues ( myHT );
}
public static void PrintKeysAndValues ( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Response.Write ("\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Response.Write ("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
Response.Write ();
}
}
/*
This code produces the following output.
myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
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 properties and values of the Hashtable.
Response.Write ("myHT")
Response.Write (" Count: {0}", myHT.Count)
Response.Write (" Keys and Values:")
PrintKeysAndValues (myHT)
End Sub
Public Shared Sub PrintKeysAndValues (myList As Hashtable)
Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
Response.Write (ControlChars.Tab + "-KEY-" + ControlChars.Tab _
+ "-VALUE-")
While myEnumerator.MoveNext()
Response.Write (ControlChars.Tab + "{0}:" + ControlChars.Tab _ + "{1}", myEnumerator.Key, myEnumerator.Value)
End While
Response.Write ()
End Sub
End Class
' This code produces the following output.
'
' myHT
' Count: 3
' Keys and Values:
' -KEY- -VALUE-
' Third: !
' Second: World
' First: Hello
import System
import 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.
Response.Write ("myHT")
Response.Write (" Count: {0}", myHT.Count)
Response.Write (" Keys and Values:")
PrintKeysAndValues (myHT)
function PrintKeysAndValues (myList : Hashtable){
var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
Response.Write ("\t-KEY-\t-VALUE-")
while(myEnumerator.MoveNext())
Response.Write ("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
Response.Write ()
}
// This code produces the following output.
//
// myHT
// Count: 3
// Keys and Values:
// -KEY- -VALUE-
// Third: !
// Second: World
// First: Hello |
|
C# |
VB |
JScript |
|
|