Hashtable 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 Hashtable 类的新实例。
重载
Hashtable()
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
使用默认的初始容量、负载因子、哈希代码提供程序和比较器初始化类的新空实例 Hashtable 。
public:
Hashtable();
public Hashtable();
Public Sub New ()
示例
下面的代码示例使用不同的 Hashtable 构造函数创建哈希表,并演示哈希表行为的差异,即使每个哈希表都包含相同的元素也是如此。
using System;
using System.Collections;
using System.Globalization;
class myComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
var myHT1 = new Hashtable();
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
var myHT2 = new Hashtable(new myComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable myHT3 = new Hashtable(
CaseInsensitiveHashCodeProvider.DefaultInvariant,
CaseInsensitiveComparer.DefaultInvariant);
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT4 = new Hashtable(new myCultureComparer(myCul));
myHT4.Add("FIRST", "Hello");
myHT4.Add("SECOND", "World");
myHT4.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myComparer
Implements IEqualityComparer
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return x.Equals(y)
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable()
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the default Object.Equals to determine equality.
Dim myHT2 As New Hashtable(New myComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using a case-insensitive hash code provider and
' case-insensitive comparer based on the InvariantCulture.
Dim myHT3 As New Hashtable( _
CaseInsensitiveHashCodeProvider.DefaultInvariant, _
CaseInsensitiveComparer.DefaultInvariant)
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
myHT4.Add("FIRST", "Hello")
myHT4.Add("SECOND", "World")
myHT4.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False
注解
哈希表的容量用于根据负载因子计算哈希表存储桶的最佳数量。 容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
哈希代码提供程序为对象中的 Hashtable 键分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
此构造函数是一个 O(1) 操作。
另请参阅
适用于
Hashtable(Int32, Single, IHashCodeProvider, IComparer)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
注意
This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.
注意
Please use Hashtable(int, float, IEqualityComparer) instead.
使用指定的初始容量、负载因子、哈希代码提供程序和比较器初始化类的新空实例 Hashtable 。
public:
Hashtable(int capacity, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable(int capacity, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable(int capacity, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable(int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable(int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)
参数
- loadFactor
- Single
范围为 0.1 到 1.0 的数字,乘以提供最佳性能的默认值。 结果是元素与存储桶的最大比率。
- 属性
例外
注解
指定初始容量无需在向对象添加元素 Hashtable 时执行大量调整大小操作。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。 负载系数为 1.0 是速度和大小之间的最佳平衡。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
哈希代码提供程序为键 Hashtable分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
自定义哈希代码提供程序和自定义比较器支持使用不区分大小写的字符串执行查找等方案。
此构造函数是一个 O(n) 操作,其中 n 是 capacity 参数。
另请参阅
适用于
Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
注意
This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.
注意
Please use Hashtable(IDictionary, float, IEqualityComparer) instead.
public:
Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable(System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable(System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable(System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable(System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)
参数
IDictionary要复制到新Hashtable对象的对象。
- loadFactor
- Single
范围为 0.1 到 1.0 的数字,乘以提供最佳性能的默认值。 结果是元素与存储桶的最大比率。
- 属性
例外
d 是 null。
注解
初始容量设置为源字典中的元素数。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。 负载系数为 1.0 是速度和大小之间的最佳平衡。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
哈希代码提供程序为对象中的 Hashtable 键分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
自定义哈希代码提供程序和自定义比较器支持使用不区分大小写的字符串执行查找等方案。
新 Hashtable 元素的排序顺序与枚举器循环 IDictionary 访问对象的顺序相同。
此构造函数是一个 O(n) 操作,其中 n 参数中的 d 元素数。
适用于
Hashtable(Int32, Single, IEqualityComparer)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- 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
定义
-或-
null 使用默认哈希代码提供程序和默认比较器。 默认哈希代码提供程序是每个键的实现 GetHashCode() ,默认比较器是每个键的实现 Equals(Object)。
例外
示例
下面的代码示例使用不同的 Hashtable 构造函数创建哈希表,并演示哈希表行为的差异,即使每个哈希表都包含相同的元素也是如此。
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3, .8f);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
注解
指定初始容量无需在向对象添加元素 Hashtable 时执行大量调整大小操作。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。 负载系数为 1.0 是速度和大小之间的最佳平衡。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
该 IEqualityComparer 对象包括哈希代码提供程序和比较器。 如果在构造函数中使用了 aIEqualityComparer,则不需要在构造函数中Hashtable用作键的对象来替代Hashtable和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
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
注意
This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.
注意
Please use Hashtable(int, IEqualityComparer) instead.
使用指定的初始容量、哈希代码提供程序、比较器和默认负载因子初始化类的新空实例 Hashtable 。
public:
Hashtable(int capacity, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable(int capacity, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable(int capacity, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable(int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable(int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, hcp As IHashCodeProvider, comparer As IComparer)
参数
- 属性
例外
capacity 小于零。
注解
指定初始容量无需在向对象添加元素 Hashtable 时执行大量调整大小操作。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
哈希代码提供程序为键 Hashtable分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
自定义哈希代码提供程序和自定义比较器支持使用不区分大小写的字符串执行查找等方案。
此构造函数是一个 O(n) 操作,其中 n 是 capacity 参数。
另请参阅
适用于
Hashtable(IDictionary, Single, IEqualityComparer)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- 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
定义
-或-
null 使用默认哈希代码提供程序和默认比较器。 默认哈希代码提供程序是每个键的实现 GetHashCode() ,默认比较器是每个键的实现 Equals(Object)。
例外
d 是 null。
示例
下面的代码示例使用不同的 Hashtable 构造函数创建哈希表,并演示哈希表行为的差异,即使每个哈希表都包含相同的元素也是如此。
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
SortedList mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(mySL, .8f);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(mySL, .8f,
new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
注解
初始容量设置为源字典中的元素数。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。 负载系数为 1.0 是速度和大小之间的最佳平衡。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
该 IEqualityComparer 对象包括哈希代码提供程序和比较器。 如果在构造函数中使用 aIEqualityComparer,则不需要在对象中Hashtable用作键的对象来替代Hashtable和Object.GetHashCodeObject.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
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
注意
This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.
注意
Please use Hashtable(IDictionary, IEqualityComparer) instead.
通过将元素从指定的字典复制到新对象来初始化类的新Hashtable实例Hashtable。 新 Hashtable 对象的初始容量等于复制的元素数,并使用默认加载因子,以及指定的哈希代码提供程序和比较器。 此 API 已过时。 有关替代方法,请参阅 Hashtable(IDictionary, IEqualityComparer)。
public:
Hashtable(System::Collections::IDictionary ^ d, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable(System.Collections.IDictionary d, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable(System.Collections.IDictionary d, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable(System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable(System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, hcp As IHashCodeProvider, comparer As IComparer)
参数
IDictionary要复制到新Hashtable对象的对象。
- 属性
例外
d 是 null。
注解
初始容量设置为源字典中的元素数。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
哈希代码提供程序为对象中的 Hashtable 键分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
自定义哈希代码提供程序和自定义比较器支持使用不区分大小写的字符串执行查找等方案。
新 Hashtable 元素的排序顺序与枚举器循环 IDictionary 访问对象的顺序相同。
此构造函数是一个 O(n) 操作,其中 n 参数中的 d 元素数。
另请参阅
适用于
Hashtable(Int32, Single)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- 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 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
- 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);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected Hashtable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
protected Hashtable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
Protected Sub New (info As SerializationInfo, context As StreamingContext)
参数
- info
- SerializationInfo
包含 SerializationInfo 序列化 Hashtable 对象所需的信息的对象。
- context
- StreamingContext
一个对象,包含与 <
- 属性
例外
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
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
注意
This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.
注意
Please use Hashtable(IEqualityComparer) instead.
注意
This constructor has been deprecated. Use Hashtable(IEqualityComparer).
使用默认的初始容量和负载因子以及指定的哈希代码提供程序和比较器初始化类的新空实例 Hashtable 。
public:
Hashtable(System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")]
public Hashtable(System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable(System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")]
public Hashtable(System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable(System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable(System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (hcp As IHashCodeProvider, comparer As IComparer)
参数
- 属性
注解
哈希表的容量用于根据负载因子计算哈希表存储桶的最佳数量。 容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
哈希代码提供程序为对象中的 Hashtable 键分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
自定义哈希代码提供程序和自定义比较器支持使用不区分大小写的字符串执行查找等方案。
此构造函数是一个 O(1) 操作。
另请参阅
适用于
Hashtable(IDictionary, Single)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- 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 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
- 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
定义
-或-
null 使用默认哈希代码提供程序和默认比较器。 默认哈希代码提供程序是每个键的实现 GetHashCode() ,默认比较器是每个键的实现 Equals(Object)。
例外
d 是 null。
示例
下面的代码示例使用不同的 Hashtable 构造函数创建哈希表,并演示哈希表行为的差异,即使每个哈希表都包含相同的元素也是如此。
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
var mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
var myHT1 = new Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
var myHT2 = new Hashtable(mySL, new myCultureComparer());
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL)
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
注解
初始容量设置为源字典中的元素数。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
该 IEqualityComparer 对象包括哈希代码提供程序和比较器。 如果在构造函数中使用 aIEqualityComparer,则不需要在对象中Hashtable用作键的对象来替代Hashtable和Object.GetHashCodeObject.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
- 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 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
- 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 System;
using System.Collections;
using System.Globalization;
class myComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
var myHT1 = new Hashtable();
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the default Object.Equals to determine equality.
var myHT2 = new Hashtable(new myComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using a case-insensitive hash code provider and
// case-insensitive comparer based on the InvariantCulture.
Hashtable myHT3 = new Hashtable(
CaseInsensitiveHashCodeProvider.DefaultInvariant,
CaseInsensitiveComparer.DefaultInvariant);
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
var myCul = new CultureInfo("tr-TR");
var myHT4 = new Hashtable(new myCultureComparer(myCul));
myHT4.Add("FIRST", "Hello");
myHT4.Add("SECOND", "World");
myHT4.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myComparer
Implements IEqualityComparer
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return x.Equals(y)
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable()
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the default Object.Equals to determine equality.
Dim myHT2 As New Hashtable(New myComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using a case-insensitive hash code provider and
' case-insensitive comparer based on the InvariantCulture.
Dim myHT3 As New Hashtable( _
CaseInsensitiveHashCodeProvider.DefaultInvariant, _
CaseInsensitiveComparer.DefaultInvariant)
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
myHT4.Add("FIRST", "Hello")
myHT4.Add("SECOND", "World")
myHT4.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False
注解
哈希表的容量用于根据负载因子计算哈希表存储桶的最佳数量。 容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
该 IEqualityComparer 对象包括哈希代码提供程序和比较器。 如果在构造函数中使用 aIEqualityComparer,则不需要在对象中Hashtable用作键的对象来替代Hashtable和Object.GetHashCodeObject.Equals方法。
哈希代码提供程序为键 Hashtable分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
启用 IEqualityComparer 方案,例如使用不区分大小写的字符串执行查找。
此构造函数是一个 O(1) 操作。
另请参阅
适用于
Hashtable(IDictionary)
- Source:
- Hashtable.cs
- Source:
- Hashtable.cs
- 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 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
- 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
定义
-或-
null 使用默认哈希代码提供程序和默认比较器。 默认哈希代码提供程序是每个键的实现 GetHashCode() ,默认比较器是每个键的实现 Equals(Object)。
例外
capacity 小于零。
示例
下面的代码示例使用不同的 Hashtable 构造函数创建哈希表,并演示哈希表行为的差异,即使每个哈希表都包含相同的元素也是如此。
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3);
myHT1.Add("FIRST", "Hello");
myHT1.Add("SECOND", "World");
myHT1.Add("THIRD", "!");
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
myHT2.Add("FIRST", "Hello");
myHT2.Add("SECOND", "World");
myHT2.Add("THIRD", "!");
// Create a hash table using an IEqualityComparer that is based on
// the Turkish culture (tr-TR) where "I" is not the uppercase
// version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
myHT3.Add("FIRST", "Hello");
myHT3.Add("SECOND", "World");
myHT3.Add("THIRD", "!");
// Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
first is in myHT1: False
first is in myHT2: True
first is in myHT3: False
*/
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3)
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False
注解
指定初始容量无需在向对象添加元素 Hashtable 时执行大量调整大小操作。 根据负载系数,容量会根据需要自动增加。
负载因子是元素与存储桶的最大比率。 较小的负载因子意味着以增加内存消耗为代价更快地查找。
当实际负载因子达到指定的负载因子时,存储桶数会自动增加到大于当前存储桶数两倍的最小质数。
该 IEqualityComparer 对象包括哈希代码提供程序和比较器。 如果在构造函数中使用了 aIEqualityComparer,则不需要在构造函数中Hashtable用作键的对象来替代Hashtable和Object.GetHashCode方法。Object.Equals
哈希代码提供程序为键 Hashtable分配哈希代码。 默认哈希代码提供程序是密钥的 Object.GetHashCode实现。
比较器确定两个键是否相等。 每个密钥在 Hashtable 中必须是唯一的。 默认比较器是密钥的 Object.Equals实现。
启用 IEqualityComparer 方案,例如使用不区分大小写的字符串执行查找。
此构造函数是一个 O(n) 操作,其中 n 是 capacity 参数。