📌  相关文章
📜  C#|检查HashSet是否是指定集合的正确超集

📅  最后修改于: 2021-05-29 20:17:00             🧑  作者: Mango

HashSet是唯一元素的无序集合。它位于System.Collections.Generic命名空间下。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。
哈希集.IsProperSupersetOf(IEnumerable ) 方法用于检查HashSet是否是指定集合的正确超集。

句法:

mySet1.IsProperSupersetOf(mySet2);

在这里, mySet1mySet2是2个HashSet。

返回值:如果mySet1是mySet2的正确超,则该函数返回true ,否则返回false

异常:如果HashSet为空,则此方法将提供ArgumentNullException

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to Check if a HashSet is a proper
// superset of the specified collection
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 into HashSet mySet1
        for (int i = 10; i < 20; i++) {
            mySet1.Add(i);
        }
  
        // Creating a HashSet of integers
        HashSet mySet2 = new HashSet();
  
        // Inserting elements into HashSet mySet2
        for (int i = 0; i < 25; i++) {
            mySet2.Add(i);
        }
  
        // Checking if mySet1 is Proper Superset Of mySet2
        // The function returns true if the condition
        // satisfies, else returns false
        Console.WriteLine(mySet1.IsProperSupersetOf(mySet2));
    }
}
输出:
False

范例2:

// C# code to Check if a HashSet is a proper
// superset of the specified collection
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 into HashSet mySet1
        mySet1.Add("India");
        mySet1.Add("Japan");
        mySet1.Add("USA");
        mySet1.Add("Spain");
        mySet1.Add("Italy");
  
        // Creating a HashSet of strings
        HashSet mySet2 = new HashSet();
  
        // Inserting elements into HashSet mySet2
        mySet2.Add("USA");
        mySet2.Add("Spain");
        mySet2.Add("Italy");
  
        // Checking if mySet1 is Proper Superset Of mySet2
        // The function returns true if the condition
        // satisfies, else returns false
        Console.WriteLine(mySet1.IsProperSupersetOf(mySet2));
    }
}
输出:
True

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.hashset-1.ispropersupersetof?view=netframework-4.7.2