Hash tableとは? わかりやすく解説

Weblio 辞書 > 辞書・百科事典 > デジタル大辞泉 > Hash tableの意味・解説 

ハッシュ‐テーブル【hash table】


ハッシュ‐ひょう〔‐ヘウ〕【ハッシュ表】

読み方:はっしゅひょう

《hash table》⇒ハッシュテーブル


Hashtable クラス

キーのハッシュ コードに基づいて編成された、キー/値ペアのコレクションを表します。

名前空間: System.Collections
アセンブリ: 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
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class Hashtable implements IDictionary,
 ICollection, 
    IEnumerable, ISerializable, IDeserializationCallback, ICloneable
SerializableAttribute 
ComVisibleAttribute(true) 
public class Hashtable implements 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.Object
  System.Collections.Hashtable
     System.Configuration.SettingsAttributeDictionary
     System.Configuration.SettingsContext
     System.Data.PropertyCollection
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable メンバ
System.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer

Hashtable コンストラクタ ()

既定の初期量、テーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説
使用例使用例

異なる 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (Int32, Single)

指定した初期量とテーブル占有率、および既定のハッシュ コード プロバイダと比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

または

loadFactor が 0.1 未満です。

または

loadFactor が 1.0 より大きい値です。

解説解説
使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
Object.GetHashCode
Object.Equals

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
)

パラメータ

d

新しい Hashtable オブジェクトにコピーする IDictionary オブジェクト。

hcp

Hashtable 内のすべてのキーにハッシュ コードを提供する IHashCodeProvider オブジェクト。

または

各キーの Object.GetHashCode の実装である既定のハッシュ コード プロバイダを使用する場合は null 参照 (Visual Basic では Nothing)。

comparer

2 つのキーが等しいかどうかを判断するために使用する IComparer オブジェクト。

または

各キーの Object.Equals の実装である既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

d が null 参照 (Visual Basic では Nothing) です。

解説解説

初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。

テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。

実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。

ハッシュ コード プロバイダは、Hashtable オブジェクト内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。

比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。

新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

このコンストラクタは O(n) 操作です。ここで、n は d パラメータ内の要素数です。

使用例使用例

異なる 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
IDictionary
IHashCodeProvider
IComparer
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (IDictionary, Single, IEqualityComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、指定したテーブル占有率および IEqualityComparer オブジェクトを使用します。

名前空間: System.Collections
アセンブリ: 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
)

パラメータ

d

新しい Hashtable オブジェクトにコピーする IDictionary オブジェクト。

loadFactor

0.1 ~ 1.0 の範囲の値。これに、最高のパフォーマンスを提供する既定値を掛けます。その結果が、バケット数に対する要素数の最大比率です。

equalityComparer

Hashtable で使用するハッシュ コード プロバイダと比較演算子を定義する IEqualityComparer オブジェクト。

または

既定のハッシュ コード プロバイダおよび既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。既定のハッシュ コード プロバイダは、各キーの Object.GetHashCode の実装です。また、既定の比較演算子は各キーの Object.Equals の実装です。

例外例外
例外の種類条件

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 オブジェクトを反復処理するのと同じ順序に並べ替えられます。

このコンストラクタは O(n) 操作です。ここで、n は d パラメータ内の要素数です。

使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Hashtable コンストラクタ (Int32, Single, IEqualityComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定した初期量、テーブル占有率、および IEqualityComparer オブジェクトを使用して、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: 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 Hashtable (
    int capacity,
    float loadFactor,
    IEqualityComparer equalityComparer
)
public:
Hashtable (
    int capacity, 
    float loadFactor, 
    IEqualityComparer^ equalityComparer
)
public Hashtable (
    int capacity, 
    float loadFactor, 
    IEqualityComparer equalityComparer
)
public function Hashtable (
    capacity : int, 
    loadFactor : float, 
    equalityComparer : IEqualityComparer
)

パラメータ

capacity

Hashtable オブジェクトが初期状態で格納できる要素の概数。

loadFactor

0.1 ~ 1.0 の範囲の値。これに、最高のパフォーマンスを提供する既定値を掛けます。その結果が、バケット数に対する要素数の最大比率です。

equalityComparer

Hashtable で使用するハッシュ コード プロバイダと比較演算子を定義する IEqualityComparer オブジェクト。

または

既定のハッシュ コード プロバイダおよび既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。既定のハッシュ コード プロバイダは、各キーの Object.GetHashCode の実装です。また、既定の比較演算子は各キーの Object.Equals の実装です。

例外例外
例外の種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

または

loadFactor が 0.1 未満です。

または

loadFactor が 1.0 より大きい値です。

解説解説

初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。

テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。

実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。

IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable でキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。

ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。

比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

このコンストラクタは O(n) 操作です。ここで、n は capacity パラメータです。

使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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
)

パラメータ

d

新しい Hashtable オブジェクトにコピーする IDictionary オブジェクト。

loadFactor

0.1 ~ 1.0 の範囲の値。これに、最高のパフォーマンスを提供する既定値を掛けます。その結果が、バケット数に対する要素数の最大比率です。

hcp

Hashtable 内のすべてのキーにハッシュ コードを提供する IHashCodeProvider オブジェクト。

または

各キーの Object.GetHashCode の実装である既定のハッシュ コード プロバイダを使用する場合は null 参照 (Visual Basic では Nothing)。

comparer

2 つのキーが等しいかどうかを判断するために使用する IComparer オブジェクト。

または

各キーの Object.Equals の実装である既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

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 オブジェクトを反復処理するのと同じ順序に並べ替えられます。

このコンストラクタは O(n) 操作です。ここで、n は d パラメータ内の要素数です。

使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Hashtable コンストラクタ

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 クラス
Hashtable メンバ
System.Collections 名前空間
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (IDictionary, Single)

指定したディレクトリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、指定したテーブル占有率、および既定のハッシュ コード プロバイダと比較演算子を使用します。

名前空間: System.Collections
アセンブリ: 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
)

パラメータ

d

新しい Hashtable オブジェクトにコピーする IDictionary オブジェクト。

loadFactor

0.1 ~ 1.0 の範囲の値。これに、最高のパフォーマンスを提供する既定値を掛けます。その結果が、バケット数に対する要素数の最大比率です。

例外例外
例外の種類条件

ArgumentNullException

d が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

loadFactor が 0.1 未満です。

または

loadFactor が 1.0 より大きい値です。

解説解説
使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
IDictionary
Object.GetHashCode
Object.Equals

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
)

パラメータ

capacity

Hashtable オブジェクトが初期状態で格納できる要素の概数。

hcp

Hashtable 内のすべてのキーにハッシュ コードを提供する IHashCodeProvider オブジェクト。

または

各キーの Object.GetHashCode の実装である既定のハッシュ コード プロバイダを使用する場合は null 参照 (Visual Basic では Nothing)。

comparer

2 つのキーが等しいかどうかを判断するために使用する IComparer オブジェクト。

または

各キーの Object.Equals の実装である既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

解説解説
使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
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 _
)
Dim hcp As IHashCodeProvider
Dim comparer As IComparer

Dim instance As New Hashtable(hcp,
 comparer)
[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
)

パラメータ

hcp

Hashtable オブジェクト内のすべてのキーにハッシュ コードを提供する IHashCodeProvider オブジェクト。

または

各キーの Object.GetHashCode の実装である既定のハッシュ コード プロバイダを使用する場合は null 参照 (Visual Basic では Nothing)。

comparer

2 つのキーが等しいかどうかを判断するために使用する IComparer オブジェクト。

または

各キーの Object.Equals の実装である既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。

解説解説
使用例使用例

異なる 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
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
)

パラメータ

capacity

Hashtable オブジェクトが初期状態で格納できる要素の概数。

loadFactor

0.1 ~ 1.0 の範囲の値。これに、最高のパフォーマンスを提供する既定値を掛けます。その結果が、バケット数に対する要素数の最大比率です。

hcp

Hashtable 内のすべてのキーにハッシュ コードを提供する IHashCodeProvider オブジェクト。

または

各キーの Object.GetHashCode の実装である既定のハッシュ コード プロバイダを使用する場合は null 参照 (Visual Basic では Nothing)。

comparer

2 つのキーが等しいかどうかを判断するために使用する IComparer オブジェクト。

または

各キーの Object.Equals の実装である既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

または

loadFactor が 0.1 未満です。

または

loadFactor が 1.0 より大きい値です。

解説解説

初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。

テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。テーブル占有率 1.0 は、検索速度とサイズの最適なバランスを実現します。

実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。

ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。

比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

カスタム ハッシュ コード プロバイダとカスタム比較演算子を使用すると、大文字と小文字を区別しない文字列検索などの処理を実行できます。

このコンストラクタは O(n) 操作です。ここで、n は capacity パラメータです。

使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
IHashCodeProvider
IComparer
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (SerializationInfo, StreamingContext)

指定した SerializationInfo オブジェクトと StreamingContext オブジェクトを使用してシリアル化できる、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Protected Sub New ( _
    info As SerializationInfo, _
    context As StreamingContext _
)
Dim info As SerializationInfo
Dim context As StreamingContext

Dim instance As New Hashtable(info,
 context)
protected Hashtable (
    SerializationInfo info,
    StreamingContext context
)
protected:
Hashtable (
    SerializationInfo^ info, 
    StreamingContext context
)
protected Hashtable (
    SerializationInfo info, 
    StreamingContext context
)
protected function Hashtable (
    info : SerializationInfo, 
    context : StreamingContext
)

パラメータ

info

Hashtable オブジェクトをシリアル化するために必要な情報を格納している SerializationInfo オブジェクト。

context

Hashtable に関連付けられているシリアル化ストリームのソースおよびデスティネーションを格納している StreamingContext オブジェクト。

例外例外
例外の種類条件

ArgumentNullException

info が null 参照 (Visual Basic では Nothing) です。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
ISerializable
SerializationInfo
StreamingContext
OnDeserialization
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (Int32)

指定した初期量と、既定のテーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

解説解説
使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (IDictionary)

指定したディレクトリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、既定のテーブル占有率、ハッシュ コード プロバイダ、および比較演算子を使用します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    d As IDictionary _
)
Dim d As IDictionary

Dim instance As New Hashtable(d)
public Hashtable (
    IDictionary d
)
public:
Hashtable (
    IDictionary^ d
)
public Hashtable (
    IDictionary d
)
public function Hashtable (
    d : IDictionary
)

パラメータ

d

新しい Hashtable オブジェクトにコピーする IDictionary オブジェクト。

例外例外
例外の種類条件

ArgumentNullException

d が null 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

異なる 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
IDictionary
Object.GetHashCode
Object.Equals

Hashtable コンストラクタ (IEqualityComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

既定の初期量とテーブル占有率、および指定した IEqualityComparer オブジェクトを使用して、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    equalityComparer As IEqualityComparer _
)
Dim equalityComparer As IEqualityComparer

Dim instance As New Hashtable(equalityComparer)
public Hashtable (
    IEqualityComparer equalityComparer
)
public:
Hashtable (
    IEqualityComparer^ equalityComparer
)
public Hashtable (
    IEqualityComparer equalityComparer
)
public function Hashtable (
    equalityComparer : IEqualityComparer
)

パラメータ

equalityComparer

Hashtable オブジェクトで使用するハッシュ コード プロバイダと比較演算子を定義する IEqualityComparer オブジェクト。

または

既定のハッシュ コード プロバイダおよび既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。既定のハッシュ コード プロバイダは、各キーの Object.GetHashCode の実装です。また、既定の比較演算子は各キーの Object.Equals の実装です。

解説解説

ハッシュ テーブルの容量は、テーブル占有率に基づいてハッシュ テーブル バケットの最適数を計算するために使用されます。容量は必要に応じて自動的に増加します。

テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。

実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。

IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable オブジェクトでキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。

ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。

比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

このコンストラクタは O(1) 操作です。

使用例使用例

異なる 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Hashtable コンストラクタ (IDictionary, IEqualityComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定したディクショナリの要素を新しい Hashtable オブジェクトにコピーすることによって、Hashtable クラスの新しいインスタンスを初期化します。新しい Hashtable オブジェクトは、コピーされた要素数に等しい初期量を備えており、既定のテーブル占有率および指定した IEqualityComparer オブジェクトを使用します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    d As IDictionary, _
    equalityComparer As IEqualityComparer _
)
Dim d As IDictionary
Dim equalityComparer As IEqualityComparer

Dim instance As New Hashtable(d,
 equalityComparer)
public Hashtable (
    IDictionary d,
    IEqualityComparer equalityComparer
)
public:
Hashtable (
    IDictionary^ d, 
    IEqualityComparer^ equalityComparer
)
public Hashtable (
    IDictionary d, 
    IEqualityComparer equalityComparer
)
public function Hashtable (
    d : IDictionary, 
    equalityComparer : IEqualityComparer
)

パラメータ

d

新しい Hashtable オブジェクトにコピーする IDictionary オブジェクト。

equalityComparer

Hashtable で使用するハッシュ コード プロバイダと比較演算子を定義する IEqualityComparer オブジェクト。

または

既定のハッシュ コード プロバイダおよび既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。既定のハッシュ コード プロバイダは、各キーの Object.GetHashCode の実装です。また、既定の比較演算子は各キーの Object.Equals の実装です。

例外例外
例外の種類条件

ArgumentNullException

d が null 参照 (Visual Basic では Nothing) です。

解説解説

初期量は、コピー元のディクショナリ内の要素数に設定されます。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。

テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。

実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。

IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable オブジェクトでキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。

ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。

比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

新しい Hashtable の要素は、列挙子が IDictionary オブジェクトを反復処理するのと同じ順序に並べ替えられます。

このコンストラクタは O(n) 操作です。ここで、n は d パラメータ内の要素数です。

使用例使用例

異なる 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Hashtable クラス
Hashtable メンバ
System.Collections 名前空間
IDictionary
IEqualityComparer

Hashtable コンストラクタ (Int32, IEqualityComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定した初期量と IEqualityComparer、および既定のテーブル占有率を使用して、Hashtable クラスの新しい空のインスタンスを初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    capacity As Integer, _
    equalityComparer As IEqualityComparer _
)
Dim capacity As Integer
Dim equalityComparer As IEqualityComparer

Dim instance As New Hashtable(capacity,
 equalityComparer)
public Hashtable (
    int capacity,
    IEqualityComparer equalityComparer
)
public:
Hashtable (
    int capacity, 
    IEqualityComparer^ equalityComparer
)
public Hashtable (
    int capacity, 
    IEqualityComparer equalityComparer
)
public function Hashtable (
    capacity : int, 
    equalityComparer : IEqualityComparer
)

パラメータ

capacity

Hashtable オブジェクトが初期状態で格納できる要素の概数。

equalityComparer

Hashtable で使用するハッシュ コード プロバイダと比較演算子を定義する IEqualityComparer オブジェクト。

または

既定のハッシュ コード プロバイダおよび既定の比較演算子を使用する場合は null 参照 (Visual Basic では Nothing)。既定のハッシュ コード プロバイダは、各キーの Object.GetHashCode の実装です。また、既定の比較演算子は各キーの Object.Equals の実装です。

例外例外
例外の種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

解説解説

初期量を指定すると、Hashtable オブジェクトに要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。容量は、必要に応じてテーブル占有率に基づいて自動的に増加します。

テーブル占有率は、バケット数に対する要素数の最大比率です。テーブル占有率を小さくすると、検索速度は速くなりますが、メモリの消費量は増加します。

実際のテーブル占有率が指定した占有率に達すると、バケット数は、現在のバケット数の 2 倍より大きい範囲で最小の素数になるように、自動的に増やされます。

IEqualityComparer オブジェクトには、ハッシュ コード プロバイダおよび比較演算子が含まれます。IEqualityComparer が Hashtable コンストラクタで使用される場合、Hashtable でキーとして使用されるオブジェクトは、Object.GetHashCode メソッドおよび Object.Equals メソッドをオーバーライドする必要はありません。

ハッシュ コード プロバイダは、Hashtable 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは、キーの Object.GetHashCode の実装です。

比較演算子は 2 つのキーが等しいかどうかを判断します。Hashtable 内のすべてのキーは一意である必要があります。既定の比較演算子は、キーの Object.Equals の実装です。

IEqualityComparer により、大文字と小文字を区別せずに文字列の検索を行うようなシナリオが可能になります。

このコンストラクタは O(n) 操作です。ここで、n は capacity パラメータです。

使用例使用例

異なる 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

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Hashtable プロパティ


パブリック プロパティパブリック プロパティ

( プロテクト プロパティ も参照)
  名前 説明
パブリック プロパティ Values Hashtable 内の値を格納している ICollection を取得します。
プロテクト プロパティプロテクト プロパティ
参照参照

関連項目

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 から継承されます。)
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator コレクションを反復処理する列挙子を返します。
参照参照

関連項目

Hashtable クラス
System.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer

Hashtable メンバ

キーのハッシュ コードに基づいて編成された、キー/値ペアのコレクションを表します。

Hashtable データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
プロテクト コンストラクタプロテクト コンストラクタ
パブリック プロパティパブリック プロパティ
( プロテクト プロパティ も参照)
  名前 説明
パブリック プロパティ Values Hashtable 内の値を格納している ICollection を取得します。
プロテクト プロパティプロテクト プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド 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 から継承されます。)
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator コレクションを反復処理する列挙子を返します。
参照参照

関連項目

Hashtable クラス
System.Collections 名前空間
IDictionary
IHashCodeProvider
Object.GetHashCode
Object.Equals
DictionaryEntry 構造体
System.Collections.Generic.Dictionary
IEqualityComparer

ハッシュ表

読み方:はっしゅひょう
【英】:hash table

値(キー)をもつn\, 個の要素の集合に対し, 挿入・削除・検索の3つの機能を高速化したデータ構造. 値をハッシュキーと呼ばれるいくつかのキーで分類し, それぞれの要素を連結リストなどにより保持する. 挿入と削除をO(1)\, 時間で行い, 検索は, 最悪の場合はO(n)\, となるが, ハッシュキーを適切に設定すれば平均的に O(1)\, となることが多い. 辞書データの保持に多く使われ, 辞書のデータを頭文字をハッシュキーとして保持する, などの例がある.



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Hash table」の関連用語

Hash tableのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Hash tableのページの著作権

   
デジタル大辞泉デジタル大辞泉
(C)Shogakukan Inc.
株式会社 小学館
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.
日本オペレーションズ・リサーチ学会日本オペレーションズ・リサーチ学会
Copyright (C) 2026 (社)日本オペレーションズ・リサーチ学会 All rights reserved.

©2026 GRAS Group, Inc.RSS