/*
集(Set)
.Net4包含两个HashSet 和 SortedSet
HashSet包含不重复元素的无序列表,SortedSet包含不重复元素的有序集合
*/
using System.Collections.Generic;
namespace Frank
{
public class Test
{
//程序入口
public static void Main(string[] args)
{
HashSet<string> hs1 = new HashSet<string>();
hs1.Add("wuhan");
System.Console.WriteLine(hs1.Add("wuhan"));//Add方法返回一个bool值,判断是否添加成功 返回false,因为里面有相同的值了。
hs1.Add("beijing");
hs1.Add("tianjing");
HashSet<string> hs2 = new HashSet<string>();
hs2.Add("wuhan");
hs2.Add("henan");
HashSet<string> hs3 = new HashSet<string>();
hs3.Add("anhui");
hs3.Add("shijiazhuang");
System.Console.WriteLine(hs1.IsSubsetOf(hs2));//验证hs1集合中的每个元素是否都包含在hs2中 输出false
System.Console.WriteLine(hs2.IsSupersetOf(hs1));//验证hs1集合是否有与hs1集合比较的额外元素
System.Console.WriteLine("是否有共享元素:"+hs1.Overlaps(hs2));//是否有共享元素
SortedSet<string> ss = new SortedSet<string>(hs3);//创建一个有序集,并把hs3集填充进去,集合里面元素是唯一的。重复的将不会添加进去
ss.UnionWith(hs2);
ss.UnionWith(hs1);
foreach(string item in ss)
{
System.Console.WriteLine(item);//输出排序好的
}
ss.ExceptWith(hs3);//从ss中移除hs3的中的元素
foreach(string item in ss)
{
System.Console.WriteLine(item);//输出排序好的
}
}
}
}