विधेय द्वारा परिभाषित शर्तों के साथ हैशसेट से तत्वों को हटाने के लिए, कोड इस प्रकार है -
उदाहरण
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