Hashtable 생성자

정의

Hashtable 클래스의 새 인스턴스를 초기화합니다.

오버로드

Hashtable()

기본 초기 용량, 로드 비율, 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(Int32, Single, IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.

지정된 초기 용량, 로드 비율, 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 지정된 로드 비율, 해시 코드 공급자 및 비교자를 사용합니다.

Hashtable(Int32, Single, IEqualityComparer)

지정된 초기 용량, 로드 비율 및 Hashtable 개체를 사용하여 IEqualityComparer 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(Int32, IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.

지정된 초기 용량, 해시 코드 공급자 및 비교자와 기본 로드 비율을 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(IDictionary, Single, IEqualityComparer)

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 지정된 로드 비율 및 IEqualityComparer 개체를 사용합니다.

Hashtable(IDictionary, IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 기본 로드 비율과 지정된 해시 코드 공급자 및 비교자를 사용합니다. 이 API는 더 이상 사용되지 않습니다. 다른 방법을 보려면 Hashtable(IDictionary, IEqualityComparer)를 참조하십시오.

Hashtable(Int32, Single)

지정된 초기 용량 및 로드 비율과 기본 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(SerializationInfo, StreamingContext)
사용되지 않음.

지정된 HashtableSerializationInfo 개체를 사용하여 serialize할 수 있는 StreamingContext 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.
사용되지 않음.

기본 초기 용량 및 로드 비율과 지정된 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(IDictionary, Single)

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 지정된 로드 비율과 기본 해시 코드 공급자 및 비교자를 사용합니다.

Hashtable(IDictionary, IEqualityComparer)

지정된 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 기본 로드 비율과 지정된 IEqualityComparer 개체를 사용합니다.

Hashtable(Int32)

지정된 초기 용량과 기본 로드 비율, 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(IEqualityComparer)

기본 초기 용량 및 로드 비율과 지정된 Hashtable 개체를 사용하여 IEqualityComparer 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable(IDictionary)

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 기본 로드 비율, 해시 코드 공급자 및 비교자를 사용합니다.

Hashtable(Int32, IEqualityComparer)

지정된 초기 용량 및 Hashtable와 기본 로드 비율을 사용하여 IEqualityComparer 클래스의 비어 있는 새 인스턴스를 초기화합니다.

Hashtable()

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

기본 초기 용량, 로드 비율, 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable();
public Hashtable ();
Public Sub New ()

예제

다음 코드 예제에서는 서로 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 각각 동일한 요소를 포함 하는 경우에 해시 테이블의 동작의 차이점을 보여 줍니다.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myComparer : IEqualityComparer
{
public:
    virtual bool Equals(Object^ x, Object^ y) 
    {
        return x->Equals(y);
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

int main()
{
   
   // Create a hash table using the default hash code provider and the default comparer.
   Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
   myHT1->Add( "FIRST", "Hello" );
   myHT1->Add( "SECOND", "World" );
   myHT1->Add( "THIRD", "!" );
   
   // Create a hash table using the specified IEqualityComparer that uses
   // the default Object.Equals to determine equality.
   Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
   myHT2->Add( "FIRST", "Hello" );
   myHT2->Add( "SECOND", "World" );
   myHT2->Add( "THIRD", "!" );
   
   // Create a hash table using a case-insensitive hash code provider and
   // case-insensitive comparer based on the InvariantCulture.
   Hashtable^ myHT3 = gcnew Hashtable(
            CaseInsensitiveHashCodeProvider::DefaultInvariant,
            CaseInsensitiveComparer::DefaultInvariant);
   myHT3->Add( "FIRST", "Hello" );
   myHT3->Add( "SECOND", "World" );
   myHT3->Add( "THIRD", "!" );
   
   // Create a hash table using an IEqualityComparer that is based on
   // the Turkish culture (tr-TR) where "I" is not the uppercase
   // version of "i".
   CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
   Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
   myHT4->Add( "FIRST", "Hello" );
   myHT4->Add( "SECOND", "World" );
   myHT4->Add( "THIRD", "!" );
   
   // Search for a key in each hash table.
   Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
using System;
using System.Collections;
using System.Globalization;

class myComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable();
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the default Object.Equals to determine equality.
        var myHT2 = new Hashtable(new myComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using a case-insensitive hash code provider and
        // case-insensitive comparer based on the InvariantCulture.
        Hashtable myHT3 = new Hashtable(
            CaseInsensitiveHashCodeProvider.DefaultInvariant,
            CaseInsensitiveComparer.DefaultInvariant);
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");
        myHT4.Add("SECOND", "World");
        myHT4.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myComparer
    Implements IEqualityComparer
    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return x.Equals(y)
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode

        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable()
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the default Object.Equals to determine equality.
        Dim myHT2 As New Hashtable(New myComparer())
        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using a case-insensitive hash code provider and
        ' case-insensitive comparer based on the InvariantCulture.
        Dim myHT3 As New Hashtable( _
            CaseInsensitiveHashCodeProvider.DefaultInvariant, _
            CaseInsensitiveComparer.DefaultInvariant)
        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
        myHT4.Add("FIRST", "Hello")
        myHT4.Add("SECOND", "World")
        myHT4.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")

    End Sub

End Class

'This code produces the following output.
'Results vary depending on the system's culture settings.

'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False

설명

해시 테이블의 용량은 부하 요인에 따라 최적의 해시 테이블 버킷 수를 계산하는 데 사용됩니다. 필요에 따라 용량이 자동으로 증가합니다.

부하 계수는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

이 생성자는 작업입니다 O(1) .

추가 정보

적용 대상

Hashtable(Int32, Single, IHashCodeProvider, IComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

주의

Please use Hashtable(int, float, IEqualityComparer) instead.

주의

This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.

지정된 초기 용량, 로드 비율, 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(int capacity, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(int, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)

매개 변수

capacity
Int32

Hashtable 개체가 처음에 포함할 수 있는 대략적인 요소 수입니다.

loadFactor
Single

최고의 성능을 제공하는 기본값으로 곱한 0.1에서 1.0 사이의 수입니다. 결과는 버킷과 요소의 최대 비율입니다.

hcp
IHashCodeProvider

IHashCodeProvider의 모든 키에 해시 코드를 제공하는 Hashtable 개체입니다.

또는

각 키의 GetHashCode() 구현인 기본 해시 코드 공급자를 사용하면 null입니다.

comparer
IComparer

두 키가 같은지 여부를 확인하는 데 사용할 IComparer 개체입니다.

또는

각 키로 Equals(Object)를 구현한 기본 비교자를 사용하면 null입니다.

특성

예외

capacity가 0보다 작은 경우

또는

loadFactor가 0.1보다 작습니다.

또는

loadFactor가 1.0보다 큽니다.

예제

다음 코드 예제에서는 서로 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 각각 동일한 요소를 포함 하는 경우에 해시 테이블의 동작의 차이점을 보여 줍니다.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3, .8f);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));

        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer())

        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer(myCul))

        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

    End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량을 지정하면 개체에 요소를 Hashtable 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다. 부하 계수에 따라 필요에 따라 용량이 자동으로 증가합니다.

부하 계수는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다. 1.0의 부하 요소는 속도와 크기 간의 최상의 균형입니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

사용자 지정 해시 코드 공급자 및 사용자 지정 비교자를 사용하면 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

이 생성자는 작업이며 O(n) 여기서 n 는 매개 변수입니다 capacity .

추가 정보

적용 대상

Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

주의

Please use Hashtable(IDictionary, float, IEqualityComparer) instead.

주의

This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 지정된 로드 비율, 해시 코드 공급자 및 비교자를 사용합니다.

public:
 Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, float, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)

매개 변수

d
IDictionary

IDictionary 개체로 복사할 Hashtable 개체입니다.

loadFactor
Single

최고의 성능을 제공하는 기본값으로 곱한 0.1에서 1.0 사이의 수입니다. 결과는 버킷과 요소의 최대 비율입니다.

hcp
IHashCodeProvider

IHashCodeProvider의 모든 키에 해시 코드를 제공하는 Hashtable 개체입니다.

또는

각 키의 GetHashCode() 구현인 기본 해시 코드 공급자를 사용하면 null입니다.

comparer
IComparer

두 키가 같은지 여부를 확인하는 데 사용할 IComparer 개체입니다.

또는

각 키로 Equals(Object)를 구현한 기본 비교자를 사용하면 null입니다.

특성

예외

d이(가) null인 경우

loadFactor가 0.1보다 작습니다.

또는

loadFactor가 1.0보다 큽니다.

예제

다음 코드 예제에서는 서로 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 각각 동일한 요소를 포함 하는 경우에 해시 테이블의 동작의 차이점을 보여 줍니다.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
       
       // Create the dictionary.
       SortedList^ mySL = gcnew SortedList;
       mySL->Add( "FIRST", "Hello" );
       mySL->Add( "SECOND", "World" );
       mySL->Add( "THIRD", "!" );
       
       // Create a hash table using the default hash code provider and the default comparer.
       Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
       
       // Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
       Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
       
       // Create a hash table using the specified KeyComparer.
       // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
       // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
       CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
       Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
       
       // Search for a key in each hash table.
       Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(mySL, .8f);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(mySL, .8f,
            new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량은 원본 사전의 요소 수로 설정됩니다. 부하 계수에 따라 필요에 따라 용량이 자동으로 증가합니다.

부하 계수는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다. 1.0의 부하 요소는 속도와 크기 간의 최상의 균형입니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

사용자 지정 해시 코드 공급자 및 사용자 지정 비교자를 사용하면 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

Hashtable 의 요소는 열거자가 개체를 반복하는 순서와 동일한 순서로 정렬됩니다 IDictionary .

이 생성자는 연산입니다 O(n) . 여기서 n 는 매개 변수의 요소 d 수입니다.

적용 대상

Hashtable(Int32, Single, IEqualityComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정된 초기 용량, 로드 비율 및 Hashtable 개체를 사용하여 IEqualityComparer 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(int capacity, float loadFactor, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : int * single * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, equalityComparer As IEqualityComparer)

매개 변수

capacity
Int32

Hashtable 개체가 처음에 포함할 수 있는 대략적인 요소 수입니다.

loadFactor
Single

최고의 성능을 제공하는 기본값으로 곱한 0.1에서 1.0 사이의 수입니다. 결과는 버킷과 요소의 최대 비율입니다.

equalityComparer
IEqualityComparer

IEqualityComparer과 함께 사용할 해시 코드 공급자와 비교자를 정의하는 Hashtable 개체입니다.

또는

기본 해시 코드 공급자와 기본 비교자를 사용하면 null입니다. 기본 해시 코드 공급자는 각 키의 GetHashCode() 구현이며 기본 비교자는 각 키의 Equals(Object) 구현입니다.

예외

capacity가 0보다 작은 경우

또는

loadFactor가 0.1보다 작습니다.

또는

loadFactor가 1.0보다 큽니다.

예제

다음 코드 예제에서는 서로 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 각각 동일한 요소를 포함 하는 경우에 해시 테이블의 동작의 차이점을 보여 줍니다.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3, .8f);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));

        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer())

        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer(myCul))

        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

    End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량을 지정하면 개체에 요소를 Hashtable 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다. 부하 계수에 따라 필요에 따라 용량이 자동으로 증가합니다.

부하 계수는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다. 1.0의 부하 요소는 속도와 크기 간의 최상의 균형입니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

개체에는 IEqualityComparer 해시 코드 공급자와 비교자가 모두 포함됩니다. IEqualityComparer 가 생성자에서 Hashtable 사용되는 경우 의 키 Hashtable 로 사용되는 개체는 및 Object.Equals 메서드를 재정의 Object.GetHashCode 할 필요가 없습니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

IEqualityComparer 에서는 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

이 생성자는 작업이며 O(n) 여기서 n 는 매개 변수입니다 capacity .

추가 정보

적용 대상

Hashtable(Int32, IHashCodeProvider, IComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

주의

Please use Hashtable(int, IEqualityComparer) instead.

주의

This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.

지정된 초기 용량, 해시 코드 공급자 및 비교자와 기본 로드 비율을 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(int capacity, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(int, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, hcp As IHashCodeProvider, comparer As IComparer)

매개 변수

capacity
Int32

Hashtable 개체가 처음에 포함할 수 있는 대략적인 요소 수입니다.

hcp
IHashCodeProvider

IHashCodeProvider의 모든 키에 해시 코드를 제공하는 Hashtable 개체입니다.

또는

각 키의 GetHashCode() 구현인 기본 해시 코드 공급자를 사용하면 null입니다.

comparer
IComparer

두 키가 같은지 여부를 확인하는 데 사용할 IComparer 개체입니다.

또는

각 키로 Equals(Object)를 구현한 기본 비교자를 사용하면 null입니다.

특성

예외

capacity가 0보다 작은 경우

예제

다음 코드 예제에서는 서로 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 각각 동일한 요소를 포함 하는 경우에 해시 테이블의 동작의 차이점을 보여 줍니다.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(3)
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(3, New myCultureComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량을 지정하면 개체에 요소를 Hashtable 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다. 부하 계수에 따라 필요에 따라 용량이 자동으로 증가합니다.

부하 계수는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

사용자 지정 해시 코드 공급자 및 사용자 지정 비교자를 사용하면 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

이 생성자는 작업이며 O(n) 여기서 n 는 매개 변수입니다 capacity .

추가 정보

적용 대상

Hashtable(IDictionary, Single, IEqualityComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 지정된 로드 비율 및 IEqualityComparer 개체를 사용합니다.

public:
 Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, equalityComparer As IEqualityComparer)

매개 변수

d
IDictionary

IDictionary 개체로 복사할 Hashtable 개체입니다.

loadFactor
Single

최고의 성능을 제공하는 기본값으로 곱한 0.1에서 1.0 사이의 수입니다. 결과는 버킷과 요소의 최대 비율입니다.

equalityComparer
IEqualityComparer

IEqualityComparer과 함께 사용할 해시 코드 공급자와 비교자를 정의하는 Hashtable 개체입니다.

또는

기본 해시 코드 공급자와 기본 비교자를 사용하면 null입니다. 기본 해시 코드 공급자는 각 키의 GetHashCode() 구현이며 기본 비교자는 각 키의 Equals(Object) 구현입니다.

예외

d이(가) null인 경우

loadFactor가 0.1보다 작습니다.

또는

loadFactor가 1.0보다 큽니다.

예제

다음 코드 예제에서는 서로 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 각각 동일한 요소를 포함 하는 경우에 해시 테이블의 동작의 차이점을 보여 줍니다.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
       
       // Create the dictionary.
       SortedList^ mySL = gcnew SortedList;
       mySL->Add( "FIRST", "Hello" );
       mySL->Add( "SECOND", "World" );
       mySL->Add( "THIRD", "!" );
       
       // Create a hash table using the default hash code provider and the default comparer.
       Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
       
       // Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
       Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
       
       // Create a hash table using the specified KeyComparer.
       // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
       // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
       CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
       Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
       
       // Search for a key in each hash table.
       Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(mySL, .8f);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(mySL, .8f,
            new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량은 원본 사전의 요소 수로 설정됩니다. 부하 계수에 따라 필요에 따라 용량이 자동으로 증가합니다.

부하 계수는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다. 1.0의 부하 요소는 속도와 크기 간의 최상의 균형입니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

개체에는 IEqualityComparer 해시 코드 공급자와 비교자가 모두 포함됩니다. IEqualityComparer 가 생성자에서 Hashtable 사용되는 경우 개체의 키 Hashtable 로 사용되는 개체는 및 Object.Equals 메서드를 재정의 Object.GetHashCode 할 필요가 없습니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

IEqualityComparer 에서는 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

Hashtable 의 요소는 열거자가 개체를 반복하는 순서와 동일한 순서로 IDictionary 정렬됩니다.

이 생성자는 연산입니다 O(n) . 여기서 n 는 매개 변수의 요소 d 수입니다.

추가 정보

적용 대상

Hashtable(IDictionary, IHashCodeProvider, IComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

주의

Please use Hashtable(IDictionary, IEqualityComparer) instead.

주의

This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 기본 로드 비율과 지정된 해시 코드 공급자 및 비교자를 사용합니다. 이 API는 더 이상 사용되지 않습니다. 다른 방법을 보려면 Hashtable(IDictionary, IEqualityComparer)를 참조하십시오.

public:
 Hashtable(System::Collections::IDictionary ^ d, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IDictionary, IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, hcp As IHashCodeProvider, comparer As IComparer)

매개 변수

d
IDictionary

IDictionary 개체로 복사할 Hashtable 개체입니다.

hcp
IHashCodeProvider

IHashCodeProvider의 모든 키에 해시 코드를 제공하는 Hashtable 개체입니다.

또는

각 키의 GetHashCode() 구현인 기본 해시 코드 공급자를 사용하면 null입니다.

comparer
IComparer

두 키가 같은지 여부를 확인하는 데 사용할 IComparer 개체입니다.

또는

각 키로 Equals(Object)를 구현한 기본 비교자를 사용하면 null입니다.

특성

예외

d이(가) null인 경우

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create the dictionary.
        SortedList^ mySL = gcnew SortedList();
        mySL->Add("FIRST", "Hello");
        mySL->Add("SECOND", "World");
        mySL->Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
    }
};

int main()
{
    SamplesHashtable::Main();
};

/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        var mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        var myHT2 = new Hashtable(mySL, new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL)

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량은 원본 사전의 요소 수로 설정됩니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

사용자 지정 해시 코드 공급자 및 사용자 지정 비교자를 사용하면 대/소문자를 구분하지 않는 문자열로 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

Hashtable 의 요소는 열거자가 개체를 반복하는 순서와 동일한 순서로 IDictionary 정렬됩니다.

이 생성자는 연산입니다 O(n) . 여기서 n 는 매개 변수의 요소 d 수입니다.

추가 정보

적용 대상

Hashtable(Int32, Single)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정된 초기 용량 및 로드 비율과 기본 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(int capacity, float loadFactor);
public Hashtable (int capacity, float loadFactor);
new System.Collections.Hashtable : int * single -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single)

매개 변수

capacity
Int32

Hashtable 개체가 처음에 포함할 수 있는 대략적인 요소 수입니다.

loadFactor
Single

최고의 성능을 제공하는 기본값으로 곱한 0.1에서 1.0 사이의 수입니다. 결과는 버킷과 요소의 최대 비율입니다.

예외

capacity가 0보다 작은 경우

또는

loadFactor가 0.1보다 작습니다.

또는

loadFactor가 1.0보다 큽니다.

capacity가 오버플로를 일으킬 수 있습니다.

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3, .8f);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));

        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer())

        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer(myCul))

        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

    End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량을 지정하면 개체에 요소를 Hashtable 추가하는 동안 여러 크기 조정 작업을 수행할 필요가 없습니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다. 1.0의 부하 요소는 속도와 크기 간의 최상의 균형입니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

이 생성자는 작업이며 O(n) 여기서 n 는 매개 변수입니다 capacity .

추가 정보

적용 대상

Hashtable(SerializationInfo, StreamingContext)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

주의

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

지정된 HashtableSerializationInfo 개체를 사용하여 serialize할 수 있는 StreamingContext 클래스의 비어 있는 새 인스턴스를 초기화합니다.

protected:
 Hashtable(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected Hashtable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected Hashtable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
Protected Sub New (info As SerializationInfo, context As StreamingContext)

매개 변수

info
SerializationInfo

SerializationInfo 개체를 serialize하는 데 필요한 정보가 들어 있는 Hashtable 개체입니다.

context
StreamingContext

StreamingContext와 관련하여 serialize된 스트림의 소스와 대상이 포함된 Hashtable개체입니다.

특성

예외

infonull입니다.

설명

해시 테이블의 용량은 부하 요인에 따라 최적의 해시 테이블 버킷 수를 계산하는 데 사용됩니다. 필요에 따라 용량이 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

이 생성자는 작업입니다 O(n) . 여기서 n 는 입니다 Count.

에 대한 Hashtable 열거자를 직렬화 및 역직렬화하면 요소의 순서가 다시 지정될 수 있으므로 메서드를 호출 Reset 하지 않고는 열거를 계속할 수 없습니다.

추가 정보

적용 대상

Hashtable(IHashCodeProvider, IComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

주의

Please use Hashtable(IEqualityComparer) instead.

주의

This constructor has been deprecated. Use Hashtable(IEqualityComparer).

주의

This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.

기본 초기 용량 및 로드 비율과 지정된 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")]
public Hashtable (System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider? hcp, System.Collections.IComparer? comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
public Hashtable (System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[<System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer).")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
[<System.Obsolete("This constructor has been deprecated. Use Hashtable(IEqualityComparer) instead.")>]
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (hcp As IHashCodeProvider, comparer As IComparer)

매개 변수

hcp
IHashCodeProvider

IHashCodeProvider 개체의 모든 키에 대한 해시 코드를 제공하는 Hashtable 개체입니다.

또는

각 키의 GetHashCode() 구현인 기본 해시 코드 공급자를 사용하면 null입니다.

comparer
IComparer

두 키가 같은지 여부를 확인하는 데 사용할 IComparer 개체입니다.

또는

각 키로 Equals(Object)를 구현한 기본 비교자를 사용하면 null입니다.

특성

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myComparer : IEqualityComparer
{
public:
    virtual bool Equals(Object^ x, Object^ y) 
    {
        return x->Equals(y);
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

int main()
{
   
   // Create a hash table using the default hash code provider and the default comparer.
   Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
   myHT1->Add( "FIRST", "Hello" );
   myHT1->Add( "SECOND", "World" );
   myHT1->Add( "THIRD", "!" );
   
   // Create a hash table using the specified IEqualityComparer that uses
   // the default Object.Equals to determine equality.
   Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
   myHT2->Add( "FIRST", "Hello" );
   myHT2->Add( "SECOND", "World" );
   myHT2->Add( "THIRD", "!" );
   
   // Create a hash table using a case-insensitive hash code provider and
   // case-insensitive comparer based on the InvariantCulture.
   Hashtable^ myHT3 = gcnew Hashtable(
            CaseInsensitiveHashCodeProvider::DefaultInvariant,
            CaseInsensitiveComparer::DefaultInvariant);
   myHT3->Add( "FIRST", "Hello" );
   myHT3->Add( "SECOND", "World" );
   myHT3->Add( "THIRD", "!" );
   
   // Create a hash table using an IEqualityComparer that is based on
   // the Turkish culture (tr-TR) where "I" is not the uppercase
   // version of "i".
   CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
   Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
   myHT4->Add( "FIRST", "Hello" );
   myHT4->Add( "SECOND", "World" );
   myHT4->Add( "THIRD", "!" );
   
   // Search for a key in each hash table.
   Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
using System;
using System.Collections;
using System.Globalization;

class myComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable();
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the default Object.Equals to determine equality.
        var myHT2 = new Hashtable(new myComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using a case-insensitive hash code provider and
        // case-insensitive comparer based on the InvariantCulture.
        Hashtable myHT3 = new Hashtable(
            CaseInsensitiveHashCodeProvider.DefaultInvariant,
            CaseInsensitiveComparer.DefaultInvariant);
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");
        myHT4.Add("SECOND", "World");
        myHT4.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myComparer
    Implements IEqualityComparer
    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return x.Equals(y)
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode

        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable()
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the default Object.Equals to determine equality.
        Dim myHT2 As New Hashtable(New myComparer())
        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using a case-insensitive hash code provider and
        ' case-insensitive comparer based on the InvariantCulture.
        Dim myHT3 As New Hashtable( _
            CaseInsensitiveHashCodeProvider.DefaultInvariant, _
            CaseInsensitiveComparer.DefaultInvariant)
        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
        myHT4.Add("FIRST", "Hello")
        myHT4.Add("SECOND", "World")
        myHT4.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")

    End Sub

End Class

'This code produces the following output.
'Results vary depending on the system's culture settings.

'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False

설명

해시 테이블의 용량은 부하 요인에 따라 최적의 해시 테이블 버킷 수를 계산하는 데 사용됩니다. 필요에 따라 용량이 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

사용자 지정 해시 코드 공급자 및 사용자 지정 비교자를 사용하면 대/소문자를 구분하지 않는 문자열로 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

이 생성자는 작업입니다 O(1) .

추가 정보

적용 대상

Hashtable(IDictionary, Single)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 지정된 로드 비율과 기본 해시 코드 공급자 및 비교자를 사용합니다.

public:
 Hashtable(System::Collections::IDictionary ^ d, float loadFactor);
public Hashtable (System.Collections.IDictionary d, float loadFactor);
new System.Collections.Hashtable : System.Collections.IDictionary * single -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single)

매개 변수

d
IDictionary

IDictionary 개체로 복사할 Hashtable 개체입니다.

loadFactor
Single

최고의 성능을 제공하는 기본값으로 곱한 0.1에서 1.0 사이의 수입니다. 결과는 버킷과 요소의 최대 비율입니다.

예외

d이(가) null인 경우

loadFactor가 0.1보다 작습니다.

또는

loadFactor가 1.0보다 큽니다.

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
       
       // Create the dictionary.
       SortedList^ mySL = gcnew SortedList;
       mySL->Add( "FIRST", "Hello" );
       mySL->Add( "SECOND", "World" );
       mySL->Add( "THIRD", "!" );
       
       // Create a hash table using the default hash code provider and the default comparer.
       Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
       
       // Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
       Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
       
       // Create a hash table using the specified KeyComparer.
       // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
       // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
       CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
       Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
       
       // Search for a key in each hash table.
       Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(mySL, .8f);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(mySL, .8f,
            new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량은 원본 사전의 요소 수로 설정됩니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다. 1.0의 부하 요소는 속도와 크기 간의 최상의 균형입니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

Hashtable 의 요소는 열거자가 개체를 반복하는 순서와 동일한 순서로 IDictionary 정렬됩니다.

이 생성자는 연산입니다 O(n) . 여기서 n 는 매개 변수의 요소 d 수입니다.

추가 정보

적용 대상

Hashtable(IDictionary, IEqualityComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정된 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 기본 로드 비율과 지정된 IEqualityComparer 개체를 사용합니다.

public:
 Hashtable(System::Collections::IDictionary ^ d, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, equalityComparer As IEqualityComparer)

매개 변수

d
IDictionary

IDictionary 개체로 복사할 Hashtable 개체입니다.

equalityComparer
IEqualityComparer

IEqualityComparer과 함께 사용할 해시 코드 공급자와 비교자를 정의하는 Hashtable 개체입니다.

또는

기본 해시 코드 공급자와 기본 비교자를 사용하면 null입니다. 기본 해시 코드 공급자는 각 키의 GetHashCode() 구현이며 기본 비교자는 각 키의 Equals(Object) 구현입니다.

예외

d이(가) null인 경우

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create the dictionary.
        SortedList^ mySL = gcnew SortedList();
        mySL->Add("FIRST", "Hello");
        mySL->Add("SECOND", "World");
        mySL->Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
    }
};

int main()
{
    SamplesHashtable::Main();
};

/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        var mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        var myHT2 = new Hashtable(mySL, new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL)

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량은 원본 사전의 요소 수로 설정됩니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

개체에는 IEqualityComparer 해시 코드 공급자와 비교자가 모두 포함됩니다. IEqualityComparer 가 생성자에서 Hashtable 사용되는 경우 개체의 키 Hashtable 로 사용되는 개체는 및 Object.Equals 메서드를 재정의 Object.GetHashCode 할 필요가 없습니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

IEqualityComparer 에서는 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

Hashtable 의 요소는 열거자가 개체를 반복하는 순서와 동일한 순서로 IDictionary 정렬됩니다.

이 생성자는 연산입니다 O(n) . 여기서 n 는 매개 변수의 요소 d 수입니다.

추가 정보

적용 대상

Hashtable(Int32)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정된 초기 용량과 기본 로드 비율, 해시 코드 공급자 및 비교자를 사용하여 Hashtable 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(int capacity);
public Hashtable (int capacity);
new System.Collections.Hashtable : int -> System.Collections.Hashtable
Public Sub New (capacity As Integer)

매개 변수

capacity
Int32

Hashtable 개체가 처음에 포함할 수 있는 대략적인 요소 수입니다.

예외

capacity가 0보다 작은 경우

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(3)
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(3, New myCultureComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량을 지정하면 개체에 요소를 Hashtable 추가하는 동안 여러 크기 조정 작업을 수행할 필요가 없습니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

이 생성자는 작업입니다 O(n) . 여기서 n 는 입니다 capacity.

추가 정보

적용 대상

Hashtable(IEqualityComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

기본 초기 용량 및 로드 비율과 지정된 Hashtable 개체를 사용하여 IEqualityComparer 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IEqualityComparer equalityComparer);
public Hashtable (System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (equalityComparer As IEqualityComparer)

매개 변수

equalityComparer
IEqualityComparer

IEqualityComparer 개체와 함께 사용할 해시 코드 공급자와 비교자를 정의하는 Hashtable 개체입니다.

또는

기본 해시 코드 공급자와 기본 비교자를 사용하면 null입니다. 기본 해시 코드 공급자는 각 키의 GetHashCode() 구현이며 기본 비교자는 각 키의 Equals(Object) 구현입니다.

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myComparer : IEqualityComparer
{
public:
    virtual bool Equals(Object^ x, Object^ y) 
    {
        return x->Equals(y);
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

int main()
{
   
   // Create a hash table using the default hash code provider and the default comparer.
   Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
   myHT1->Add( "FIRST", "Hello" );
   myHT1->Add( "SECOND", "World" );
   myHT1->Add( "THIRD", "!" );
   
   // Create a hash table using the specified IEqualityComparer that uses
   // the default Object.Equals to determine equality.
   Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
   myHT2->Add( "FIRST", "Hello" );
   myHT2->Add( "SECOND", "World" );
   myHT2->Add( "THIRD", "!" );
   
   // Create a hash table using a case-insensitive hash code provider and
   // case-insensitive comparer based on the InvariantCulture.
   Hashtable^ myHT3 = gcnew Hashtable(
            CaseInsensitiveHashCodeProvider::DefaultInvariant,
            CaseInsensitiveComparer::DefaultInvariant);
   myHT3->Add( "FIRST", "Hello" );
   myHT3->Add( "SECOND", "World" );
   myHT3->Add( "THIRD", "!" );
   
   // Create a hash table using an IEqualityComparer that is based on
   // the Turkish culture (tr-TR) where "I" is not the uppercase
   // version of "i".
   CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
   Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
   myHT4->Add( "FIRST", "Hello" );
   myHT4->Add( "SECOND", "World" );
   myHT4->Add( "THIRD", "!" );
   
   // Search for a key in each hash table.
   Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
using System;
using System.Collections;
using System.Globalization;

class myComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable();
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the default Object.Equals to determine equality.
        var myHT2 = new Hashtable(new myComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using a case-insensitive hash code provider and
        // case-insensitive comparer based on the InvariantCulture.
        Hashtable myHT3 = new Hashtable(
            CaseInsensitiveHashCodeProvider.DefaultInvariant,
            CaseInsensitiveComparer.DefaultInvariant);
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");
        myHT4.Add("SECOND", "World");
        myHT4.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myComparer
    Implements IEqualityComparer
    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return x.Equals(y)
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode

        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable()
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the default Object.Equals to determine equality.
        Dim myHT2 As New Hashtable(New myComparer())
        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using a case-insensitive hash code provider and
        ' case-insensitive comparer based on the InvariantCulture.
        Dim myHT3 As New Hashtable( _
            CaseInsensitiveHashCodeProvider.DefaultInvariant, _
            CaseInsensitiveComparer.DefaultInvariant)
        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
        myHT4.Add("FIRST", "Hello")
        myHT4.Add("SECOND", "World")
        myHT4.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")

    End Sub

End Class

'This code produces the following output.
'Results vary depending on the system's culture settings.

'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False

설명

해시 테이블의 용량은 부하 요인에 따라 최적의 해시 테이블 버킷 수를 계산하는 데 사용됩니다. 필요에 따라 용량이 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

개체에는 IEqualityComparer 해시 코드 공급자와 비교자가 모두 포함됩니다. IEqualityComparer 가 생성자에서 Hashtable 사용되는 경우 개체의 키 Hashtable 로 사용되는 개체는 및 Object.Equals 메서드를 재정의 Object.GetHashCode 할 필요가 없습니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

IEqualityComparer 에서는 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

이 생성자는 작업입니다 O(1) .

추가 정보

적용 대상

Hashtable(IDictionary)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정한 사전의 요소를 새 Hashtable 개체에 복사하여 Hashtable 클래스의 새 인스턴스를 초기화합니다. 새 Hashtable 개체는 복사된 요소 수와 같은 초기 용량을 갖고 있고 기본 로드 비율, 해시 코드 공급자 및 비교자를 사용합니다.

public:
 Hashtable(System::Collections::IDictionary ^ d);
public Hashtable (System.Collections.IDictionary d);
new System.Collections.Hashtable : System.Collections.IDictionary -> System.Collections.Hashtable
Public Sub New (d As IDictionary)

매개 변수

d
IDictionary

IDictionary 개체로 복사할 Hashtable 개체입니다.

예외

d이(가) null인 경우

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create the dictionary.
        SortedList^ mySL = gcnew SortedList();
        mySL->Add("FIRST", "Hello");
        mySL->Add("SECOND", "World");
        mySL->Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
    }
};

int main()
{
    SamplesHashtable::Main();
};

/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        var mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        var myHT2 = new Hashtable(mySL, new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL)

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량은 원본 사전의 요소 수로 설정됩니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 부하 요소가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

해시 코드 공급자는 개체의 키 Hashtable 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

Hashtable 의 요소는 열거자가 개체를 반복하는 순서와 동일한 순서로 IDictionary 정렬됩니다.

이 생성자는 연산입니다 O(n) . 여기서 n 는 매개 변수의 요소 d 수입니다.

추가 정보

적용 대상

Hashtable(Int32, IEqualityComparer)

Source:
Hashtable.cs
Source:
Hashtable.cs
Source:
Hashtable.cs

지정된 초기 용량 및 Hashtable와 기본 로드 비율을 사용하여 IEqualityComparer 클래스의 비어 있는 새 인스턴스를 초기화합니다.

public:
 Hashtable(int capacity, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (int capacity, System.Collections.IEqualityComparer equalityComparer);
public Hashtable (int capacity, System.Collections.IEqualityComparer? equalityComparer);
new System.Collections.Hashtable : int * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, equalityComparer As IEqualityComparer)

매개 변수

capacity
Int32

Hashtable 개체가 처음에 포함할 수 있는 대략적인 요소 수입니다.

equalityComparer
IEqualityComparer

IEqualityComparer과 함께 사용할 해시 코드 공급자와 비교자를 정의하는 Hashtable 개체입니다.

또는

기본 해시 코드 공급자와 기본 비교자를 사용하면 null입니다. 기본 해시 코드 공급자는 각 키의 GetHashCode() 구현이며 기본 비교자는 각 키의 Equals(Object) 구현입니다.

예외

capacity가 0보다 작은 경우

예제

다음 코드 예제에서는 다른 Hashtable 생성자를 사용 하 여 해시 테이블을 만들고 해시 테이블의 동작에 차이 보여 줍니다., 각 동일한 요소를 포함 하는 경우에.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/*
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(3)
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(3, New myCultureComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

설명

초기 용량을 지정하면 개체에 요소를 Hashtable 추가하는 동안 여러 크기 조정 작업을 수행할 필요가 없습니다. 용량은 부하 요인에 따라 필요에 따라 자동으로 증가합니다.

부하 요소는 버킷에 대한 요소의 최대 비율입니다. 로드 팩터가 작을수록 메모리 사용량이 증가하는 비용을 더 빠르게 조회할 수 있습니다.

실제 로드 팩터가 지정된 부하 계수에 도달하면 버킷 수가 현재 버킷 수의 두 배보다 큰 가장 작은 소수로 자동으로 증가합니다.

개체에는 IEqualityComparer 해시 코드 공급자와 비교자가 모두 포함됩니다. IEqualityComparer 가 생성자에서 Hashtable 사용되는 경우 의 키 Hashtable 로 사용되는 개체는 및 Object.Equals 메서드를 재정의 Object.GetHashCode 할 필요가 없습니다.

해시 코드 공급자는 의 키 Hashtable에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 키의 구현입니다 Object.GetHashCode.

비교자는 두 키가 같은지 여부를 결정합니다. 의 모든 키는 Hashtable 고유해야 합니다. 기본 비교자는 키의 구현입니다 Object.Equals.

IEqualityComparer 에서는 대/소문자를 구분하지 않는 문자열을 사용하여 조회를 수행하는 등의 시나리오를 사용할 수 있습니다.

이 생성자는 작업이며 O(n) 여기서 n 는 매개 변수입니다 capacity .

추가 정보

적용 대상