Hashtable 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 Hashtable 類別的新執行個體。
多載
Hashtable()
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
使用預設的初始容量、負載因子、雜湊碼提供者及比較器,初始化一個新的空實例 Hashtable 。
public:
Hashtable();
public Hashtable();
Public Sub New ()
範例
以下程式碼範例使用不同的 Hashtable 建構子建立雜湊表,並展示雜湊表行為上的差異,即使每個雜湊表包含相同元素。
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
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.
警告
Please use Hashtable(int, float, IEqualityComparer) instead.
使用指定的初始容量、負載因子、雜湊碼提供者及比較器,初始化一個新的空實例 Hashtable 。
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);
[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("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
[<System.Obsolete("Please 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 之間的數字,乘以提供最佳效能的預設值。 結果就是元素與桶的最大比例。
- 屬性
例外狀況
備註
指定初始容量可免除在物件中加入元素 Hashtable 時進行多次調整大小操作的需求。 容量會根據負載因數自動增加。
載重因數是元件與鏟斗的最大比值。 較低的負載因子意味著查詢速度更快,但代價是記憶體消耗增加。 載重因子為1.0是速度與體積間最佳平衡。
當實際負載因子達到指定負載因子時,桶數會自動增加到最小且大於當前桶數兩倍的質數。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
自訂雜湊碼提供者與自訂比較器可實現使用大小寫不區分字串的查詢等情境。
此構造子是一個 O(n) 運算,其中 n 為 capacity 參數。
另請參閱
適用於
Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.
警告
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);
[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("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
[<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
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 之間的數字,乘以提供最佳效能的預設值。 結果就是元素與桶的最大比例。
- 屬性
例外狀況
d 是 null。
備註
初始容量設定為來源字典中的元素數量。 容量會根據負載因數自動增加。
載重因數是元件與鏟斗的最大比值。 較低的負載因子意味著查詢速度更快,但代價是記憶體消耗增加。 載重因子為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.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 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 物件包含雜湊碼提供者與比較器。 若建構子中使用 aIEqualityComparer,作為鍵的Hashtable物件不必覆蓋 Hashtable and Object.GetHashCodeObject.Equals 方法。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
這 IEqualityComparer 讓使用大小寫不區分字串的查找等情境成為可能。
此構造子是一個 O(n) 運算,其中 n 為 capacity 參數。
另請參閱
適用於
Hashtable(Int32, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.
警告
Please use Hashtable(int, IEqualityComparer) instead.
使用指定的初始容量、雜湊碼提供者、比較器及預設載入因子,初始化一個新的空實例 Hashtable 。
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);
[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("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("Please 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)
參數
- 屬性
例外狀況
capacity 小於零。
備註
指定初始容量可免除在物件中加入元素 Hashtable 時進行多次調整大小操作的需求。 容量會根據負載因數自動增加。
載重因數是元件與鏟斗的最大比值。 較低的負載因子意味著查詢速度更快,但代價是記憶體消耗增加。
當實際負載因子達到指定負載因子時,桶數會自動增加到最小且大於當前桶數兩倍的質數。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
自訂雜湊碼提供者與自訂比較器可實現使用大小寫不區分字串的查詢等情境。
此構造子是一個 O(n) 運算,其中 n 為 capacity 參數。
另請參閱
適用於
Hashtable(IDictionary, Single, IEqualityComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- 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 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 物件包含雜湊碼提供者與比較器。 若建構子中使用 aIEqualityComparer,物件中用作鍵的Hashtable物件不必覆蓋 Hashtable and Object.GetHashCodeObject.Equals 方法。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
這 IEqualityComparer 讓使用大小寫不區分字串的查找等情境成為可能。
新物件 Hashtable 的元素排序順序與列舉器遍歷 IDictionary 物件的順序相同。
此構造子是一個 O(n) 運算,其中 n 是參數中的 d 元素數。
另請參閱
適用於
Hashtable(IDictionary, IHashCodeProvider, IComparer)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.
警告
Please 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("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);
[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("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
[<System.Obsolete("Please 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 物件的物件。
- 屬性
例外狀況
d 是 null。
備註
初始容量設定為來源字典中的元素數量。 容量會根據負載因數自動增加。
載重因數是元件與鏟斗的最大比值。 較低的負載因子意味著查詢速度更快,但代價是記憶體消耗增加。
當實際負載因子達到指定負載因子時,桶數會自動增加到最小且大於當前桶數兩倍的質數。
雜湊碼提供者會為物件中的 Hashtable 鍵分配雜湊碼。 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
自訂雜湊碼提供者與自訂比較器可實現使用大小寫不區分字串的查詢等情境。
新物件 Hashtable 的元素排序順序與列舉器遍歷 IDictionary 物件的順序相同。
此構造子是一個 O(n) 運算,其中 n 是參數中的 d 元素數。
另請參閱
適用於
Hashtable(Int32, Single)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- 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 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
- 來源:
- 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);
[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);
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}")>]
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
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
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
警告
This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.
警告
Please use Hashtable(IEqualityComparer) instead.
警告
This constructor has been deprecated. Use Hashtable(IEqualityComparer).
使用預設的初始容量與負載因子,以及指定的雜湊碼提供者與比較器,初始化一個新的空實例 Hashtable 。
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);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")]
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("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<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
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (hcp As IHashCodeProvider, comparer As IComparer)
參數
- 屬性
備註
雜湊表的容量用來計算根據負載因子的最佳雜湊表桶數。 容量會根據需要自動增加。
載重因數是元件與鏟斗的最大比值。 較低的負載因子意味著查詢速度更快,但代價是記憶體消耗增加。
當實際負載因子達到指定負載因子時,桶數會自動增加到最小且大於當前桶數兩倍的質數。
雜湊碼提供者會為物件中的 Hashtable 鍵分配雜湊碼。 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
自訂雜湊碼提供者與自訂比較器可實現使用大小寫不區分字串的查詢等情境。
這個建構子是一個 O(1) 運算。
另請參閱
適用於
Hashtable(IDictionary, Single)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- 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 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.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 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 物件包含雜湊碼提供者與比較器。 若建構子中使用 aIEqualityComparer,物件中用作鍵的Hashtable物件不必覆蓋 Hashtable and Object.GetHashCodeObject.Equals 方法。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
這 IEqualityComparer 讓使用大小寫不區分字串的查找等情境成為可能。
新物件 Hashtable 的元素排序順序與列舉器遍歷 IDictionary 物件的順序相同。
此構造子是一個 O(n) 運算,其中 n 是參數中的 d 元素數。
另請參閱
適用於
Hashtable(Int32)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- 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 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.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 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 物件包含雜湊碼提供者與比較器。 若建構子中使用 aIEqualityComparer,物件中用作鍵的Hashtable物件不必覆蓋 Hashtable and Object.GetHashCodeObject.Equals 方法。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
這 IEqualityComparer 讓使用大小寫不區分字串的查找等情境成為可能。
這個建構子是一個 O(1) 運算。
另請參閱
適用於
Hashtable(IDictionary)
- 來源:
- Hashtable.cs
- 來源:
- Hashtable.cs
- 來源:
- 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 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.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 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 物件包含雜湊碼提供者與比較器。 若建構子中使用 aIEqualityComparer,作為鍵的Hashtable物件不必覆蓋 Hashtable and Object.GetHashCodeObject.Equals 方法。
雜湊碼提供者會為 Hashtable. 預設的雜湊碼提供者是金鑰的 Object.GetHashCode實作。
比較器判斷兩個鍵是否相等。 Hashtable 中的每個鍵必須是唯一的。 預設比較器是該鍵對 Object.Equals的實作。
這 IEqualityComparer 讓使用大小寫不區分字串的查找等情境成為可能。
此構造子是一個 O(n) 運算,其中 n 為 capacity 參數。