Hashtable 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 Hashtable 類別的新執行個體。
多載
Hashtable() |
使用預設初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。 |
Hashtable(Int32, Single, IHashCodeProvider, IComparer) |
已淘汰.
已淘汰.
使用指定的初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。 |
Hashtable(IDictionary, Single, IHashCodeProvider, IComparer) |
已淘汰.
已淘汰.
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數、雜湊程式碼提供者和比較子。 |
Hashtable(Int32, Single, IEqualityComparer) |
使用指定的初始容量、載入因數和 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。 |
Hashtable(Int32, IHashCodeProvider, IComparer) |
已淘汰.
已淘汰.
使用指定的初始容量、雜湊程式碼提供者、比較子和預設的載入因數,初始化 Hashtable 類別的空白新執行個體。 |
Hashtable(IDictionary, Single, IEqualityComparer) |
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數和 IEqualityComparer 物件。 |
Hashtable(IDictionary, IHashCodeProvider, IComparer) |
已淘汰.
已淘汰.
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數,以及指定的雜湊程式碼提供者和比較子。 這個 API 已經過時。 請參閱 Hashtable(IDictionary, IEqualityComparer) 以瞭解替代步驟。 |
Hashtable(Int32, Single) |
使用指定的初始容量和載入因數,以及預設的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。 |
Hashtable(SerializationInfo, StreamingContext) |
已淘汰.
初始化 Hashtable 類別的空白新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfo 和 StreamingContext 物件。 |
Hashtable(IHashCodeProvider, IComparer) |
已淘汰.
已淘汰.
已淘汰.
使用預設的初始容量和載入因數,以及指定的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。 |
Hashtable(IDictionary, Single) |
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數,以及預設的雜湊程式碼提供者和比較子。 |
Hashtable(IDictionary, IEqualityComparer) |
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數以及指定的 IEqualityComparer 物件。 |
Hashtable(Int32) |
使用指定的初始容量以及預設的載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。 |
Hashtable(IEqualityComparer) |
使用預設的初始容量和載入因數,以及指定的 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。 |
Hashtable(IDictionary) |
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設載入因數、雜湊程式碼提供者和比較子。 |
Hashtable(Int32, IEqualityComparer) |
使用指定的初始容量和 Hashtable,以及預設的載入因數,初始化 IEqualityComparer 類別的空白新執行個體。 |
Hashtable()
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用預設初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。
public:
Hashtable();
public Hashtable ();
Public Sub New ()
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myComparer : IEqualityComparer
{
public:
virtual bool Equals(Object^ x, Object^ y)
{
return x->Equals(y);
}
virtual int GetHashCode(Object^ obj)
{
return obj->ToString()->ToLower()->GetHashCode();
}
};
ref class myCultureComparer : IEqualityComparer
{
private:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
virtual int GetHashCode(Object^ obj)
{
return obj->ToString()->ToLower()->GetHashCode();
}
};
int main()
{
// Create a hash table using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
myHT1->Add( "FIRST", "Hello" );
myHT1->Add( "SECOND", "World" );
myHT1->Add( "THIRD", "!" );
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
myHT2->Add( "FIRST", "Hello" );
myHT2->Add( "SECOND", "World" );
myHT2->Add( "THIRD", "!" );
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable^ myHT3 = gcnew Hashtable(
CaseInsensitiveHashCodeProvider::DefaultInvariant,
CaseInsensitiveComparer::DefaultInvariant);
myHT3->Add( "FIRST", "Hello" );
myHT3->Add( "SECOND", "World" );
myHT3->Add( "THIRD", "!" );
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
myHT4->Add( "FIRST", "Hello" );
myHT4->Add( "SECOND", "World" );
myHT4->Add( "THIRD", "!" );
// Search for a key in each hash table.
Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
using System;
using System.Collections;
using System.Globalization;
class myComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
var myHT1 = new Hashtable();
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
var myHT2 = new Hashtable(new myComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable myHT3 = new Hashtable(
CaseInsensitiveHashCodeProvider.DefaultInvariant,
CaseInsensitiveComparer.DefaultInvariant);
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT4 = new Hashtable(new myCultureComparer(myCul));
myHT4.Add("FIRST", "Hello");
myHT4.Add("SECOND", "World");
myHT4.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myComparer
Implements IEqualityComparer
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return x.Equals(y)
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable()
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the default Object.Equals to determine equality.
Dim myHT2 As New Hashtable(New myComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using a case-insensitive hash code provider and
' case-insensitive comparer based on the InvariantCulture.
Dim myHT3 As New Hashtable( _
CaseInsensitiveHashCodeProvider.DefaultInvariant, _
CaseInsensitiveComparer.DefaultInvariant)
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
myHT4.Add("FIRST", "Hello")
myHT4.Add("SECOND", "World")
myHT4.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False
備註
哈希表的容量是用來根據負載因數計算哈希表貯體的最佳數目。 容量會視需要自動增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
此建構函式是 O(1)
作業。
另請參閱
適用於
Hashtable(Int32, Single, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
Please use Hashtable(int, float, IEqualityComparer) instead.
警告
This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.
使用指定的初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。
public:
Hashtable(int capacity, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)
參數
- loadFactor
- Single
從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。 結果是雜湊桶項目的最大比例。
IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。
-或-
要使用預設雜湊程式碼提供者的 null
是每個索引鍵的 GetHashCode() 之實作。
- comparer
- IComparer
要用來判斷兩個索引鍵是否相等的 IComparer 物件。
-或-
表示使用預設比較子的 null
,預設比較子是每個索引鍵的 Equals(Object) 實作。
- 屬性
例外狀況
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
myHT1->Add("FIRST", "Hello");
myHT1->Add("SECOND", "World");
myHT1->Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
myHT2->Add("FIRST", "Hello");
myHT2->Add("SECOND", "World");
myHT2->Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
myHT3->Add("FIRST", "Hello");
myHT3->Add("SECOND", "World");
myHT3->Add("THIRD", "!");
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3, .8f);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
指定初始容量不需要在將元素新增至 Hashtable 物件時執行一些重設大小作業。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。 1.0 的負載因數是速度與大小之間的最佳平衡。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
自訂哈希程式代碼提供者和自定義比較子可啟用案例,例如使用不區分大小寫的字串進行查閱。
這個建構函式是 O(n)
作業,其中 n
是 capacity
參數。
另請參閱
適用於
Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
Please use Hashtable(IDictionary, float, IEqualityComparer) instead.
警告
This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.
public:
Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)
參數
要複製到新 IDictionary 物件的 Hashtable 物件。
- loadFactor
- Single
從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。 結果是雜湊桶項目的最大比例。
IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。
-或-
要使用預設雜湊程式碼提供者的 null
是每個索引鍵的 GetHashCode() 之實作。
- comparer
- IComparer
要用來判斷兩個索引鍵是否相等的 IComparer 物件。
-或-
表示使用預設比較子的 null
,預設比較子是每個索引鍵的 Equals(Object) 實作。
- 屬性
例外狀況
d
為 null
。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create the dictionary.
SortedList^ mySL = gcnew SortedList;
mySL->Add( "FIRST", "Hello" );
mySL->Add( "SECOND", "World" );
mySL->Add( "THIRD", "!" );
// Create a hash table using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
// Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
// Create a hash table using the specified KeyComparer.
// The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
// which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
// Search for a key in each hash table.
Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
SortedList mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(mySL, .8f);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(mySL, .8f,
new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
初始容量會設定為來源字典中的元素數目。 根據負載因數,容量會自動視需要增加。
負載因數是專案與值區的最大比率。 較小的負載因數表示以增加的記憶體耗用量來加快查閱速度。 1.0 的負載因數是速度與大小之間的最佳平衡。
當實際負載因數達到指定的負載因數時,值區數目會自動增加為大於目前值區數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是 索引鍵的 實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的 實作 Object.Equals。
自訂哈希程式代碼提供者和自定義比較子會啟用案例,例如使用不區分大小寫的字串進行查閱。
新 Hashtable 專案的排序順序與列舉值逐 IDictionary 一查看 對象的順序相同。
此建構函式是作業 O(n)
,其中 n
是參數中的 d
項目數目。
適用於
Hashtable(Int32, Single, IEqualityComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用指定的初始容量、載入因數和 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。
public:
Hashtable(int capacity, float loadFactor, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : int * single * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, equalityComparer As IEqualityComparer)
參數
- loadFactor
- Single
從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。 結果是雜湊桶項目的最大比例。
- equalityComparer
- IEqualityComparer
IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。
-或-
null
表示使用預設雜湊程式碼提供者和預設比較子。 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。
例外狀況
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式來建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
myHT1->Add("FIRST", "Hello");
myHT1->Add("SECOND", "World");
myHT1->Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
myHT2->Add("FIRST", "Hello");
myHT2->Add("SECOND", "World");
myHT2->Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
myHT3->Add("FIRST", "Hello");
myHT3->Add("SECOND", "World");
myHT3->Add("THIRD", "!");
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3, .8f);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
指定初始容量不需要在將元素新增至 Hashtable 物件時執行數個重設大小作業。 根據負載因數,容量會自動視需要增加。
負載因數是專案與值區的最大比率。 較小的負載因數表示以增加的記憶體耗用量來加快查閱速度。 1.0 的負載因數是速度與大小之間的最佳平衡。
當實際負載因數達到指定的負載因數時,值區數目會自動增加為大於目前值區數目兩倍的最小質數。
物件 IEqualityComparer 同時包含哈希碼提供者和比較子。 IEqualityComparer如果在建構函式中使用 Hashtable ,則不需要用來作為 中Hashtable索引鍵的對象來覆寫 Object.GetHashCode 和 Object.Equals 方法。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是 索引鍵的 實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的 實作 Object.Equals。
會 IEqualityComparer 啟用像是使用不區分大小寫字串進行查閱等案例。
此建構函式是作業 O(n)
,其中 n
是 capacity
參數。
另請參閱
適用於
Hashtable(Int32, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
Please use Hashtable(int, IEqualityComparer) instead.
警告
This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.
使用指定的初始容量、雜湊程式碼提供者、比較子和預設的載入因數,初始化 Hashtable 類別的空白新執行個體。
public:
Hashtable(int capacity, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, hcp As IHashCodeProvider, comparer As IComparer)
參數
IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。
-或-
要使用預設雜湊程式碼提供者的 null
是每個索引鍵的 GetHashCode() 之實作。
- comparer
- IComparer
要用來判斷兩個索引鍵是否相等的 IComparer 物件。
-或-
表示使用預設比較子的 null
,預設比較子是每個索引鍵的 Equals(Object) 實作。
- 屬性
例外狀況
capacity
小於零。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式來建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(3);
myHT1->Add("FIRST", "Hello");
myHT1->Add("SECOND", "World");
myHT1->Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
myHT2->Add("FIRST", "Hello");
myHT2->Add("SECOND", "World");
myHT2->Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
myHT3->Add("FIRST", "Hello");
myHT3->Add("SECOND", "World");
myHT3->Add("THIRD", "!");
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3)
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
指定初始容量不需要在將元素新增至 Hashtable 物件時執行數個重設大小作業。 根據負載因數,容量會自動視需要增加。
負載因數是專案與值區的最大比率。 較小的負載因數表示以增加的記憶體耗用量來加快查閱速度。
當實際負載因數達到指定的負載因數時,值區數目會自動增加為大於目前值區數目兩倍的最小質數。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是 索引鍵的 實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的 實作 Object.Equals。
自訂哈希程式代碼提供者和自定義比較子會啟用案例,例如使用不區分大小寫的字串進行查閱。
此建構函式是作業 O(n)
,其中 n
是 capacity
參數。
另請參閱
適用於
Hashtable(IDictionary, Single, IEqualityComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數和 IEqualityComparer 物件。
public:
Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, equalityComparer As IEqualityComparer)
參數
要複製到新 IDictionary 物件的 Hashtable 物件。
- loadFactor
- Single
從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。 結果是雜湊桶項目的最大比例。
- equalityComparer
- IEqualityComparer
IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。
-或-
null
表示使用預設雜湊程式碼提供者和預設比較子。 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。
例外狀況
d
為 null
。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式來建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create the dictionary.
SortedList^ mySL = gcnew SortedList;
mySL->Add( "FIRST", "Hello" );
mySL->Add( "SECOND", "World" );
mySL->Add( "THIRD", "!" );
// Create a hash table using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
// Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
// Create a hash table using the specified KeyComparer.
// The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
// which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
// Search for a key in each hash table.
Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
SortedList mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(mySL, .8f);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(mySL, .8f,
new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
初始容量會設定為來源字典中的元素數目。 根據負載因數,容量會自動視需要增加。
負載因數是專案與值區的最大比率。 較小的負載因數表示以增加的記憶體耗用量來加快查閱速度。 1.0 的負載因數是速度與大小之間的最佳平衡。
當實際負載因數達到指定的負載因數時,值區數目會自動增加為大於目前值區數目兩倍的最小質數。
物件 IEqualityComparer 同時包含哈希碼提供者和比較子。 IEqualityComparer如果在建構函式中使用 Hashtable ,則不需要物件中Hashtable做為索引鍵的物件來覆寫 Object.GetHashCode 和 Object.Equals 方法。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是 索引鍵的 實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
會 IEqualityComparer 啟用使用不區分大小寫字串進行查閱等案例。
新 Hashtable 專案的排序順序與列舉值 IDictionary 逐一查看 對象的順序相同。
這個建構函式是 O(n)
作業,其中 n
是 參數中的 d
元素數目。
另請參閱
適用於
Hashtable(IDictionary, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
Please use Hashtable(IDictionary, IEqualityComparer) instead.
警告
This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數,以及指定的雜湊程式碼提供者和比較子。 這個 API 已經過時。 請參閱 Hashtable(IDictionary, IEqualityComparer) 以瞭解替代步驟。
public:
Hashtable(System::Collections::IDictionary ^ d, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, hcp As IHashCodeProvider, comparer As IComparer)
參數
要複製到新 IDictionary 物件的 Hashtable 物件。
IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。
-或-
要使用預設雜湊程式碼提供者的 null
是每個索引鍵的 GetHashCode() 之實作。
- comparer
- IComparer
要用來判斷兩個索引鍵是否相等的 IComparer 物件。
-或-
表示使用預設比較子的 null
,預設比較子是每個索引鍵的 Equals(Object) 實作。
- 屬性
例外狀況
d
為 null
。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create the dictionary.
SortedList^ mySL = gcnew SortedList();
mySL->Add("FIRST", "Hello");
mySL->Add("SECOND", "World");
mySL->Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
};
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
var mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
var myHT1 = new Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
var myHT2 = new Hashtable(mySL, new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL)
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
初始容量會設定為來源字典中的元素數目。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
自訂哈希程式代碼提供者和自定義比較子可啟用案例,例如使用不區分大小寫的字串進行查閱。
新 Hashtable 專案的排序順序與列舉值 IDictionary 逐一查看 對象的順序相同。
這個建構函式是 O(n)
作業,其中 n
是 參數中的 d
元素數目。
另請參閱
適用於
Hashtable(Int32, Single)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用指定的初始容量和載入因數,以及預設的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。
public:
Hashtable(int capacity, float loadFactor);
public Hashtable (int capacity, float loadFactor);
new System.Collections.Hashtable : int * single -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single)
參數
- loadFactor
- Single
從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。 結果是雜湊桶項目的最大比例。
例外狀況
capacity
導致溢位。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
myHT1->Add("FIRST", "Hello");
myHT1->Add("SECOND", "World");
myHT1->Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
myHT2->Add("FIRST", "Hello");
myHT2->Add("SECOND", "World");
myHT2->Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
myHT3->Add("FIRST", "Hello");
myHT3->Add("SECOND", "World");
myHT3->Add("THIRD", "!");
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3, .8f);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
指定初始容量不需要在將元素新增至 Hashtable 物件時執行一些重設大小作業。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。 1.0 的負載因數是速度與大小之間的最佳平衡。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
這個建構函式是 O(n)
作業,其中 n
是 capacity
參數。
另請參閱
適用於
Hashtable(SerializationInfo, StreamingContext)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
初始化 Hashtable 類別的空白新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfo 和 StreamingContext 物件。
protected:
Hashtable(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected Hashtable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected Hashtable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
Protected Sub New (info As SerializationInfo, context As StreamingContext)
參數
- info
- SerializationInfo
SerializationInfo 物件,含有要序列化 Hashtable 物件的必要資訊。
- context
- StreamingContext
StreamingContext 物件,包含 Hashtable 所關聯之序列化資料流的來源及目的地。
- 屬性
例外狀況
info
為 null
。
備註
哈希表的容量是用來根據負載因數計算哈希表貯體的最佳數目。 容量會視需要自動增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
這個建構函式是作業 O(n)
,其中 n
是 Count。
由於串行化和還原串行化 的 Hashtable 列舉值可能會導致元素重新排序,因此無法繼續列舉而不呼叫 Reset 方法。
另請參閱
- ISerializable
- SerializationInfo
- StreamingContext
- OnDeserialization(Object)
- GetHashCode()
- Equals(Object)
適用於
Hashtable(IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
Please use Hashtable(IEqualityComparer) instead.
警告
This constructor has been deprecated. Use Hashtable(IEqualityComparer).
警告
This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.
使用預設的初始容量和載入因數,以及指定的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。
public:
Hashtable(System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")]
public Hashtable (System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (hcp As IHashCodeProvider, comparer As IComparer)
參數
IHashCodeProvider 物件,為 Hashtable 物件中的所有索引鍵提供雜湊程式碼。
-或-
要使用預設雜湊程式碼提供者的 null
是每個索引鍵的 GetHashCode() 之實作。
- comparer
- IComparer
要用來判斷兩個索引鍵是否相等的 IComparer 物件。
-或-
表示使用預設比較子的 null
,預設比較子是每個索引鍵的 Equals(Object) 實作。
- 屬性
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myComparer : IEqualityComparer
{
public:
virtual bool Equals(Object^ x, Object^ y)
{
return x->Equals(y);
}
virtual int GetHashCode(Object^ obj)
{
return obj->ToString()->ToLower()->GetHashCode();
}
};
ref class myCultureComparer : IEqualityComparer
{
private:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
virtual int GetHashCode(Object^ obj)
{
return obj->ToString()->ToLower()->GetHashCode();
}
};
int main()
{
// Create a hash table using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
myHT1->Add( "FIRST", "Hello" );
myHT1->Add( "SECOND", "World" );
myHT1->Add( "THIRD", "!" );
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
myHT2->Add( "FIRST", "Hello" );
myHT2->Add( "SECOND", "World" );
myHT2->Add( "THIRD", "!" );
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable^ myHT3 = gcnew Hashtable(
CaseInsensitiveHashCodeProvider::DefaultInvariant,
CaseInsensitiveComparer::DefaultInvariant);
myHT3->Add( "FIRST", "Hello" );
myHT3->Add( "SECOND", "World" );
myHT3->Add( "THIRD", "!" );
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
myHT4->Add( "FIRST", "Hello" );
myHT4->Add( "SECOND", "World" );
myHT4->Add( "THIRD", "!" );
// Search for a key in each hash table.
Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
using System;
using System.Collections;
using System.Globalization;
class myComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
var myHT1 = new Hashtable();
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
var myHT2 = new Hashtable(new myComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable myHT3 = new Hashtable(
CaseInsensitiveHashCodeProvider.DefaultInvariant,
CaseInsensitiveComparer.DefaultInvariant);
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT4 = new Hashtable(new myCultureComparer(myCul));
myHT4.Add("FIRST", "Hello");
myHT4.Add("SECOND", "World");
myHT4.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myComparer
Implements IEqualityComparer
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return x.Equals(y)
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable()
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the default Object.Equals to determine equality.
Dim myHT2 As New Hashtable(New myComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using a case-insensitive hash code provider and
' case-insensitive comparer based on the InvariantCulture.
Dim myHT3 As New Hashtable( _
CaseInsensitiveHashCodeProvider.DefaultInvariant, _
CaseInsensitiveComparer.DefaultInvariant)
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
myHT4.Add("FIRST", "Hello")
myHT4.Add("SECOND", "World")
myHT4.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False
備註
哈希表的容量是用來根據負載因數計算哈希表貯體的最佳數目。 容量會視需要自動增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
自訂哈希程式代碼提供者和自定義比較子可啟用案例,例如使用不區分大小寫的字串進行查閱。
此建構函式是 O(1)
作業。
另請參閱
適用於
Hashtable(IDictionary, Single)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
public:
Hashtable(System::Collections::IDictionary ^ d, float loadFactor);
public Hashtable (System.Collections.IDictionary d, float loadFactor);
new System.Collections.Hashtable : System.Collections.IDictionary * single -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single)
參數
要複製到新 IDictionary 物件的 Hashtable 物件。
- loadFactor
- Single
從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。 結果是雜湊桶項目的最大比例。
例外狀況
d
為 null
。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create the dictionary.
SortedList^ mySL = gcnew SortedList;
mySL->Add( "FIRST", "Hello" );
mySL->Add( "SECOND", "World" );
mySL->Add( "THIRD", "!" );
// Create a hash table using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
// Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
// Create a hash table using the specified KeyComparer.
// The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
// which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
// Search for a key in each hash table.
Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
SortedList mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(mySL, .8f);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(mySL, .8f,
new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
初始容量會設定為來源字典中的元素數目。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。 1.0 的負載因數是速度與大小之間的最佳平衡。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
新 Hashtable 專案的排序順序與列舉值 IDictionary 逐一查看 對象的順序相同。
這個建構函式是 O(n)
作業,其中 n
是 參數中的 d
元素數目。
另請參閱
適用於
Hashtable(IDictionary, IEqualityComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數以及指定的 IEqualityComparer 物件。
public:
Hashtable(System::Collections::IDictionary ^ d, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, equalityComparer As IEqualityComparer)
參數
要複製到新 IDictionary 物件的 Hashtable 物件。
- equalityComparer
- IEqualityComparer
IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。
-或-
null
表示使用預設雜湊程式碼提供者和預設比較子。 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。
例外狀況
d
為 null
。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create the dictionary.
SortedList^ mySL = gcnew SortedList();
mySL->Add("FIRST", "Hello");
mySL->Add("SECOND", "World");
mySL->Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
};
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
var mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
var myHT1 = new Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
var myHT2 = new Hashtable(mySL, new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL)
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
初始容量會設定為來源字典中的元素數目。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
物件 IEqualityComparer 同時包含哈希碼提供者和比較子。 IEqualityComparer如果在建構函式中使用 ,則不需要在物件中HashtableHashtable做為索引鍵的物件來覆寫 Object.GetHashCode 和 Object.Equals 方法。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
會 IEqualityComparer 啟用使用不區分大小寫字串進行查閱等案例。
新 Hashtable 專案的排序順序與列舉值 IDictionary 逐一查看 對象的順序相同。
這個建構函式是 O(n)
作業,其中 n
是 參數中的 d
元素數目。
另請參閱
適用於
Hashtable(Int32)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用指定的初始容量以及預設的載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。
public:
Hashtable(int capacity);
public Hashtable (int capacity);
new System.Collections.Hashtable : int -> System.Collections.Hashtable
Public Sub New (capacity As Integer)
參數
例外狀況
capacity
小於零。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(3);
myHT1->Add("FIRST", "Hello");
myHT1->Add("SECOND", "World");
myHT1->Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
myHT2->Add("FIRST", "Hello");
myHT2->Add("SECOND", "World");
myHT2->Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
myHT3->Add("FIRST", "Hello");
myHT3->Add("SECOND", "World");
myHT3->Add("THIRD", "!");
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3)
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
指定初始容量不需要在將元素新增至 Hashtable 物件時執行一些重設大小作業。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
這個建構函式是作業 O(n)
,其中 n
是 capacity
。
另請參閱
適用於
Hashtable(IEqualityComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用預設的初始容量和載入因數,以及指定的 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。
public:
Hashtable(System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IEqualityComparer equalityComparer);
public Hashtable (System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (equalityComparer As IEqualityComparer)
參數
- equalityComparer
- IEqualityComparer
IEqualityComparer 物件,它會定義與 Hashtable 物件並用的雜湊程式碼提供者和比較子。
-或-
null
表示使用預設雜湊程式碼提供者和預設比較子。 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myComparer : IEqualityComparer
{
public:
virtual bool Equals(Object^ x, Object^ y)
{
return x->Equals(y);
}
virtual int GetHashCode(Object^ obj)
{
return obj->ToString()->ToLower()->GetHashCode();
}
};
ref class myCultureComparer : IEqualityComparer
{
private:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
virtual int GetHashCode(Object^ obj)
{
return obj->ToString()->ToLower()->GetHashCode();
}
};
int main()
{
// Create a hash table using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
myHT1->Add( "FIRST", "Hello" );
myHT1->Add( "SECOND", "World" );
myHT1->Add( "THIRD", "!" );
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
myHT2->Add( "FIRST", "Hello" );
myHT2->Add( "SECOND", "World" );
myHT2->Add( "THIRD", "!" );
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable^ myHT3 = gcnew Hashtable(
CaseInsensitiveHashCodeProvider::DefaultInvariant,
CaseInsensitiveComparer::DefaultInvariant);
myHT3->Add( "FIRST", "Hello" );
myHT3->Add( "SECOND", "World" );
myHT3->Add( "THIRD", "!" );
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
myHT4->Add( "FIRST", "Hello" );
myHT4->Add( "SECOND", "World" );
myHT4->Add( "THIRD", "!" );
// Search for a key in each hash table.
Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
using System;
using System.Collections;
using System.Globalization;
class myComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
var myHT1 = new Hashtable();
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
var myHT2 = new Hashtable(new myComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable myHT3 = new Hashtable(
CaseInsensitiveHashCodeProvider.DefaultInvariant,
CaseInsensitiveComparer.DefaultInvariant);
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT4 = new Hashtable(new myCultureComparer(myCul));
myHT4.Add("FIRST", "Hello");
myHT4.Add("SECOND", "World");
myHT4.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myComparer
Implements IEqualityComparer
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return x.Equals(y)
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable()
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the default Object.Equals to determine equality.
Dim myHT2 As New Hashtable(New myComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using a case-insensitive hash code provider and
' case-insensitive comparer based on the InvariantCulture.
Dim myHT3 As New Hashtable( _
CaseInsensitiveHashCodeProvider.DefaultInvariant, _
CaseInsensitiveComparer.DefaultInvariant)
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
myHT4.Add("FIRST", "Hello")
myHT4.Add("SECOND", "World")
myHT4.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False
備註
哈希表的容量是用來根據負載因數計算哈希表貯體的最佳數目。 容量會視需要自動增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
物件 IEqualityComparer 同時包含哈希碼提供者和比較子。 IEqualityComparer如果在建構函式中使用 ,則不需要在物件中HashtableHashtable做為索引鍵的物件來覆寫 Object.GetHashCode 和 Object.Equals 方法。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
會 IEqualityComparer 啟用使用不區分大小寫字串進行查閱等案例。
此建構函式是 O(1)
作業。
另請參閱
適用於
Hashtable(IDictionary)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
public:
Hashtable(System::Collections::IDictionary ^ d);
public Hashtable (System.Collections.IDictionary d);
new System.Collections.Hashtable : System.Collections.IDictionary -> System.Collections.Hashtable
Public Sub New (d As IDictionary)
參數
要複製到新 IDictionary 物件的 Hashtable 物件。
例外狀況
d
為 null
。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create the dictionary.
SortedList^ mySL = gcnew SortedList();
mySL->Add("FIRST", "Hello");
mySL->Add("SECOND", "World");
mySL->Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
};
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
var mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
var myHT1 = new Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
var myHT2 = new Hashtable(mySL, new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL)
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
初始容量會設定為來源字典中的元素數目。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加記憶體耗用量的成本進行更快速的查閱。
當實際負載因數達到指定的負載因數時,值區數目會自動增加至大於目前貯體數目兩倍的最小質數。
哈希碼提供者會為 物件中的 Hashtable 索引鍵分配哈希碼。 預設哈希碼提供者是的索引鍵實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的實作 Object.Equals。
新 Hashtable 專案的排序順序與列舉值 IDictionary 逐一查看 對象的順序相同。
這個建構函式是 O(n)
作業,其中 n
是 參數中的 d
元素數目。
另請參閱
適用於
Hashtable(Int32, IEqualityComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用指定的初始容量和 Hashtable,以及預設的載入因數,初始化 IEqualityComparer 類別的空白新執行個體。
public:
Hashtable(int capacity, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (int capacity, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (int capacity, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : int * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, equalityComparer As IEqualityComparer)
參數
- equalityComparer
- IEqualityComparer
IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。
-或-
null
表示使用預設雜湊程式碼提供者和預設比較子。 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。
例外狀況
capacity
小於零。
範例
下列程式代碼範例會使用不同的 Hashtable 建構函式建立哈希表,並示範哈希表行為的差異,即使每個哈希表都包含相同的元素也一樣。
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
ref class myCultureComparer : public IEqualityComparer
{
public:
CaseInsensitiveComparer^ myComparer;
public:
myCultureComparer()
{
myComparer = CaseInsensitiveComparer::DefaultInvariant;
}
public:
myCultureComparer(CultureInfo^ myCulture)
{
myComparer = gcnew CaseInsensitiveComparer(myCulture);
}
public:
virtual bool Equals(Object^ x, Object^ y)
{
if (myComparer->Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public:
virtual int GetHashCode(Object^ obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj->ToString()->ToLower()->GetHashCode();
}
};
public ref class SamplesHashtable
{
public:
static void Main()
{
// Create a hash table using the default comparer.
Hashtable^ myHT1 = gcnew Hashtable(3);
myHT1->Add("FIRST", "Hello");
myHT1->Add("SECOND", "World");
myHT1->Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
myHT2->Add("FIRST", "Hello");
myHT2->Add("SECOND", "World");
myHT2->Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
myHT3->Add("FIRST", "Hello");
myHT3->Add("SECOND", "World");
myHT3->Add("THIRD", "!");
// Search for a key in each hash table.
Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
}
};
int main()
{
SamplesHashtable::Main();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3)
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
備註
指定初始容量不需要在將元素新增至 Hashtable 物件時執行一些重設大小作業。 根據負載因數,容量會自動視需要增加。
負載因數是元素與貯體的最大比率。 較小的負載因數表示以增加的記憶體耗用量來加快查閱速度。
當實際負載因數達到指定的負載因數時,值區數目會自動增加為大於目前值區數目兩倍的最小質數。
物件 IEqualityComparer 同時包含哈希碼提供者和比較子。 IEqualityComparer如果在建構函式中使用 Hashtable ,則不需要用來作為 中Hashtable索引鍵的對象來覆寫 Object.GetHashCode 和 Object.Equals 方法。
哈希碼提供者會為 中的 Hashtable索引鍵分配哈希碼。 預設哈希碼提供者是 索引鍵的 實作 Object.GetHashCode。
比較子會判斷兩個索引鍵是否相等。 中的每個索引鍵都必須是唯一 Hashtable 的。 默認比較子是 索引鍵的 實作 Object.Equals。
會 IEqualityComparer 啟用像是使用不區分大小寫字串進行查閱等案例。
此建構函式是作業 O(n)
,其中 n
是 capacity
參數。