Hashtable 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 Hashtable 类的新实例。
重载
Hashtable()
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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() 实现。
- 属性
例外
示例
下面的代码示例使用不同的 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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() 实现。
- 属性
例外
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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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() 实现。
- 属性
例外
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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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() 实现。
- 属性
例外
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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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() 实现。
- 属性
示例
下面的代码示例使用不同的 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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如果在构造函数中使用 Hashtable ,则不需要在 对象中Hashtable用作键的对象来替代 Object.GetHashCode 和 Object.Equals 方法。
哈希代码提供程序为 中的 Hashtable键分配哈希代码。 默认哈希代码提供程序是 密钥的实现 Object.GetHashCode。
比较器确定两个键是否相等。 中的每个键都必须是唯一的 Hashtable 。 默认比较器是 键的实现 Object.Equals。
支持 IEqualityComparer 使用不区分大小写的字符串执行查找等方案。
新 Hashtable 元素的排序顺序与枚举器循环访问 IDictionary 对象的顺序相同。
此构造函数是一个 O(n)
操作,其中 n
是 参数中的 d
元素数。
另请参阅
适用于
Hashtable(Int32)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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如果在构造函数中使用 Hashtable ,则不需要在 对象中Hashtable用作键的对象来替代 Object.GetHashCode 和 Object.Equals 方法。
哈希代码提供程序为 中的 Hashtable键分配哈希代码。 默认哈希代码提供程序是 密钥的实现 Object.GetHashCode。
比较器确定两个键是否相等。 中的每个键都必须是唯一的 Hashtable 。 默认比较器是 键的实现 Object.Equals。
支持 IEqualityComparer 使用不区分大小写的字符串执行查找等方案。
此构造函数是一个 O(1)
操作。
另请参阅
适用于
Hashtable(IDictionary)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- 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
参数。