您的位置:首页 > 编程语言 > C#

C# HashSet类常用函数

2015-09-21 16:28 751 查看


C# HashSet类常用函数

初始化

static HashSet<int> INT_HASHSET_1 = new HashSet<int>()
{
1,2,3,4,5
};
static HashSet<int> INT_HASHSET_2 = new HashSet<int>()
{
2,4,6,8,10
};


bool Add(T item)

如果该元素添加到 System.Collections.Generic.HashSet<T> 对象中则为 true;如果该元素已存在则为 false。

bool isHave5 = INT_HASHSET_1.Add(5);
bool isHave6 = INT_HASHSET_1.Add(6);
Console.WriteLine("isHave5 = " + isHave5 + "isHave6 = "+ isHave6);
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}



bool Remove(T item)

从 System.Collections.Generic.HashSet<T> 对象中移除某个元素。
bool isRemove5 = INT_HASHSET_1.Remove(5);
bool isRemove6 = INT_HASHSET_1.Remove(6);
Console.WriteLine("isRemove5 = " + isRemove5 + "isRemove6 = " + isRemove6);
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}



void Clear()

从 System.Collections.Generic.HashSet<T> 对象中移除所有元素。
INT_HASHSET_1.Clear();
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}


控制台无输出

bool Contains(T item)

如果 System.Collections.Generic.HashSet<T> 对象包含指定的元素,则为 true;否则为 false

bool isHave5 = INT_HASHSET_1.Contains(5);
Console.WriteLine("isHave5 = " + isHave5);
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}


控制台输出 : isHave5 = Ture

void CopyTo(T[] array)

作为从 System.Collections.Generic.HashSet<T> 对象复制的元素的目标的一维数组。 该数组的索引必须从零开始。

int[] copyArray = new int[INT_HASHSET_1.Count];
INT_HASHSET_1.CopyTo(copyArray);
foreach (var team in copyArray)
{
Console.WriteLine(team);
}



void CopyTo(T[] array, int arrayIndex)

从指定数组索引处开始,将 System.Collections.Generic.HashSet<T> 对象的元素复制到数组中。

int index = 2;
int[] copyArray = new int[INT_HASHSET_1.Count + index];
INT_HASHSET_1.CopyTo(copyArray, index);
foreach (var team in copyArray)
{
Console.WriteLine(team);
}




void CopyTo(T[] array, int arrayIndex, int count)

从指定数组索引处开始,将 System.Collections.Generic.HashSet<T> 对象的指定数目的元素复制到数组中。

int index = 2, length = 3;
int[] copyArray = new int[INT_HASHSET_1.Count];
INT_HASHSET_1.CopyTo(copyArray, index, length);
foreach (var team in copyArray)
{
Console.WriteLine(team);
}



void UnionWith(IEnumerable<T> other)

A.UnionWith(B) 返回两个集合的合集 修改 A 为A B合集运算后的集合

INT_HASHSET_1.UnionWith(INT_HASHSET_2);
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}



void IntersectWith(IEnumerable<T> other)

A.IntersectWith(B) 返回两个集合的交集 修改 A 为A B交集运算后的集合

INT_HASHSET_1.IntersectWith(INT_HASHSET_2);
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}



void ExceptWith(IEnumerable<T> other)

A.ExceptWith(B) 返回两个集合的差集 修改 A 为A B差集运算后的集合

INT_HASHSET_1.ExceptWith(INT_HASHSET_2);
foreach (var team in INT_HASHSET_1)
{
Console.WriteLine(team);
}



bool Overlaps(IEnumerable<T> other)

Overlaps 对象和指定的集合是否共享常见元素(只要包含其中一个即返回true)。
HashSet<int> INT_HASHSET_3 = new HashSet<int>()
{
1,10
};
bool isOverlaps = INT_HASHSET_1.Overlaps(INT_HASHSET_3);
Console.WriteLine("isOverlaps = " + isOverlaps);


控制台输出 :isOverlaps = True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: