SortedSet Class in C#

CsharpServer Side ProgrammingProgramming

The SortedSet class in C# represents a collection of objects that is maintained in sorted order.

Following are the properties of the SortedSet class −

Sr.NoProperty & Description
1Comparer
Gets the IComparer<T> object that is used to order the values in the SortedSet<T>.
2Count
Gets the number of elements in the SortedSet<T>.
3Max
Gets the maximum value in the SortedSet<T>, as defined by the comparer.
4Min
Gets the minimum value in the SortedSet<T>, as defined by the comparer.

Following are some of the methods of the SortedSet class −

Sr.NoMethod & Description
1Add(T)
Adds an element to the set and returns a value that indicates if it was successfully added.
2Clear()
Removes all elements from the set.
3Contains(T)
Determines whether the set contains a specific element.
4CopyTo(T[])
Copies the complete SortedSet<T> to a compatible onedimensional array, starting at the beginning of the target array.
5CopyTo(T[], Int32)
Copies the complete SortedSet<T> to a compatible onedimensional array, starting at the specified array index.
6CopyTo(T[], Int32, Int32)
Copies a specified number of elements from SortedSet<T> to a compatible one-dimensional array, starting at the specified array index.
7CreateSetComparer()
Returns an IEqualityComparer object that can be used to create a collection that contains individual sets.

Example

Let us now see some examples −

To check if the SortedSet contains a specific element, the code is as follows −

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Does the SortedSet1 contains the element DE? = "+set1.Contains("DE"));
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2...");
      foreach (string res in set2) {
         Console.WriteLine(res);
      }
      Console.WriteLine("SortedSet2 is a superset of SortedSet1? = "+set2.IsSupersetOf(set1));
   }
}

Output

This will produce the following output −

Elements in SortedSet1...
CD
Does the SortedSet1 contains the element DE? = False
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 is a superset of SortedSet1? = True

To get an enumerator that iterates through the SortedSet, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("AB");
      set1.Add("BC");
      set1.Add("CD");
      set1.Add("EF");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2 (Enumerator for SortedSet)...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

Output

This will produce the following output −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK
raja
Published on 11-Dec-2019 10:29:18
Advertisements