술어에 의해 정의된 조건으로 HashSet에서 요소를 제거하려면 코드는 다음과 같습니다. -
예시
using System;
using System.Collections.Generic;
public class Demo {
private static bool demo(int i) {
return (i == 100);
}
public static void Main(String[] args) {
HashSet<int> list = new HashSet<int>();
list.Add(100);
list.Add(300);
list.Add(400);
list.Add(500);
list.Add(600);
Console.WriteLine("HashSet elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine(" ");
list.RemoveWhere(demo);
Console.WriteLine("HashSet after removing element 100...");
foreach (int i in list) {
Console.WriteLine(i);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
HashSet elements... 100 300 400 500 600 HashSet after removing element 100... 300 400 500 600
예시
다른 예를 살펴보겠습니다 -
using System;
using System.Collections.Generic;
public class Demo {
private static bool demo(int i) {
return ((i % 10) == 0);
}
public static void Main(String[] args) {
HashSet<int> list = new HashSet<int>();
list.Add(100);
list.Add(355);
list.Add(400);
list.Add(555);
list.Add(600);
Console.WriteLine("HashSet elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine(" ");
list.RemoveWhere(demo);
Console.WriteLine("HashSet after removing some elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
HashSet elements... 100 355 400 555 600 HashSet after removing some elements... 355 555