Working on SortedSet Using C#

Introduction

The SortedSet<> class represents a collection of objects that is maintained in sorted order.The Hierarchy of this class is System.Collections.Generic.SortedSet<T>, where T is the type of elements in the set. In this article I explain how to create the SortedSet<> and how to perform the various methods and properties of this class.

Creation of the SortedSet<>

In order to create the SortedSet<> use the following lines:

SortedSet<string> sortedset = new SortedSet<string>();

SortedSet<int> sortedset1 = new SortedSet<int>();

Now before applying any property we should add some elements to the SortedSet that we created. So the next lines of code shows how we can add the elements to the SortedSet<>.

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //display the elements of SortedList

            Console.WriteLine("The elements in the SortedSet1:");

            foreach (int i in sortedset1)

            {

                Console.WriteLine(i);

            }

            //display the elements of SortedList

            Console.WriteLine("The elements in the SortedSet:");

            foreach (string s in sortedset)

            {

                Console.WriteLine(s);

            }        

        }

    }

}

 

Output 
 

IMAGE1.jpg

 

In this example I add the integer type and string type values in any order, in other words, in an unsorted order, but when I run this project the values are automatically sorted because I use SortedSet<>.

 

Properties

 

Count: Gets the number of elements in the SortedSet<T>.For example,

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //count the number of element in the SortedSet

            Console.WriteLine("The number of element in the sortedset is:" + sortedset.Count());

            //count the number of element in the SortedSet1

            Console.WriteLine("The number of element in the sortedset is:" + sortedset1.Count());

        }

    }

}

 

Output 
 

IMAGE2.jpg

 

Max: Gets the maximum value in the SortedSet<T>. For example:

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //The maximum number in the SortedSet

            Console.WriteLine("The maximum element in SortedSet is:" + sortedset.Max());

            //The maximum number in the SortedSet1

            Console.WriteLine("The maximum element in SortedSet1 is:" + sortedset1.Max());

        }

    }

}

 

Output 
 

IMAGE3.jpg

 

Min: Gets the minimum value in the SortedSet<T>. For example:

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //The minimum number in the SortedSet

            Console.WriteLine("The minimum element in SortedSet is:" + sortedset.Min());

            //The minimum number in the SortedSet1

            Console.WriteLine("The minimum element in SortedSet1 is:" + sortedset1.Min());

        }

    }

}

 

Output
 

image4.jpg

 

Methods

 

Clear(): Removes all elements from the set. For example:

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //The number of items contains in the set

            Console.WriteLine("The number of element in the sortedset is:" + sortedset.Count());

            Console.WriteLine("The number of element in the sortedset is:" + sortedset1.Count());

            //apply clear() method

            sortedset.Clear();

            sortedset1.Clear();

            Console.WriteLine("The number of element after apply the clear method in the sortedset is:" + sortedset.Count());

            Console.WriteLine("The number of element after apply the clear method in the sortedset is:" + sortedset1.Count());

        }

    }

}

 

Output 
 

IMAGE5.jpg

 

Contains(): Determines whether the set contains a specific element. For example:

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //apply contain method

            Console.WriteLine("The element Monday contain in the SortedSet : " + sortedset.Contains("Monday"));

            Console.WriteLine("The element Saturday contain in the SortedSet : " + sortedset.Contains("Saturday"));

            Console.WriteLine("The element December contain in the SortedSet : " + sortedset.Contains("December"));

            Console.WriteLine("The element 12 contain in the SortedSet : " + sortedset1.Contains(12));

            Console.WriteLine("The element 20 contain in the SortedSet : " + sortedset1.Contains(20));

        }

    }

}

 

Output 
 

IMAGE6.jpg

 

Remove(): Removes a specified item from the SortedSet<T>. For example:


namespace
SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //create the SortedSet having integer type of elements

            SortedSet<int> sortedset1 = new SortedSet<int>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //add the elements in the sortedSet having integer type

            sortedset1.Add(1);

            sortedset1.Add(40);

            sortedset1.Add(25);

            sortedset1.Add(10);

            sortedset1.Add(20);

            //display the elements of SortedList

            Console.WriteLine("The elements in the SortedSet1:");

            Console.WriteLine();

            foreach (int i in sortedset1)

            {

                Console.WriteLine(i);

            }

            //display the elements of SortedList

            Console.WriteLine();

            Console.WriteLine("The elements in the SortedSet:");

            Console.WriteLine();

            foreach (string s in sortedset)

            {

                Console.WriteLine(s);

            }

            //remove the elements from string type sortedset

            sortedset.Remove("Monday");

            sortedset.Remove("Tuesday");

            sortedset.Remove("Sunday");

            sortedset.Remove("Saturday");

            Console.WriteLine();

            Console.WriteLine("After removal of the elements from the SortedSet is as:");

            Console.WriteLine();

            foreach (string s in sortedset)

            {

                Console.WriteLine(s);

            }

            //remove the elements from integer type sortedset

            sortedset1.Remove(1);

            sortedset1.Remove(20);

            sortedset1.Remove(25);

            Console.WriteLine();

            Console.WriteLine("After removal of the elements from the SortedSet1 is as:");

            Console.WriteLine();

            foreach (int s in sortedset1)

            {

                Console.WriteLine(s);

            }

        }

    }

}

 

Output

IMAGE7.jpg

RemoveWhere(): Removes all elements that match the conditions defined by the specified predicate from a SortedSet<T>. For example:

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the SortedSet having string type of elements

            SortedSet<string> sortedset = new SortedSet<string>();

            //add the elements in the SortedSet having string type

            sortedset.Add("Sunady");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");          

            //display the elements of SortedList

            Console.WriteLine("The elements in the SortedSet1:");

            Console.WriteLine();

            foreach (string i in sortedset)

            {

                Console.WriteLine(i);

            }           

            //remove the elements using REMOVEWHERE

            sortedset.RemoveWhere(Element => Element.StartsWith("T"));

            sortedset.RemoveWhere(Element => Element.StartsWith("S"));           

            Console.WriteLine();

            Console.WriteLine("After removal of the elements from the SortedSet is as:");

            Console.WriteLine();

            foreach (string s in sortedset)

            {

                Console.WriteLine(s);

            }           

        }

    }

}

 

Output

IMAGE8.jpg

UnionWith(): The UnionWith instance method on the SortedSet type returns the union of two collections.Suppose the first set contains the elements "MCA","MBA","MTECH" and the second set contains the elements "BCA","BBA","BTECH". Then the UnionWith method adds all the elements into one collection. No duplicates will be found in the SortedSet. For example:

 

Syntax -  First_set.UnionWith(Second_Set)

 

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            SortedSet<string> sortedset1 = new SortedSet<string>();

            sortedset1.Add("MCA");

            sortedset1.Add("MBA");

            sortedset1.Add("MTECH");

            SortedSet<string> sortedset2 = new SortedSet<string>();

            sortedset2.Add("BCA");

            sortedset2.Add("BBA");

            sortedset2.Add("BTECH");

            //APPLY UNIONWITH METHOD

            sortedset1.UnionWith(sortedset2);

            Console.WriteLine("After apply the unionwith operation the set is as:");

            foreach (string str in sortedset1)

            {

                Console.WriteLine(str);

            }

        }

    }

}

 

Output

IMAGE9.jpg

SymmetricExceptWith(): The SymmetricExceptWith method returns all the elements in the two collections that are found in only one collection and not both collections. So if both collections contain the word "MCA", then the SymmetricExceptWith method will remove the word "MCA" from the SortedSet instance. For example:

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //FIRST LIST

            SortedSet<string> set = new SortedSet<string>();

            set.Add("MCA");

            set.Add("MBA");

            set.Add("MTECH");

            //SECOND LIST

            SortedSet<string> list = new SortedSet<string>();

            list.Add("MCA");

            list.Add("MTECH");

            // Determine symmetric set.

            set.SymmetricExceptWith(list);

            // Display elements.

            foreach (string val in set)

            {

                Console.WriteLine(val);

            }

        }

    }

}

 

Output

IMAGE10.jpg

Summary

In this article I explained  the concept of SortedSet<> and the various methods and properties of the SortedSet<> class. 

Up Next
    Ebook Download
    View all
    Learn
    View all