HashSet是唯一元素的无序集合。在System.Collections.Generic命名空间中找到它。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。哈希集
句法:
mySet1.Overlaps(mySet2);
其中,mySet1和mySet2是HashSets。
返回类型:如果两个HashSet共享至少一个公共元素,则此方法返回true ,否则返回false 。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to Check if a HashSet and a
// specified collection share common elements
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a HashSet of strings
HashSet mySet1 = new HashSet();
// Inserting elements in HashSet
mySet1.Add("Geeks");
mySet1.Add("GeeksforGeeks");
mySet1.Add("GeeksClasses");
mySet1.Add("GeeksQuiz");
// Creating a HashSet of strings
HashSet mySet2 = new HashSet();
// Inserting elements in HashSet
mySet2.Add("DS");
mySet2.Add("C++");
mySet2.Add("Java");
mySet2.Add("JavaScript");
// Check if a HashSet and a specified
// collection share common elements
Console.WriteLine(mySet1.Overlaps(mySet2));
}
}
输出:
False
范例2:
// C# code to Check if a HashSet and a
// specified collection share common elements
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a HashSet of integers
HashSet mySet1 = new HashSet();
// Inserting elements in HashSet
mySet1.Add(4);
mySet1.Add(8);
mySet1.Add(12);
mySet1.Add(16);
// Creating a HashSet of integers
HashSet mySet2 = new HashSet();
// Inserting elements in HashSet
mySet2.Add(4);
mySet2.Add(8);
mySet2.Add(15);
mySet2.Add(20);
// Check if a HashSet and a specified
// collection share common elements
Console.WriteLine(mySet1.Overlaps(mySet2));
}
}
输出:
True
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.hashset-1.overlaps?view=netframework-4.7.2