ハッシュ‐テーブル【hash table】
読み方:はっしゅてーぶる
元の数値や文字列とハッシュ関数によって得られたハッシュ値を、一意的に結びつけて格納したデータ構造。データの規模が特に大きな場合、個々の要素をハッシュ値と結びつけることで、検索・挿入・削除などを高速化することができる。ハッシュ表。
ハッシュ‐ひょう〔‐ヘウ〕【ハッシュ表】
読み方:はっしゅひょう
《hash table》⇒ハッシュテーブル
Hashtable クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class Hashtable Implements IDictionary, ICollection, IEnumerable, ISerializable, _ IDeserializationCallback, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable

各要素は、DictionaryEntry オブジェクトに格納されているキー/値ペアです。キーには null 参照 (Visual Basic では Nothing) は使用できませんが、値は null でもかまいません。
Hashtable でキーとして使用されるオブジェクトは、Object.GetHashCode メソッド (または IHashCodeProvider インターフェイス) および Object.Equals メソッド (または IComparer インターフェイス) をオーバーライドする必要があります。メソッドおよびインターフェイスのどちらの実装でも、大文字と小文字の区別を同じ方法で処理する必要があります。そのように実装されていないと、Hashtable が正しく動作しない場合があります。たとえば、Hashtable を作成するとき、CaseInsensitiveHashCodeProvider クラス (または大文字と小文字を区別しない任意の IHashCodeProvider 実装) は、CaseInsensitiveComparer クラス (または大文字と小文字を区別しない任意の IComparer 実装) と共に使用する必要があります。
また、これらのメソッドは、キーが Hashtable に存在している間に同じパラメータを指定して呼び出された場合は、同じ結果を生成する必要があります。代わりに、Hashtable コンストラクタで IEqualityComparer パラメータを使用することもできます。キーが等値で単純に参照が等値である場合は、Object.GetHashCode および Object.Equals を継承して実装するだけで十分です。
キー オブジェクトは、Hashtable でキーとして使用されている間は不変であることが必要です。
Hashtable に要素が追加されると、その要素はキーのハッシュ コードに基づいてバケットに配置されます。それ以後にキーを検索する際には、そのキーのハッシュ コードを使用して、1 つのバケットだけを検索します。これにより、要素を見つけるために必要となるキーの比較回数を減らすことができます。
Hashtable のテーブル占有率は、バケット数に対する要素数の最大比率を決定します。テーブル占有率を小さくすると、検索時間は平均的に短くなりますが、メモリの消費量は増加します。既定のテーブル占有率は 1.0 です。通常、この値は、検索速度とサイズの最適なバランスを実現します。Hashtable を作成するときに、既定値とは異なるテーブル占有率を指定することもできます。
Hashtable に要素が追加されると、Hashtable の実際のテーブル占有率は大きくなります。実際のテーブル占有率が指定した占有率に達すると、Hashtable のバケット数は、現在の Hashtable のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。
Hashtable の各キー オブジェクトは、GetHash を呼び出すことによってアクセスできる独自のハッシュ関数を用意する必要があります。ただし、IHashCodeProvider を実装するオブジェクトは Hashtable コンストラクタに渡すことができ、そのハッシュ関数はテーブル内のすべてのオブジェクトに対して使用できます。
Hashtable の容量は、Hashtable が保持できる要素数になります。Hashtable の既定の初期量はゼロです。Hashtable に要素を追加すると、必要に応じて、再割り当てを行うことによって容量が自動的に増加します。
C# 言語の foreach ステートメント (Visual Basic では for each) は、コレクション内の各要素の型を必要とします。Hashtable の各要素はキー/値ペアであるため、要素の型は、キーの型や値の型にはなりません。その代わり、要素の型は DictionaryEntry になります。例 :
foreach (DictionaryEntry de in myHashtable) {...}
For Each de as DictionaryEntry In myHashtable ... Next de
foreach ステートメントは、列挙子のラッパーです。これは、コレクションからの読み取りだけを許可し、コレクションへの書き込みを防ぎます。
Hashtable の列挙子をシリアル化または逆シリアル化すると要素の順番が変更される場合があるため、列挙を続けるためには Reset メソッドを呼び出す必要があります。

Hashtable を作成および初期化し、それに対して各種の関数を実行する方法と、そのキーと値を出力する方法の例を次に示します。
Imports System Imports System.Collections Module Example Sub Main() ' Create a new hash table. ' Dim openWith As New Hashtable() ' Add some elements to the hash table. There are no ' duplicate keys, but some of the values are duplicates. openWith.Add("txt", "notepad.exe") openWith.Add("bmp", "paint.exe") openWith.Add("dib", "paint.exe") openWith.Add("rtf", "wordpad.exe") ' The Add method throws an exception if the new key is ' already in the hash table. Try openWith.Add("txt", "winword.exe") Catch Console.WriteLine("An element with Key = ""txt"" already exists.") End Try ' The Item property is the default property, so you ' can omit its name when accessing elements. Console.WriteLine("For key = ""rtf"", value = {0}.", _ openWith("rtf")) ' The default Item property can be used to change the value ' associated with a key. openWith("rtf") = "winword.exe" Console.WriteLine("For key = ""rtf"", value = {0}.", _ openWith("rtf")) ' If a key does not exist, setting the default Item property ' for that key adds a new key/value pair. openWith("doc") = "winword.exe" ' The default Item property throws an exception if the requested ' key is not in the hash table. Try Console.WriteLine("For key = ""tif"", value = {0}.", _ openWith("tif")) Catch Console.WriteLine("Key = ""tif"" is not found.") End Try ' ContainsKey can be used to test keys before inserting ' them. If Not openWith.ContainsKey("ht") Then openWith.Add("ht", "hypertrm.exe") Console.WriteLine("Value added for key = ""ht"": {0}", _ openWith("ht")) End If ' When you use foreach to enumerate hash table elements, ' the elements are retrieved as KeyValuePair objects. Console.WriteLine() For Each de As DictionaryEntry In openWith Console.WriteLine("Key = {0}, Value = {1}", _ de.Key, de.Value) Next de ' To get the values alone, use the Values property. Dim valueColl As ICollection = openWith.Values ' The elements of the ValueCollection are strongly typed ' with the type that was specified for hash table values. Console.WriteLine() For Each s As String In valueColl Console.WriteLine("Value = {0}", s) Next s ' To get the keys alone, use the Keys property. Dim keyColl As ICollection = openWith.Keys ' The elements of the KeyCollection are strongly typed ' with the type that was specified for hash table keys. Console.WriteLine() For Each s As String In keyColl Console.WriteLine("Key = {0}", s) Next s ' Use the Remove method to remove a key/value pair. Console.WriteLine(vbLf + "Remove(""doc"")") openWith.Remove("doc") If Not openWith.ContainsKey("doc") Then Console.WriteLine("Key ""doc"" is not found.") End If End Sub End Module ' This code example produces the following output: ' 'An element with Key = "txt" already exists. 'For key = "rtf", value = wordpad.exe. 'For key = "rtf", value = winword.exe. 'For key = "tif", value = . 'Value added for key = "ht": hypertrm.exe ' 'Key = dib, Value = paint.exe 'Key = txt, Value = notepad.exe 'Key = ht, Value = hypertrm.exe 'Key = bmp, Value = paint.exe 'Key = rtf, Value = winword.exe 'Key = doc, Value = winword.exe ' 'Value = paint.exe 'Value = notepad.exe 'Value = hypertrm.exe 'Value = paint.exe 'Value = winword.exe 'Value = winword.exe ' 'Key = dib 'Key = txt 'Key = ht 'Key = bmp 'Key = rtf 'Key = doc ' 'Remove("doc") 'Key "doc" is not found.
using System; using System.Collections; class Example { public static void Main() { // Create a new hash table. // Hashtable openWith = new Hashtable(); // Add some elements to the hash table. There are no // duplicate keys, but some of the values are duplicates. openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); // The Add method throws an exception if the new key is // already in the hash table. try { openWith.Add("txt", "winword.exe"); } catch { Console.WriteLine("An element with Key = \"txt\" already exists."); } // The Item property is the default property, so you // can omit its name when accessing elements. Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // The default Item property can be used to change the value // associated with a key. openWith["rtf"] = "winword.exe"; Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // If a key does not exist, setting the default Item property // for that key adds a new key/value pair. openWith["doc"] = "winword.exe"; // The default Item property throws an exception if the requested // key is not in the hash table. try { Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); } catch { Console.WriteLine("Key = \"tif\" is not found."); } // ContainsKey can be used to test keys before inserting // them. if (!openWith.ContainsKey("ht")) { openWith.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]); } // When you use foreach to enumerate hash table elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); foreach( DictionaryEntry de in openWith ) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); } // To get the values alone, use the Values property. ICollection valueColl = openWith.Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for hash table values. Console.WriteLine(); foreach( string s in valueColl ) { Console.WriteLine("Value = {0}", s); } // To get the keys alone, use the Keys property. ICollection keyColl = openWith.Keys; // The elements of the KeyCollection are strongly typed // with the type that was specified for hash table keys. Console.WriteLine(); foreach( string s in keyColl ) { Console.WriteLine("Key = {0}", s); } // Use the Remove method to remove a key/value pair. Console.WriteLine("\nRemove(\"doc\")"); openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); } } } /* This code example produces the following output: An element with Key = "txt" already exists. For key = "rtf", value = wordpad.exe. For key = "rtf", value = winword.exe. For key = "tif", value = . Value added for key = "ht": hypertrm.exe Key = dib, Value = paint.exe Key = txt, Value = notepad.exe Key = ht, Value = hypertrm.exe Key = bmp, Value = paint.exe Key = rtf, Value = winword.exe Key = doc, Value = winword.exe Value = paint.exe Value = notepad.exe Value = hypertrm.exe Value = paint.exe Value = winword.exe Value = winword.exe Key = dib Key = txt Key = ht Key = bmp Key = rtf Key = doc Remove("doc") Key "doc" is not found. */

System.Collections.Hashtable
System.Configuration.SettingsAttributeDictionary
System.Configuration.SettingsContext
System.Data.PropertyCollection

Hashtable は、複数の読み取りスレッドまたは 1 つの書き込みスレッドで使用される場合はスレッド セーフです。複数のスレッドで使用される場合、いずれかのスレッドが書き込み (更新) 操作を実行すると、スレッド セーフでなくなります。複数の書き込みスレッドをサポートするには、Hashtable オブジェクトを読み取るスレッドが存在しないことと、Hashtable に対するすべての操作を Synchronized メソッドで返されるラッパーを経由して実行することが必要です。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

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 メンバ
System.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer
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 コンストラクタ (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 クラス
Hashtable メンバ
System.Collections 名前空間
IDictionary
IHashCodeProvider
IComparer
Object.GetHashCode
Object.Equals
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 () | 既定の初期量、テーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IDictionary) | 指定したディレクトリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、既定のテーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IEqualityComparer) | 既定の初期量とテーブル占有率、および指定した IEqualityComparer オブジェクトを使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (Int32) | 指定した初期量と、既定のテーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IDictionary, IEqualityComparer) | 指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、既定のテーブル占有率および指定した IEqualityComparer オブジェクトを使用します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IDictionary, Single) | 指定したディレクトリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、指定したテーブル占有率、および既定のハッシュ コード プロバイダと比較演算子を使用します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IHashCodeProvider, IComparer) | 既定の初期量とテーブル占有率、および指定したハッシュ コード プロバイダと比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (Int32, IEqualityComparer) | 指定した初期量と IEqualityComparer、および既定のテーブル占有率を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (Int32, Single) | 指定した初期量とテーブル占有率、および既定のハッシュ コード プロバイダと比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (SerializationInfo, StreamingContext) | 指定した SerializationInfo オブジェクトと StreamingContext オブジェクトを使用してシリアル化できる、Hashtable クラスの新しい空のインスタンスを初期化します。 |
Hashtable (IDictionary, IHashCodeProvider, IComparer) | 指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、既定のテーブル占有率、および指定したハッシュ コード プロバイダと比較演算子を使用します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IDictionary, Single, IEqualityComparer) | 指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、指定したテーブル占有率および IEqualityComparer オブジェクトを使用します。 .NET Compact Framework によってサポートされています。 |
Hashtable (Int32, IHashCodeProvider, IComparer) | 指定した初期量、ハッシュ コード プロバイダ、および比較演算子と、既定のテーブル占有率を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (Int32, Single, IEqualityComparer) | 指定した初期量、テーブル占有率、および IEqualityComparer オブジェクトを使用して、Hashtable クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Hashtable (IDictionary, Single, IHashCodeProvider, IComparer) | 指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、指定したテーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用します。 |
Hashtable (Int32, Single, IHashCodeProvider, IComparer) | 指定した初期量、テーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用して、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 )
public function Hashtable ( d : IDictionary, loadFactor : float )

例外の種類 | 条件 |
---|---|
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 クラス
Hashtable メンバ
System.Collections 名前空間
IHashCodeProvider
IComparer
Object.GetHashCode
Object.Equals
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 クラス
Hashtable メンバ
System.Collections 名前空間
IHashCodeProvider
IComparer
Object.GetHashCode
Object.Equals
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 クラス
Hashtable メンバ
System.Collections 名前空間
IHashCodeProvider
IComparer
Object.GetHashCode
Object.Equals
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 クラス
Hashtable メンバ
System.Collections 名前空間
ISerializable
SerializationInfo
StreamingContext
OnDeserialization
Object.GetHashCode
Object.Equals
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 プロパティ



関連項目
Hashtable クラスSystem.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer
Hashtable メソッド

名前 | 説明 | |
---|---|---|
![]() | Add | 指定したキーおよび値を持つ要素を Hashtable に追加します。 |
![]() | Clear | Hashtable からすべての要素を削除します。 |
![]() | Clone | Hashtable の簡易コピーを作成します。 |
![]() | Contains | Hashtable に特定のキーが格納されているかどうかを判断します。 |
![]() | ContainsKey | Hashtable に特定のキーが格納されているかどうかを判断します。 |
![]() | ContainsValue | Hashtable に特定の値が格納されているかどうかを判断します。 |
![]() | CopyTo | 1 次元の Array インスタンスの指定したインデックスに Hashtable の要素をコピーします。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetEnumerator | Hashtable を反復処理する IDictionaryEnumerator を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetObjectData | ISerializable インターフェイスを実装し、Hashtable をシリアル化するために必要なデータを返します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | OnDeserialization | ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントを発生させます。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Remove | 指定したキーを持つ要素を Hashtable から削除します。 |
![]() | Synchronized | Hashtable 用の同期された (スレッド セーフな) ラッパーを返します。 |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | GetHash | 指定したキーのハッシュ コードを返します。 |
![]() | KeyEquals | 特定の Object を Hashtable 内の特定のキーと比較します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |


関連項目
Hashtable クラスSystem.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer
Hashtable メンバ
キーのハッシュ コードに基づいて編成された、キー/値ペアのコレクションを表します。
Hashtable データ型で公開されるメンバを以下の表に示します。





名前 | 説明 | |
---|---|---|
![]() | Add | 指定したキーおよび値を持つ要素を Hashtable に追加します。 |
![]() | Clear | Hashtable からすべての要素を削除します。 |
![]() | Clone | Hashtable の簡易コピーを作成します。 |
![]() | Contains | Hashtable に特定のキーが格納されているかどうかを判断します。 |
![]() | ContainsKey | Hashtable に特定のキーが格納されているかどうかを判断します。 |
![]() | ContainsValue | Hashtable に特定の値が格納されているかどうかを判断します。 |
![]() | CopyTo | 1 次元の Array インスタンスの指定したインデックスに Hashtable の要素をコピーします。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetEnumerator | Hashtable を反復処理する IDictionaryEnumerator を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetObjectData | ISerializable インターフェイスを実装し、Hashtable をシリアル化するために必要なデータを返します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | OnDeserialization | ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントを発生させます。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Remove | 指定したキーを持つ要素を Hashtable から削除します。 |
![]() | Synchronized | Hashtable 用の同期された (スレッド セーフな) ラッパーを返します。 |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | GetHash | 指定したキーのハッシュ コードを返します。 |
![]() | KeyEquals | 特定の Object を Hashtable 内の特定のキーと比較します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |


関連項目
Hashtable クラスSystem.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer
ハッシュ表
【英】:hash table
値(キー)をもつ 個の要素の集合に対し, 挿入・削除・検索の3つの機能を高速化したデータ構造. 値をハッシュキーと呼ばれるいくつかのキーで分類し, それぞれの要素を連結リストなどにより保持する. 挿入と削除をO
時間で行い, 検索は, 最悪の場合はO
となるが, ハッシュキーを適切に設定すれば平均的に O
となることが多い. 辞書データの保持に多く使われ, 辞書のデータを頭文字をハッシュキーとして保持する, などの例がある.
組合せ最適化: | データ構造 ナップサック問題 ネットワークフロー問題 ハッシュ表 パーフェクトグラフ パーフェクトグラフ予想 ヒープ |
ハッシュテーブル
(Hashtable から転送)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2022/10/18 03:00 UTC 版)
![]() | この記事は検証可能な参考文献や出典が全く示されていないか、不十分です。2016年9月) ( |
ハッシュテーブル (英: hash table) は、キーと値の組(エントリと呼ぶ)を複数個格納し、キーに対応する値をすばやく参照するためのデータ構造。ハッシュ表ともいう。ハッシュテーブルは連想配列や集合の効率的な実装のうち1つである。
概要
ハッシュテーブルはキーをもとに生成されたハッシュ値を添え字とした配列である。通常、配列の添え字には非負整数しか扱えない。そこで、キーを要約する値であるハッシュ値を添え字として値を管理することで、検索や追加を要素数によらず定数時間O(1)で実現する。しかしハッシュ関数の選び方(例えば、異なるキーから頻繁に同じハッシュ値が生成される場合)によっては、性能が劣化して最悪の場合O(n)となってしまう。
衝突処理
複数の異なるキーが同じハッシュ値になることを衝突 (collision) と呼ぶ。キーの分布が予めわからない場合、衝突を避けることはできない。同じハッシュ値となるキーを同族キーと呼ぶ。衝突が発生したときの対処の方法は、開番地法と連鎖法に大別される。
連鎖法
衝突を起こしたキー同士をポインタでつなぐ方式を連鎖法と呼ぶ。テーブルの各番地にはキーそのものではなく、同族キーを保持するリンクリストを格納する。
開番地法
衝突が発生した際、テーブル中の空いている別の番地を探す方式を開番地法と呼ぶ。その方法としてハッシュ関数とは別の関数を用いて次の候補となる番地を求める。別の番地を探す関数は繰り返し適用することによってテーブル内の全ての番地を走査できるように選ぶ。
実装方法
連鎖法の一つの実装例を示す。まず、ルート配列と呼ばれる要素数 N の配列を一つ用意する。ルート配列の各要素は、リスト(便宜上エントリリストと呼ぶ)とする。エントリリストには、少数のエントリを格納する。
エントリを格納する場合、エントリのキーをもとにハッシュ関数を用いてハッシュ値を生成する。ハッシュ関数は 0 から N - 1 までの整数値を生成するものであって、一様な分布と高速な計算が要求される。ハッシュ値を i とするとき、ルート配列上の i 番目のエントリリストにこのエントリを格納する。衝突が発生したとき、それらのエントリは同一のエントリリストに格納される。
あるキーをもつエントリを検索する場合、そのキーからハッシュ値を生成する。ハッシュ値を j とするとき、ルート配列上の j 番目のエントリリストに入っているエントリを一つずつ検索し、キーが一致しているものを取り出す。ルート配列上のエントリリストが高速でアクセスできる必要がある。
ハッシュテーブルの自動拡張
ハッシュテーブルはエントリの数が配列のサイズに近づくほど衝突の確率が高くなり、性能が悪化してしまう。この比率をload factor(座席利用率)と呼び、n/Nの形で表す。nはエントリの数、Nは配列のサイズを指す。
連鎖法の場合はload factorの増加に対して線形に性能が悪化する。しかし開番地法の場合は衝突したキーが配列の空いた番地に格納されるため、load factorが0.8を超える付近で性能が急激に悪化する。
この問題を回避するため、load factorが一定を超えた場合に、より大きいサイズのハッシュテーブルを用意して格納し直す操作が必要となる。これをリハッシュ (rehash) と呼ぶ。この操作はすべての要素のハッシュ値を再計算して新たなハッシュテーブルに格納するためO(n)であるが、配列のサイズを指数的に拡張する事で、動的配列の末尾追加操作と同様に償却解析によって計算量をO(1)とみなす事ができる。
より単純な回避方法として、あらかじめ想定されるエントリの数に対して十分に大きなサイズの配列を用意する方法もあるが、エントリの数を事前に想定できない場合には適用できない。
全要素の列挙
順序保証のない列挙
最も単純な実装として、ハッシュテーブルのルート配列上を 0 から N - 1 まで走査し、その中に存在するエントリを列挙する方法がある。連鎖法の場合は見つかったエントリリストをさらにたどる必要がある。しかしこの方法で列挙した場合、各エントリはハッシュ関数によって格納位置を決められているために、全く意味を持たない順序で要素が列挙される。これはランダムな順序という意味ではない。利用方法によっては列挙操作が追加した順序を保持しているかのように見えるため、ハッシュテーブルの利用者が誤解する場合がある。この実装による列挙の計算量はルート配列のサイズ N と連鎖法でのルート配列上にないエントリリストの数 m との合計O(N + m)となる。そのため、ルート配列をあらかじめ大きなサイズで確保しているとこの実装での列挙に時間がかかる。
追加した順序での列挙
追加した順序で要素を列挙する実装方法として、各エントリに追加順を保持するリンクリストのポインタを別に持たせ、列挙する際はそちらのポインタをたどる方法がある。検索・追加操作のみならば単方向リストで実現可能だが、削除操作もサポートする場合は双方向リストで実装しなければO(1)での操作を実現できない。JavaのLinkedHashMapはこの実装であり、その他言語でも利便性からハッシュテーブルが標準で追加順を保証している事がある。欠点として、各エントリに別のリンクリスト用ポインタが必要なためにメモリ消費量が増加する。
プログラミング言語におけるハッシュテーブルの実装
- Javaにおける
,HashMap
,LinkedHashMap
クラスHashtable
- AWKにおける連想配列
- PerlにおけるHash
- JavaScriptにおけるオブジェクト
- Pythonにおける辞書
- C++のSTLの
std::unordered_map
- Rubyの
Hash
クラス - Common Lispにおける
hash-table
クラス - CLI (Common Language Infrastructure) の
System.Collections.Hashtable
,System.Collections.Specialized.ListDictionary
,System.Collections.Specialized.HybridDictionary
,System.Collections.Generic.Dictionary<TKey, TValue>
- RustのHashMap
- Nimの
std/tables
脚注
関連項目
- Hashtableのページへのリンク