HashSet<T>.Remove(T) 메서드

정의

HashSet<T> 개체에서 지정된 요소를 제거합니다.Removes the specified element from a HashSet<T> object.

public:
 virtual bool Remove(T item);
public bool Remove (T item);
abstract member Remove : 'T -> bool
override this.Remove : 'T -> bool
Public Function Remove (item As T) As Boolean

매개 변수

item
T

제거할 요소입니다.The element to remove.

반환

요소를 성공적으로 찾아서 제거한 경우 true이고, 그렇지 않으면 false입니다.true if the element is successfully found and removed; otherwise, false. HashSet<T> 개체에 item이 없으면 이 메서드는 false를 반환합니다.This method returns false if item is not found in the HashSet<T> object.

구현

예제

다음 예제에서 값을 제거 하는 방법에 설명 된 HashSet<T> 사용 하 여 컬렉션을 Remove 메서드.The following example demonstrates how to remove values from a HashSet<T> collection using the Remove method. 이 예제에서는 0 임의로에서 제거 됩니다는 HashSet<T> 컬렉션입니다.In this example, zero is arbitrarily removed from the HashSet<T> collection.

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

for (int i = 0; i < 20; i++) {
    numbers.Add(i);
}

// Display all the numbers in the hash table.
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

// Remove all odd numbers.
numbers.RemoveWhere(IsOdd);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

// Check if the hash table contains 0 and, if so, remove it.
if (numbers.Contains(0)) {
    numbers.Remove(0);
}
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

bool IsOdd(int i)
{
    return ((i % 2) == 1);
}

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

    Console.WriteLine(" }");
}

// This example display the following output:
//    numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
//    numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
//    numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
Imports System.Collections.Generic

Module Example
    Public Sub Main()
        Dim numbers As New HashSet(Of Integer)()

        For i As Integer = 0 To 19
            numbers.Add(i)
        Next i

        ' Display all the numbers in the hash table.
        Console.Write("numbers contains {0} elements: ", numbers.Count)
        DisplaySet(numbers)

        ' Remove all odd numbers.
        numbers.RemoveWhere(AddressOf IsOdd)
        Console.Write("numbers contains {0} elements: ", numbers.Count)
        DisplaySet(numbers)

        ' Check if the hash table contains 0 and, if so, remove it.
        If numbers.Contains(0) Then
            numbers.Remove(0)
        End If
        Console.Write("numbers contains {0} elements: ", numbers.Count)
        DisplaySet(numbers)
    End Sub

    Private Function IsOdd(ByVal i As Integer) As Boolean
        Return ((i Mod 2) = 1)
    End Function

    Private Sub DisplaySet(ByVal coll As HashSet(Of Integer))
        Console.Write("{")
        For Each i As Integer In coll
            Console.Write(" {0}", i)
        Next
        Console.WriteLine(" }")
    End Sub
End Module
' The example displays the following output:
'    numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
'    numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
'    numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }

설명

경우는 HashSet<T> 개체는 지정된 된 요소를 포함 하지 않습니다, 개체 변경 되지 않습니다.If the HashSet<T> object does not contain the specified element, the object remains unchanged. 예외가 throw되지 않습니다.No exception is thrown.

이 방법은 O(1) 작업에 설명 합니다.This method is an O(1) operation.

적용 대상