Open In App

C# | How to get a subset in a SortedSet

SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.GetViewBetween(T, T) method is used to return a view of a subset in a SortedSet<T>.

Properties:



Syntax: public virtual System.Collections.Generic.SortedSet<T> GetViewBetween (T lowerValue, T upperValue);

Parameters:
lowerValue: The lowest desired value in the view.
upperValue: The highest desired value in the view.



Return Value: A subset view that contains only the values in the specified range.

Exceptions :

Example 1:




// C# code to get the subset of a SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySet1 = new SortedSet<string>();
  
        // Inserting elements in SortedSet
        mySet1.Add("A");
        mySet1.Add("B");
        mySet1.Add("C");
        mySet1.Add("D");
        mySet1.Add("E");
        mySet1.Add("F");
        mySet1.Add("G");
        mySet1.Add("H");
        mySet1.Add("I");
  
        // Get the subset between "C" and "G"
        SortedSet<string> mySet2 = mySet1.GetViewBetween("C", "G");
  
        // Displaying the elements in the subset
        foreach(string str in mySet2)
        {
            Console.WriteLine(str);
        }
    }
}

Output:
C
D
E
F
G

Example 2:




// C# code to get the subset of a SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet<int> mySet1 = new SortedSet<int>();
  
        // Inserting elements in SortedSet
        for (int i = 0; i < 10; i++) {
            mySet1.Add(i);
        }
  
        // Get the subset between "3" and "7"
        SortedSet<int> mySet2 = mySet1.GetViewBetween(3, 7);
  
        // Displaying the elements in the subset
        foreach(int i in mySet2)
        {
            Console.WriteLine(i);
        }
    }
}

Output:
3
4
5
6
7

Reference:


Article Tags :
C#