转载:C# HashSet 用法

原文地址: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

时间: 2024-08-10 00:05:50

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

[转载]typedef常见用法

注:本文系转载,并修改了一些错误. typedef常见用法 1.常规变量类型定义 例如:typedef unsigned char uchar描述:uchar等价于unsigned char类型定义 uchar c声明等于unsigned char c声明 2.数组类型定义例如: typedef int array[2];描述: array等价于 int [2]定义; array a声明等价于int a[2]声明 扩展: typedef int array[M][N];描述: array等价于

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

我用的版本是1.25的.每个版本用法有一点不同 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using NPOI.HS

[转载] boost thread用法

原文: http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html boost库中thread的用法官方文档写的不是特别清楚, 这篇文章给出了比较清晰的介绍和例子. Threading with Boost - Part I: Creating Threads May 13, 2009 Boost is an incredibly powerful collection of portable cl

【转载】AsyncTask用法

在开发Android应用时必须遵守单线程模型的原则: Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.在单线程模型中始终要记住两条法则: 1. 不要阻塞UI线程 2. 确保只在UI线程中访问Android UI工具包 当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分发到对应的组件进行处理.所以主线程通常又被叫做UI线程.

[转载]gate 的用法

1.gate descriptor 的 selector 给出目标代码的 code segment descriptor 2.由目标代码 code segment descriptor 的 base address 得出代码的 base 3.这个 base 加上 gate descriptor 的 offset 值,最终得到代码的入口点. 用 C 代码描述为: void do_call_with_gate(selector_t call_gate_descriptor) {         co

C#解leetcode 219. Contains Duplicate II

该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet 用法. 题目: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and t

网页词频统计工具

阅读英文文章时有时会出现不少这篇文章专有的一些单词,这些单词在其他地方不太可能会使用到,但是在阅读这篇文章时使用的频率可能会比较大,于是想能不能做一个工具,当你给定文章的url时,它将这篇文章中出现次数较多的那些单词统计出来.这样当你把这些单词的意义搞明白,再读这篇文章会不会压力小很多? 那么做这个工具的思路如下: 首先必须能够根据给定的url获取网页的正文信息或者网页的html文件: 如果第一步获取的是网页的html文件,那么需要根据这个html文件获取网页的正文信息: 网页的正文信息应该是包

C语言函数sscanf()的用法 (转载

在我的学习过程中,从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( string str, string fmt, mixed var1, mixed var2 ... ); int scanf( const char *format [,argument]... ); 说明: sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)

Java HashSet和LinkedHashSet的用法

Java HashSet和LinkedHashSet的用法 类HashSet和LinkedHashSet都是接口Set的实现,两者都不能保存重复的数据.主要区别是HashSet不保证集合中元素的顺序,即不能保证迭代的顺序与插入的顺序一致. 而LinkedHashSet按照元素插入的顺序进行迭代,即迭代输出的顺序与插入的顺序保持一致. 以下是HastSet和LinkedHashSet的用法示例: [java] view plain copy import java.util.Collections