Freigeben über


Hashtable-Konstruktor (Int32, Single)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen anfänglichen Kapazität und des angegebenen Lastfaktors sowie des Hashcode-Standardanbieters und des Standardcomparers.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub New ( _
    capacity As Integer, _
    loadFactor As Single _
)
'Usage
Dim capacity As Integer
Dim loadFactor As Single

Dim instance As New Hashtable(capacity, loadFactor)
public Hashtable (
    int capacity,
    float loadFactor
)
public:
Hashtable (
    int capacity, 
    float loadFactor
)
public Hashtable (
    int capacity, 
    float loadFactor
)
public function Hashtable (
    capacity : int, 
    loadFactor : float
)

Parameter

  • capacity
    Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.
  • loadFactor
    Eine Zahl im Bereich 0.1 bis 1.0, die mit dem Standardwert für die optimale Leistung multipliziert wird. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentOutOfRangeException

capacity ist kleiner als 0 (null).

– oder –

loadFactor ist kleiner als 0,1.

– oder –

loadFactor ist größer als 1,0.

Hinweise

Durch die Angabe der anfänglichen Kapazität entfallen einige Operationen zum Ändern der Größe, die sonst beim Hinzufügen von Elementen zum Hashtable-Objekt durchgeführt werden. Bei Bedarf wird die Kapazität entsprechend dem Lastfaktor automatisch erhöht.

Der Lastfaktor ist das maximale Verhältnis von Elementen zu Buckets. Ein kleinerer Lastfaktor führt zu einer schnelleren Suche bei höherem Speicherbedarf. Ein Lastfaktor von 1,0 stellt den besten Kompromiss zwischen Geschwindigkeit und Größe dar.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Anzahl der Buckets automatisch auf die kleinste Primzahl erhöht, die größer als die doppelte Anzahl der derzeit vorhandenen Buckets ist.

Der Hashcodeanbieter verteilt Hashcodes für Schlüssel in der Hashtable. Der Hashcode-Standardanbieter ist die Object.GetHashCode-Implementierung des Schlüssels.

Der Comparer ermittelt, ob zwei Schlüssel gleich sind. Jeder Schlüssel in einer Hashtable muss eindeutig sein. Der Standardcomparer ist die Object.Equals-Implementierung des Schlüssels.

Diese Methode ist eine O(n)-Operation, wobei n der capacity-Parameter ist.

Beispiel

Im folgenden Codebeispiel werden Hashtabellen mit verschiedenen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hashtabellen dargestellt, auch dann, wenn jede Tabelle dieselben Elemente enthält.

Imports System
Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

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

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

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

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

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

Public Class SamplesHashtable

    Public Shared Sub Main()

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

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

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

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

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

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

    End Sub 'Main 

End Class 'SamplesHashtable


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

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

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

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

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

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

public class SamplesHashtable
{

    public static void Main()
    {

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

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

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

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

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

    }

}


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

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

*/

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Hashtable-Klasse
Hashtable-Member
System.Collections-Namespace
Object.GetHashCode
Object.Equals