使用C#中的谓词定义的条件从HashSet中删除元素
要使用谓词定义的条件从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
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短