Introduction
In any programming language, collections play a very important role. Many times we need Sorted collections, so I felt to discuss SortedSet collections in this article. This article can be used by beginners, intermediate, and professionals.
We are going to cover,
- Where should we use it?
- What is SortedSet Collection?
- How to add elements in SortedSet Collection?
- How to Remove Element from Sorted Set Collection?
- Remove Method
- Clear Method
- RemoveWhere
- Method available in SortedSet Collections
- Count
- UnionWith
- SymmetricExceptWith
- ExceptWith
- Overlaps
- IntersectWith
- Min, max
- SetEquals
- GetViewBetween
Let’s start with,
Where Should we use SortedSet?
Suppose you have a requirement to store a unique element that needs to be in sorted order then SortedSet is the right choice for you. By default, it would be sorted in ascending order.
What is SortedSet Collection?
SortedSet is a generic collection of objects in sorted order. Duplicate elements are not allowed in SortedSet Collections like HashSet Collection.
SortedSet is a class defined in System.Collections.Generic.
It is Dynamic Collection means it can grow when you add an element and Shrink when you remove the element from the Collection.
Syntax of SortedSet<T> Class
public class SortedSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlySet<T>, System.Collections.Generic.ISet<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
Following interfaces implemented in SortedSet<T> Class.
- ICollection<T>
- IEnumerable<T>
- IEnumerable
- IReadOnlyCollection<T>
- ISet<T>
- ICollection
- IDeserializationCallback
- ISerializable
How to Create SortedSet collection?
Here I am going to create an empty SortedSet Collection.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
}
}
}
How to add elements in SortedSet Collection?
We will use Add method to add elements to the collection. In the below example, we will add cities to the collection and then print on the screen.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
Contain Method in SortedSet Collection
Contain method is used to see element is present in the collection or not. Let’s see the below example.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
if(sortedSetDemo.Contains("Vadodara"))
{
Console.WriteLine("Vadodara is present in the collection");
}
else
{
Console.WriteLine("Vadodara is not present in the collection");
}
Console.ReadLine();
}
}
}
Output
How to Remove element from Collection?
We can use Remove, Clear, and RemoveWhere methods to remove elements from Collection as per requirement.
Remove
This method is used to remove a specific item from the collection. See the below example,
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
sortedSetDemo.Remove("Surat");
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
In the above code, “Surat” is removed from the collection.
Clear()
This method is used to remove all items from the collection.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
sortedSetDemo.Clear();
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.WriteLine("Clear all elements from collection");
Console.ReadLine();
}
}
}
Output
RemoveWhere
This method is used to remove all elements that match the condition defined in method.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
sortedSetDemo.Add("Mathura");
sortedSetDemo.RemoveWhere(myFunc);
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
private static bool myFunc(string strcity)
{
if(strcity.StartsWith('M'))
{
return true;
}
return false;
}
}
}
Output
We have removed all cities starting with ‘M’ from the collection using the RemoveWhere method.
Methods Available in SortedSet Collections
Many methods are available in sortedSet collection to perform various operations. We will discuss a few important methods Here.
CopyList
In the below example, we have copied the list to sortedset.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
List<string> lstString = new List<string>()
{
"Mumbai",
"Surat",
"Vadodara",
"Dabhoi",
"Pune",
"Mathura"
};
SortedSet<string> sortedSetDemo = new SortedSet<string>(lstString);
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
Count, Clear Methods
The count method is used to get the number of items available in SortedSet. The clear method was used to clear all items from SortedSet collection.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> sortedSetDemo = new SortedSet<string>();
sortedSetDemo.Add("Mumbai");
sortedSetDemo.Add("Surat");
sortedSetDemo.Add("Vadodara");
sortedSetDemo.Add("Dabhoi");
sortedSetDemo.Add("Pune");
sortedSetDemo.Add("Mathura");
Console.WriteLine(sortedSetDemo.Count);
sortedSetDemo.Clear();
foreach (var item in sortedSetDemo)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
UnionWith
This method returns the union of two collections. Please see the below example,
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Pune");
collection2.Add("Mathura");
collection1.UnionWith(collection2);
foreach (var item in collection1)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
ExceptWith
This is removed match items from collections.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
collection1.ExceptWith(collection2);
foreach (var item in collection1)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
OverLaps
This method compares 2 collections and returns true even a single match found.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
Console.WriteLine(collection1.Overlaps(collection2));
Console.ReadLine();
}
}
}
Output
IntersectWith
This method returns common items from collections.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
collection1.IntersectWith(collection2);
foreach (var item in collection1)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
SymmetricExceptWith
This method returns all elements that are not present in both collections.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Pune");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
collection1.SymmetricExceptWith(collection2);
foreach (var item in collection1)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
Min, max
This method returns Min and Max values from the collections.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
Console.WriteLine(collection2.Min);
Console.Write(collection2.Max);
Console.ReadLine();
}
}
}
Output
SetEquals
This method returns true if both collections have the same items.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<string> collection1 = new SortedSet<string>();
collection1.Add("Mumbai");
collection1.Add("Surat");
collection1.Add("Vadodara");
collection1.Add("Dabhoi");
collection1.Add("Mathura");
SortedSet<string> collection2 = new SortedSet<string>();
collection2.Add("Vadodara");
collection2.Add("Dabhoi");
collection2.Add("Mathura");
collection2.Add("Mumbai");
collection2.Add("Surat");
Console.WriteLine(collection1.SetEquals(collection2));
Console.ReadLine();
}
}
}
Output
GetViewBetween
This method returns items from the collection between the given range.
using System;
using System.Collections.Generic;
namespace SortedSetDemo
{
class Program
{
static void Main(string[] args)
{
SortedSet<int> collection1 = new SortedSet<int>();
collection1.Add(9);
collection1.Add(5);
collection1.Add(7);
collection1.Add(4);
collection1.Add(6);
foreach (var item in collection1.GetViewBetween(4,8))
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output
Hope you enjoyed this article and found it useful. Thank you for reading this article.