HashSet<T>.TrimExcess 메서드

정의

오버로드

TrimExcess()

HashSet<T> 개체의 용량을 실제로 포함된 요소 수로 설정하고 구현별로 다른 근방 값으로 올림합니다.

TrimExcess(Int32)

TrimExcess()

Source:
HashSet.cs
Source:
HashSet.cs
Source:
HashSet.cs

HashSet<T> 개체의 용량을 실제로 포함된 요소 수로 설정하고 구현별로 다른 근방 값으로 올림합니다.

public:
 void TrimExcess();
public void TrimExcess ();
member this.TrimExcess : unit -> unit
Public Sub TrimExcess ()

예제

다음 예제에서는 컬렉션을 만들고 채웁 HashSet<T> 니다. 그런 다음 컬렉션을 지우고 참조하는 메모리를 해제합니다.

HashSet<int> Numbers = new HashSet<int>();

for (int i = 0; i < 10; i++)
{
    Numbers.Add(i);
}

Console.Write("Numbers contains {0} elements: ", Numbers.Count);
DisplaySet(Numbers);

Numbers.Clear();
Numbers.TrimExcess();

Console.Write("Numbers contains {0} elements: ", Numbers.Count);
DisplaySet(Numbers);

void DisplaySet(HashSet<int> set)
{
    Console.Write("{");
    foreach (int i in set)
    {
        Console.Write(" {0}", i);
    }
    Console.WriteLine(" }");
}

/* This example produces output similar to the following:
* Numbers contains 10 elements: { 0 1 2 3 4 5 6 7 8 9 }
* Numbers contains 0 elements: { }
*/
Imports System.Collections.Generic

Class Program

    Shared Sub Main()

        Dim Numbers As HashSet(Of Integer) = New HashSet(Of Integer)()

        For i As Integer = 0 To 9
            Numbers.Add(i)
        Next i

        Console.Write("Numbers contains {0} elements: ", Numbers.Count)
        DisplaySet(Numbers)

        Numbers.Clear()
        Numbers.TrimExcess()

        Console.Write("Numbers contains {0} elements: ", Numbers.Count)
        DisplaySet(Numbers)

    End Sub
    ' This code example produces output similar to the following:
    ' Numbers contains 10 elements: { 0 1 2 3 4 5 6 7 8 9 }
    ' Numbers contains 0 elements: { }

    Private Shared Sub DisplaySet(ByVal coll As HashSet(Of Integer))
        Console.Write("{")
        For Each i As Integer In coll
            Console.Write(" {0}", i)
        Next i
        Console.WriteLine(" }")
    End Sub

End Class

설명

새 요소가 추가되지 않을 것으로 알려지면 메서드를 HashSet<T> 사용하여 TrimExcess 개체의 메모리 오버헤드를 최소화할 수 있습니다. 개체를 완전히 지우고 참조하는 HashSet<T> 모든 메모리를 해제하려면 메서드를 호출한 후 이 메서드를 Clear 호출합니다.

이 메서드는 O (n) 작업, 여기서 nCount합니다.

적용 대상

TrimExcess(Int32)

Source:
HashSet.cs
public:
 void TrimExcess(int capacity);
public void TrimExcess (int capacity);
member this.TrimExcess : int -> unit
Public Sub TrimExcess (capacity As Integer)

매개 변수

capacity
Int32

적용 대상