Hashtable コンストラクタ (Int32, Single)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim capacity As Integer Dim loadFactor As Single Dim instance As New Hashtable(capacity, loadFactor)


初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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, (float).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. Hashtable myHT2 = new Hashtable(3, (float).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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(3, (float).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")); } } /* 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (IDictionary, IHashCodeProvider, IComparer)
メモ : このコンストラクタは、互換性のために残されています。
指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、既定のテーブル占有率、および指定したハッシュ コード プロバイダと比較演算子を使用します。 名前空間: System.Collectionsアセンブリ: mscorlib (mscorlib.dll 内)

<ObsoleteAttribute("Please use Hashtable(IDictionary, IEqualityComparer) instead.")> _ Public Sub New ( _ d As IDictionary, _ hcp As IHashCodeProvider, _ comparer As IComparer _ )
Dim d As IDictionary Dim hcp As IHashCodeProvider Dim comparer As IComparer Dim instance As New Hashtable(d, hcp, comparer)
[ObsoleteAttribute("Please use Hashtable(IDictionary, IEqualityComparer) instead.")] public Hashtable ( IDictionary d, IHashCodeProvider hcp, IComparer comparer )
[ObsoleteAttribute(L"Please use Hashtable(IDictionary, IEqualityComparer) instead.")] public: Hashtable ( IDictionary^ d, IHashCodeProvider^ hcp, IComparer^ comparer )
/** @attribute ObsoleteAttribute("Please use Hashtable(IDictionary, IEqualityComparer) instead.") */ public Hashtable ( IDictionary d, IHashCodeProvider hcp, IComparer comparer )
ObsoleteAttribute("Please use Hashtable(IDictionary, IEqualityComparer) instead.") public function Hashtable ( d : IDictionary, hcp : IHashCodeProvider, comparer : IComparer )


初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。
新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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) ' 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: {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 'Main End Class 'SamplesHashtable '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); // Create a hash table using the specified IEqualityComparer that uses // the CaseInsensitiveComparer.DefaultInvariant to determine equality. Hashtable 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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(mySL, 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 */
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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

サポート対象 : 1.0、1.1
2.0 では、互換性のために残されています (コンパイル時に警告)
.NET Compact Framework
サポート対象 : 1.0
2.0 では、互換性のために残されています (コンパイル時に警告)

Hashtable コンストラクタ (IDictionary, Single, IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ d As IDictionary, _ loadFactor As Single, _ equalityComparer As IEqualityComparer _ )
Dim d As IDictionary Dim loadFactor As Single Dim equalityComparer As IEqualityComparer Dim instance As New Hashtable(d, loadFactor, equalityComparer)
public Hashtable ( IDictionary d, float loadFactor, IEqualityComparer equalityComparer )
public: Hashtable ( IDictionary^ d, float loadFactor, IEqualityComparer^ equalityComparer )
public Hashtable ( IDictionary d, float loadFactor, IEqualityComparer equalityComparer )
public function Hashtable ( d : IDictionary, loadFactor : float, equalityComparer : IEqualityComparer )

例外の種類 | 条件 |
---|---|
ArgumentNullException | d が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | loadFactor が 0.1 未満です。 または loadFactor が 1.0 より大きい値です。 |

初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable オブジェクトでキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。
新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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, (float).8); // Create a hash table using the specified IEqualityComparer that uses // the CaseInsensitiveComparer.DefaultInvariant to determine equality. Hashtable myHT2 = new Hashtable(mySL, (float).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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(mySL, (float).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")); } } /* 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (Int32, Single, IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ capacity As Integer, _ loadFactor As Single, _ equalityComparer As IEqualityComparer _ )
Dim capacity As Integer Dim loadFactor As Single Dim equalityComparer As IEqualityComparer Dim instance As New Hashtable(capacity, loadFactor, equalityComparer)
public function Hashtable ( capacity : int, loadFactor : float, equalityComparer : IEqualityComparer )


初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable でキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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, (float).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. Hashtable myHT2 = new Hashtable(3, (float).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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(3, (float).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")); } } /* 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (IDictionary, Single, IHashCodeProvider, IComparer)
メモ : このコンストラクタは、互換性のために残されています。
指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、指定したテーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用します。 名前空間: System.Collectionsアセンブリ: mscorlib (mscorlib.dll 内)

<ObsoleteAttribute("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")> _ Public Sub New ( _ d As IDictionary, _ loadFactor As Single, _ hcp As IHashCodeProvider, _ comparer As IComparer _ )
Dim d As IDictionary Dim loadFactor As Single Dim hcp As IHashCodeProvider Dim comparer As IComparer Dim instance As New Hashtable(d, loadFactor, hcp, comparer)
[ObsoleteAttribute("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")] public Hashtable ( IDictionary d, float loadFactor, IHashCodeProvider hcp, IComparer comparer )
[ObsoleteAttribute(L"Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")] public: Hashtable ( IDictionary^ d, float loadFactor, IHashCodeProvider^ hcp, IComparer^ comparer )
/** @attribute ObsoleteAttribute("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.") */ public Hashtable ( IDictionary d, float loadFactor, IHashCodeProvider hcp, IComparer comparer )
ObsoleteAttribute("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.") public function Hashtable ( d : IDictionary, loadFactor : float, hcp : IHashCodeProvider, comparer : IComparer )

例外の種類 | 条件 |
---|---|
ArgumentNullException | d が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | loadFactor が 0.1 未満です。 または loadFactor が 1.0 より大きい値です。 |

初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。
新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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, (float).8); // Create a hash table using the specified IEqualityComparer that uses // the CaseInsensitiveComparer.DefaultInvariant to determine equality. Hashtable myHT2 = new Hashtable(mySL, (float).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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(mySL, (float).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")); } } /* 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ


Hashtable コンストラクタ (IDictionary, Single)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ d As IDictionary, _ loadFactor As Single _ )
Dim d As IDictionary Dim loadFactor As Single Dim instance As New Hashtable(d, loadFactor)
public Hashtable ( IDictionary d, float loadFactor )
public: Hashtable ( IDictionary^ d, float loadFactor )
public Hashtable ( IDictionary d, float loadFactor )

例外の種類 | 条件 |
---|---|
ArgumentNullException | d が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | loadFactor が 0.1 未満です。 または loadFactor が 1.0 より大きい値です。 |

初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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, (float).8); // Create a hash table using the specified IEqualityComparer that uses // the CaseInsensitiveComparer.DefaultInvariant to determine equality. Hashtable myHT2 = new Hashtable(mySL, (float).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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(mySL, (float).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")); } } /* 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (Int32, IHashCodeProvider, IComparer)
メモ : このコンストラクタは、互換性のために残されています。
指定した初期量、ハッシュ コード プロバイダ、および比較演算子と、既定のテーブル占有率を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 名前空間: System.Collectionsアセンブリ: mscorlib (mscorlib.dll 内)

<ObsoleteAttribute("Please use Hashtable(int, IEqualityComparer) instead.")> _ Public Sub New ( _ capacity As Integer, _ hcp As IHashCodeProvider, _ comparer As IComparer _ )
Dim capacity As Integer Dim hcp As IHashCodeProvider Dim comparer As IComparer Dim instance As New Hashtable(capacity, hcp, comparer)
[ObsoleteAttribute("Please use Hashtable(int, IEqualityComparer) instead.")] public Hashtable ( int capacity, IHashCodeProvider hcp, IComparer comparer )
[ObsoleteAttribute(L"Please use Hashtable(int, IEqualityComparer) instead.")] public: Hashtable ( int capacity, IHashCodeProvider^ hcp, IComparer^ comparer )
/** @attribute ObsoleteAttribute("Please use Hashtable(int, IEqualityComparer) instead.") */ public Hashtable ( int capacity, IHashCodeProvider hcp, IComparer comparer )
ObsoleteAttribute("Please use Hashtable(int, IEqualityComparer) instead.") public function Hashtable ( capacity : int, hcp : IHashCodeProvider, comparer : IComparer )


初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

サポート対象 : 1.0、1.1
2.0 では、互換性のために残されています (コンパイル時に警告)
.NET Compact Framework
サポート対象 : 1.0
2.0 では、互換性のために残されています (コンパイル時に警告)

Hashtable コンストラクタ (IHashCodeProvider, IComparer)
メモ : このコンストラクタは、互換性のために残されています。
既定の初期量とテーブル占有率、および指定したハッシュ コード プロバイダと比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 名前空間: System.Collectionsアセンブリ: mscorlib (mscorlib.dll 内)

<ObsoleteAttribute("Please use Hashtable(IEqualityComparer) instead.")> _ Public Sub New ( _ hcp As IHashCodeProvider, _ comparer As IComparer _ )
[ObsoleteAttribute("Please use Hashtable(IEqualityComparer) instead.")] public Hashtable ( IHashCodeProvider hcp, IComparer comparer )
[ObsoleteAttribute(L"Please use Hashtable(IEqualityComparer) instead.")] public: Hashtable ( IHashCodeProvider^ hcp, IComparer^ comparer )
/** @attribute ObsoleteAttribute("Please use Hashtable(IEqualityComparer) instead.") */ public Hashtable ( IHashCodeProvider hcp, IComparer comparer )
ObsoleteAttribute("Please use Hashtable(IEqualityComparer) instead.") public function Hashtable ( hcp : IHashCodeProvider, comparer : IComparer )

ハッシュ テーブルの容量は、テーブル占有率に基づいてハッシュ テーブル バケットの最適数を計算するために使用されます。容量は必要に応じて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 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() 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: {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")) 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
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) { if (myComparer.Compare(x, y) == 0) { return true; } else { return false; } } 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. Hashtable 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. Hashtable 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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable 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: {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 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

サポート対象 : 1.0、1.1
2.0 では、互換性のために残されています (コンパイル時に警告)
.NET Compact Framework
サポート対象 : 1.0
2.0 では、互換性のために残されています (コンパイル時に警告)

Hashtable コンストラクタ (Int32, Single, IHashCodeProvider, IComparer)
メモ : このコンストラクタは、互換性のために残されています。
指定した初期量、テーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 名前空間: System.Collectionsアセンブリ: mscorlib (mscorlib.dll 内)

<ObsoleteAttribute("Please use Hashtable(int, float, IEqualityComparer) instead.")> _ Public Sub New ( _ capacity As Integer, _ loadFactor As Single, _ hcp As IHashCodeProvider, _ comparer As IComparer _ )
Dim capacity As Integer Dim loadFactor As Single Dim hcp As IHashCodeProvider Dim comparer As IComparer Dim instance As New Hashtable(capacity, loadFactor, hcp, comparer)
[ObsoleteAttribute("Please use Hashtable(int, float, IEqualityComparer) instead.")] public Hashtable ( int capacity, float loadFactor, IHashCodeProvider hcp, IComparer comparer )
[ObsoleteAttribute(L"Please use Hashtable(int, float, IEqualityComparer) instead.")] public: Hashtable ( int capacity, float loadFactor, IHashCodeProvider^ hcp, IComparer^ comparer )
/** @attribute ObsoleteAttribute("Please use Hashtable(int, float, IEqualityComparer) instead.") */ public Hashtable ( int capacity, float loadFactor, IHashCodeProvider hcp, IComparer comparer )
ObsoleteAttribute("Please use Hashtable(int, float, IEqualityComparer) instead.") public function Hashtable ( capacity : int, loadFactor : float, hcp : IHashCodeProvider, comparer : IComparer )


初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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, (float).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. Hashtable myHT2 = new Hashtable(3, (float).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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(3, (float).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")); } } /* 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (SerializationInfo, StreamingContext)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim info As SerializationInfo Dim context As StreamingContext Dim instance As New Hashtable(info, context)


ハッシュ テーブルの容量は、テーブル占有率に基づいてハッシュ テーブル バケットの最適数を計算するために使用されます。容量は必要に応じて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
このコンストラクタは O(n) 操作です。ここで、n は Count です。
Hashtable の列挙子をシリアル化または逆シリアル化すると要素の順番が変更される場合があるため、列挙を続けるためには Reset メソッドを呼び出す必要があります。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (Int32)
アセンブリ: mscorlib (mscorlib.dll 内)



初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (IDictionary)
アセンブリ: mscorlib (mscorlib.dll 内)



初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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) ' 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: {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 'Main End Class 'SamplesHashtable '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); // Create a hash table using the specified IEqualityComparer that uses // the CaseInsensitiveComparer.DefaultInvariant to determine equality. Hashtable 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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(mySL, 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 */
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 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)


ハッシュ テーブルの容量は、テーブル占有率に基づいてハッシュ テーブル バケットの最適数を計算するために使用されます。容量は必要に応じて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable オブジェクトでキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 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() 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: {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")) 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
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) { if (myComparer.Compare(x, y) == 0) { return true; } else { return false; } } 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. Hashtable 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. Hashtable 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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable 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: {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 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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (IDictionary, IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim d As IDictionary Dim equalityComparer As IEqualityComparer Dim instance As New Hashtable(d, equalityComparer)


初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable オブジェクトでキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。
新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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) ' 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: {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 'Main End Class 'SamplesHashtable '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); // Create a hash table using the specified IEqualityComparer that uses // the CaseInsensitiveComparer.DefaultInvariant to determine equality. Hashtable 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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable myHT3 = new Hashtable(mySL, 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 */
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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ (Int32, IEqualityComparer)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim capacity As Integer Dim equalityComparer As IEqualityComparer Dim instance As New Hashtable(capacity, equalityComparer)


初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable でキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。
ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。
IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 'Main End Class 'SamplesHashtable '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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Hashtable コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)


ハッシュ テーブルの容量は、テーブル占有率に基づいてハッシュ テーブル バケットの最適数を計算するために使用されます。容量は必要に応じて自動的に増加します。
テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。
実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。
比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

異なる Hashtable コンストラクタを使用してハッシュ テーブルを作成し、各ハッシュ テーブルの動作の違いを示すコード例を次に示します。同じ要素が格納されていたとしても、動作が異なることがわかります。
Imports System 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 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() 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: {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")) 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
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) { if (myComparer.Compare(x, y) == 0) { return true; } else { return false; } } 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. Hashtable 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. Hashtable 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". CultureInfo myCul = new CultureInfo("tr-TR"); Hashtable 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: {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 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 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Hashtable コンストラクタのページへのリンク