.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
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/pretyjfh/archive/2010/08/08/5796493.aspx