原文链接

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

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

表  10-12

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

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

表  10-13

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

在示例代码中,创建了3个字符串类型的新集,并用一级方程式汽车填充。HashSet<T>类实现了ICollection<T>接口。但是在该类中,Add()方法是显式实现的,还提供了另一个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()验证traditionalTeams是否没有与companyTeams比较的额外元素。


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");
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. 转载:C# HashSet 用法

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

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

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

  3. 转载 HashSet用法

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

  4. HashSet代码分析

    HashSet (jdk 1.7)的继承关系如下: HashSet是使用HashMap实现的一个没有重复元素的集合.HashSet用法如下: HashSet<String> hashSet ...

  5. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  6. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

  7. Java高级教程02

    目录 1.Java线程 1.1. 多线程和多进程 1.2. 线程的执行过程: 1.3. 创建线程的方法 (1). 方法1:通过run() (2). 方法2: 复写Runnable接口(推荐) 1.4. ...

  8. Java HashSet和LinkedHashSet的用法

    Java HashSet和LinkedHashSet的用法 类HashSet和LinkedHashSet都是接口Set的实现,两者都不能保存重复的数据.主要区别是HashSet不保证集合中元素的顺序, ...

  9. HashMap,Hashset,ArrayList以及LinkedList集合的区别,以及各自的用法

    基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, Map ,数组.只有这四种容器. Collection(集合) 一个一个往里装,Map 一对一对往里装. ...

随机推荐

  1. android 编程小技巧(持续中)

    first:     Intent跳转一般存用于Activity类,可是若要在非activity类里跳转的话,解决方法是在startActivity(intent)前加mContext即上下文,终于为 ...

  2. Liunx之Lamp搭建笔记

    1:LAMP源代码搭建用户关系 a.  apache服务以daemon用户的处理请求.以root身份作为主进程. b. php源代码安装,会在httpd.conf文件里自己主动增加调用模块.可是在该文 ...

  3. UI类继承关系图

  4. Client should know only resource URIs and that’s all.

    REST Principles and Architectural Constraints – REST API Tutorial https://restfulapi.net/rest-archit ...

  5. poj 3017 Cut the Sequence(单调队列优化DP)

    Cut the Sequence \(solution:\) 这道题出的真的很好,奈何数据水啊! 这道题当时看得一脸懵逼,说二分也不像二分,说贪心也不像贪心,说搜索吧这题数据范围怎么这么大?而且这题看 ...

  6. RSA前端JS加密,后端JAVA解密实现

    用RSA非对称加密方式实现.后台生成rsa密钥对,然后在页面设置rsa公钥,提交时用公钥加密密码,生成的密文传到后台,后台再用私钥解密,获取密码明文.这样客户端只需要知道rsa加密方式和公钥,前台不知 ...

  7. opencv VS2010配置

    一.下载 opencv下载地址:http://www.opencv.org.cn/  点击下载栏 最新的可能有3.2了,但是支持的VS版本是VS2012等版本.这里只选用2.4.9版本 下载后就是安装 ...

  8. iOS中城市定位功能的实现

    引入框架:CoreLocation .h文件 引入CoreLocation/CoreLocation.h @interface WeatherViewController :UIViewControl ...

  9. yum下载对应内核版本的kernel-devel

    1 查看内核版本 uname -r 2 查看目前已有的kernel-devel uname -a ; rpm -qa kernel\* | sort 3 下载对应版本 $ sudo yum insta ...

  10. YTU 2457: 很简单的一道题

    2457: 很简单的一道题 时间限制: 1 Sec  内存限制: 128 MB 提交: 261  解决: 80 [提交][状态][讨论版] 题目描述 有一个简单的函数数学公式,如下 输入 重复输入多组 ...