原文地址:http://www.cnblogs.com/xiaopin/archive/2011/01/08/1930540.html   感谢博主分享!

NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>。这个集合类包含不重复项的无序列表。这种集合称为“集(set)”。集是一个保留字,所以该类有另一个名称HashSet<T>。这个名称很容易理解,因为这个集合基于散列值,插入元素的操作非常快,不需要像List<T>类那样重排集合。

HashSet<T>类提供的方法可以创建合集和交集。表1列出了改变集的值的方法。

表1

HashSet<T>的修改方法 说    明
Add() 如果某元素不在集合中,Add()方法就把该元素添加到集合中。在其返回值Boolean中,返回元素是否添加的信息
Clear() 方法Clear()删除集合中的所有元素
Remove() Remove()方法删除指定的元素
RemoveWhere() RemoveWhere()方法需要一个Predicate<T>委托作为参数。删除满足谓词条件的所有元素
CopyTo()   CopyTo()把集合中的元素复制到一个数组中
ExceptWith() ExceptWith()方法把一个集合作为参数,从集中删除该集合中的所有元素
IntersectWith() IntersectWith()修改了集,仅包含所传送的集合和集中都有的元素
UnionWith()   UnionWith()方法把传送为参数的集合中的所有元素添加到集中

表2列出了仅返回集的信息、不修改元素的方法。

HashSet<T>的验证方法 说明
Contains() 如果所传送的元素在集合中,方法Contains()就返回true
IsSubsetOf() 如果参数传送的集合是集的一个子集,方法IsSubsetOf()就返回true
IsSupersetOf() 如果参数传送的集合是集的一个超集,方法IsSupersetOf()就返回true
Overlaps() 如果参数传送的集合中至少有一个元素与集中的元素相同,Overlaps()就返回true
SetEquals() 如果参数传送的集合和集包含完全相同的元素,方法SetEquals()就返回true

在示例代码中,创建了3个字符串类型的新集,并用一级方程式汽车填充。HashSet<T>类实现了ICollection<T>接口。但是在该类中,Add()方法是显式实现的。Add()方法的区别是返回类型,它返回一个布尔值,说明是否添加了元素。如果该元素已经在集中,就不添加它,并返回false。

HashSet < string > companyTeams =new HashSet < string > (){ "Ferrari", "McLaren", "Toyota", "BMW","Renault", "Honda" };

HashSet < string > traditionalTeams =new HashSet < string > (){ "Ferrari", "McLaren" };

HashSet < string > privateTeams =new HashSet < string > (){ "Red Bull", "Toro Rosso", "Spyker","Super Aguri" };

if (privateTeams.Add("Williams"))
Console.WriteLine("Williams added");
if (!companyTeams.Add("McLaren"))
Console.WriteLine("McLaren was already in this set");

两个Add()方法的输出写到控制台上:

Williams added

McLaren was already in this set

方法IsSubsetOf()和IsSupersetOf()比较集和实现了IEnumerable<T>接口的集合,返回一个布尔结果。这里,IsSubsetOf()验证traditionalTeams中的每个元素是否都包含在companyTeams中,IsSupersetOf()验证companyTeams 是否是traditionalTeams的超集。

if (traditionalTeams.IsSubsetOf(companyTeams))
{
Console.WriteLine("traditionalTeams is " +"subset of companyTeams");
} if (companyTeams.IsSupersetOf(traditionalTeams))
{
Console.WriteLine("companyTeams is a superset of " +"traditionalTeams");
}

这个验证的结果如下:

traditionalTeams is a subset of companyTeams

companyTeams is a superset of traditionalTeams

Williams也是一个传统队,因此这个队添加到traditionalTeams集合中:

traditionalTeams.Add("Williams");//前面代码中privateTeams已经加入该元素
if (privateTeams.Overlaps(traditionalTeams))
{
Console.WriteLine("At least one team is " +"the same with the traditional " +"and privateteams");
}

这有一个重叠,所以结果如下:

At least one team is the same with the traditional and private teams.

调用UnionWith()方法,给变量allTeams填充了companyTeams、PrivateTeams和traditionalTeams的合集:

HashSet < string > allTeams =new HashSet < string > (companyTeams);
allTeams.UnionWith(privateTeams);
allTeams.UnionWith(traditionalTeams);
Console.WriteLine();
Console.WriteLine("all teams");
foreach (var team in allTeams)
{
Console.WriteLine(team);
}

这里返回所有的队,但每个队都只列出一次,因为集只包含唯一值:

Ferrari

McLaren

Toyota

BMW

Renault

Honda

Red Bull

Toro Rosso

Spyker

Super Aguri

Williams

方法ExceptWith()从allTeams集中删除所有的私人队:

allTeams.ExceptWith(privateTeams);
Console.WriteLine();
Console.WriteLine("no private team left");
foreach (var team in allTeams)
{
Console.WriteLine(team);
}

集合中的其他元素不包含私人队:

Ferrari

McLaren

Toyota

BMW

Renault

Honda

转载:C# HashSet 用法的更多相关文章

  1. 转载 HashSet用法

    NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>.这个集合类包含不重复项的无序列表.这种集合称为“集(set)”.集是 ...

  2. [转载] C++ typedef 用法详解

    typedef的语法描述 在现实生活中,信息的概念可能是长度,数量和面积等.在C语言中,信息被抽象为int.float和 double等基本数据类型.从基本数据类型名称上,不能够看出其所代表的物理属性 ...

  3. [转载]typedef常见用法

    注:本文系转载,并修改了一些错误. typedef常见用法 1.常规变量类型定义 例如:typedef unsigned char uchar描述:uchar等价于unsigned char类型定义 ...

  4. 转载 AutoFac常见用法总结

    第二节:框架前期准备篇之AutoFac常见用法总结   一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势 ...

  5. 转载 NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel

    我用的版本是1.25的.每个版本用法有一点不同 using System; using System.Collections.Generic; using System.ComponentModel; ...

  6. 【转载】Adapter用法总结大全

    下面的是看到的比较好的地址: Android各种Adapter的用法:                 http://my.oschina.net/u/658933/blog/372151 Andro ...

  7. (转载)http_build_query用法,挺方便的

    (转载)http://www.cnblogs.com/zhja/archive/2012/11/10/2764174.html http_build_query (PHP 5) http_build_ ...

  8. 刷题upupup【Java中HashMap、HashSet用法总结】

    HashMap: 常用操作 1. containsKey() 判断HashMap是否包含key 2. containsValue() 判断HashMap是否包含“值为value”的元素 3. get( ...

  9. [转载]关于generate用法的总结【Verilog】

    转载自http://www.cnblogs.com/nanoty/archive/2012/11/13/2768933.html Abtract generate语句允许细化时间(Elaboratio ...

随机推荐

  1. 张江在线APP演示

    app下载地址:https://itunes.apple.com/cn/app/zhang-jiang-zai-xian/id722630317?mt=8

  2. bzoj2180: 最小直径生成树

    Description 输入一个无向图G=(V,E),W(a,b)表示边(a,b)之间的长度,求一棵生成树T,使得T的直径最小.树的直径即树的最长链,即树上距离最远的两点之间路径长度. Input 输 ...

  3. ps的使用方法

    1.打开原图素材,Ctrl + J把背景图层复制一层,按Ctrl + Shift + U去色,执行:滤镜 > 模糊 > 高斯模糊,数值4,图层混合模式为滤色,图层不透明度改为27%. 2. ...

  4. Catharanthus roseus(长春花碱)的生物合成

    标题:Directed Biosynthesis of Alkaloid Analogs in the Medicinal Plant Catharanthus roseus 作者:Elizabeth ...

  5. [BZOJ 3209] 花神的数论题 【数位统计】

    题目链接: BZOJ - 3209 题目大意 设 f(x) 为 x 的二进制表示中 1 的个数.给定 n ,求 ∏ f(i)     (1 <= i <= n) . 题目分析 总体思路是枚 ...

  6. [转]Aggregate tasks i Sharepoint 2013

    from http://sharepoint247.com/mysite/aggregate-tasks-i-sharepoint-2013/ Aggregate tasks i Sharepoint ...

  7. c#分支语句;循环语句(随堂练习)

    1. 输入月份,日期号,输出是今年的第几天    平年,2月28天     switch (变量名) {case "": break} 2. 循环语句:    for(int i ...

  8. mysql 监控长事务

    mysql> desc information_schema.innodb_trx -> ; +----------------------------+----------------- ...

  9. 火狐浏览器对border-radius的渲染问题

  10. 将汉字转化为拼音,正则表达式和得到汉字的Unicode编码

    一:上图,不清楚的看代码注解,很详细了 二:具体代码 窗体代码 using System; using System.Collections.Generic; using System.Compone ...