C# HashSet类常用函数

来源:互联网 发布:tf家族 淘宝 高级会员 编辑:程序博客网 时间:2024/05/20 02:22

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


0 0
原创粉丝点击