C# | How to get a subset in a SortedSet
Last Updated :
01 Feb, 2019
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:
- In C#, SortedSet class can be used to store, remove or view elements.
- It maintains ascending order and does not store duplicate elements.
- It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order.
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 :
- ArgumentException: If lowerValue is more than upperValue according to the comparer.
- ArgumentOutOfRangeException: A tried operation on the view was outside the range specified by lowerValue and upperValue.
Example 1:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
SortedSet< string > mySet1 = new SortedSet< string >();
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" );
SortedSet< string > mySet2 = mySet1.GetViewBetween( "C" , "G" );
foreach ( string str in mySet2)
{
Console.WriteLine(str);
}
}
}
|
Example 2:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
SortedSet< int > mySet1 = new SortedSet< int >();
for ( int i = 0; i < 10; i++) {
mySet1.Add(i);
}
SortedSet< int > mySet2 = mySet1.GetViewBetween(3, 7);
foreach ( int i in mySet2)
{
Console.WriteLine(i);
}
}
}
|
Reference:
Share your thoughts in the comments
Please Login to comment...