C# | Unión de SortedSet a una colección

La clase SortedSet representa la colección de objetos ordenados. Esta clase viene bajo el espacio de nombres System.Collections.Generic . El método SortedSet<T>.UnionWith(IEnumerable<T>) se usa para modificar el objeto SortedSet<T> actual para que contenga todos los elementos que están presentes en el objeto actual o en la colección especificada.

Propiedades:

  • En C#, la clase SortedSet se puede usar para almacenar, eliminar o ver elementos.
  • Mantiene el orden ascendente y no almacena elementos duplicados.
  • Se sugiere usar la clase SortedSet si tiene que almacenar elementos únicos y mantener un orden ascendente.

Sintaxis:

mySortedSet1.UnionWith(mySortedSet2);

Aquí, mySortedSet1 y mySortedSet2 son dos objetos SortedSet.

Excepción: este método dará ArgumentNullException si SortedSet es nulo.

A continuación se dan algunos ejemplos para entender la implementación de una mejor manera:

Ejemplo 1:

// C# code to get the Union of 2 SortedSets
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet1 = new SortedSet<int>();
  
        // adding elements in mySortedSet1
        mySortedSet1.Add(2);
        mySortedSet1.Add(4);
        mySortedSet1.Add(6);
        mySortedSet1.Add(8);
        mySortedSet1.Add(10);
  
        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet2 = new SortedSet<int>();
  
        // adding elements in mySortedSet
        mySortedSet2.Add(4);
        mySortedSet2.Add(5);
        mySortedSet2.Add(7);
        mySortedSet2.Add(8);
        mySortedSet2.Add(9);
  
        Console.WriteLine("The union of mySortedSet1 and mySortedSet2 is: ");
  
        mySortedSet1.UnionWith(mySortedSet2);
  
        // To display the union of mySortedSet1 and mySortedSet2
        foreach(int i in mySortedSet1)
        {
            Console.WriteLine(i);
        }
    }
}
Producción:

The union of mySortedSet1 and mySortedSet2 is: 
2
4
5
6
7
8
9
10

Ejemplo 2:

// C# code to get the Union of 2 SortedSets
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet1 = new SortedSet<string>();
  
        // adding elements in mySortedSet1
        mySortedSet1.Add("Geeks");
        mySortedSet1.Add("for");
        mySortedSet1.Add("Geeks");
        mySortedSet1.Add("Noida");
        mySortedSet1.Add("Data Structures");
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet2 = new SortedSet<string>();
  
        // adding elements in mySortedSet
        mySortedSet2.Add("Geeks");
        mySortedSet2.Add("Java");
        mySortedSet2.Add("Geeks Classes");
        mySortedSet2.Add("C++");
        mySortedSet2.Add("Noida");
  
        Console.WriteLine("The union of mySortedSet1 and mySortedSet2 is: ");
  
        mySortedSet1.UnionWith(mySortedSet2);
  
        // To display the union of mySortedSet1 and mySortedSet2
        foreach(string str in mySortedSet1)
        {
            Console.WriteLine(str);
        }
    }
}
Producción:

The union of mySortedSet1 and mySortedSet2 is: 
C++
Data Structures
for
Geeks
Geeks Classes
Java
Noida

Referencia:

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *