Some Advanced Methods of SortedSet in C#

Introduction

In my previous article I explained what the SortedSet<> class is and the various properties and methods of that class. In this article I explain some new/or advance methods of the SortedSet<> class of the System.Collections.Generic Namespace.

Advance Methods

ExceptWith : Removes all elements that are in a specified collection from the current SortedSet<T> object, or we can say that The ExceptWith method simply removes all the elements found in the selected collection from the SortedSet instance. The resulting SortedSet will contain all its original elements except those that were found in the other collection. For example:

Syntax : First_set.ExceptWith(Second_set)

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create first set

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

            //add elements in first set

            sortedset.Add("Sunday");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //create second set

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

            //add element in second set

            sortedset1.Add("Wednesday");

            sortedset1.Add("Thusday");

            sortedset1.Add("Friday");

            sortedset1.Add("Saturday");

            sortedset1.Add("December");

            sortedset1.Add("Sunday");

            // apply ExceptWith method

            sortedset.ExceptWith(sortedset1);

            // Display elements.

            foreach (string set in sortedset)

            {

                Console.WriteLine(set);

            }

        }

    }

}

 

Output

ExceptWith.jpg

OverLaps : Determines whether the current SortedSet<T> object and a specified collection share common elements, or we can say that the Overlaps method returns a boolean value (true/or false) that tells us whether the target collection has any elements in common with the SortedSet. Even if only one element is found in common, the result will be True. If no elements are in common, the result will be False. For example:

Syntax : bool var_name=first_set.OverLaps(Second_collection);

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create first set

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

            //add element in the first set

            sortedset.Add("Sunday");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            //create second set

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

            //add element in the second set

            sortedset1.Add("Wednesday");

            sortedset1.Add("Thusday");

            sortedset1.Add("Friday");

            sortedset1.Add("Saturday");

            sortedset1.Add("Sunday");//match the element from the first set

            //create third set

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

            //add element in third set

            sortedset2.Add("Januray");

            sortedset2.Add("Feburay");

            sortedset2.Add("March");

            sortedset2.Add("April");

            sortedset2.Add("May");

            //apply OverLaps method

            bool a = sortedset.Overlaps(sortedset1);//it return true because one element is match

            bool b = sortedset.Overlaps(sortedset2);//it return false bacause no element are matched

            Console.WriteLine(a);

            Console.WriteLine(b);

        }

    }

}
 

Output

OverLaps.jpg

We can also apply the Overlaps method to a SortedSet from any other collection. For example:

namespace SortedSet 

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the sortedset<>

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

            sortedset.Add("Sunday");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            //create the List<>

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

            list.Add("Wednesday");

            list.Add("Thusday");

            list.Add("Friday");

            list.Add("Saturday");

            list.Add("Sunday");//match the element from the first set

            //create the Hashset<>

            HashSet<string> hashset = new HashSet<string>();           

            hashset.Add("Januray");

            hashset.Add("Feburay");

            hashset.Add("March");

            hashset.Add("April");

            hashset.Add("Monday");

            //apply OverLaps method

            bool a = sortedset.Overlaps(list);//it return true because one element is match

            bool b = sortedset.Overlaps(hashset);//it return true bacause the element is matched

            Console.WriteLine(a);

            Console.WriteLine(b);

        }

    }

}

 

Output

OverLaps1.jpg

IntersectWith : Modifies the current SortedSet<T> object so that it contains only elements that are also in a specified collection or the IntersectWith method on the SortedSet changes the set instance so that it contains only the elements that were present in both collections. For example:

Syntax : First_set.IntersectWith(Second_set);

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the sortedset<>

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

            sortedset.Add("Sunday");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            //create the List<>

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

            list.Add("Sunday");

            list.Add("Monday");

            list.Add("Tuesday");

            list.Add("Wednesday");

            list.Add("Thusday");

            list.Add("Friday");

            list.Add("Saturday");

            //apply IntersectWith method

            sortedset.IntersectWith(list);

            //display the elements that are presents in both the collection

            Console.WriteLine("The elements that are present in both the collection are:");

            foreach (string str in sortedset)

            {

                Console.WriteLine(str);

            }           

        }

    }

}

 

Output

IntersectWith.jpg

IsSubsetOf : Determines whether a SortedSet<T> object is a subset of the specified collection or we can say that the IsSubsetOf method determines whether the subset is or is not entirely contained in another collection; if yes then it returns true otherwise false.

IsSupersetOf : Determines whether a SortedSet<T> object is a superset of the specified collection or we can say that the IsSupersetOf method determines whether the superset does or does not contain another set entirely; if yes then it returns yes otherwise false.

The following example shows the use of both the IsSubsetOf and the IsSupersetOf methods:

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the sortedset<>

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

            sortedset.Add("Sunday");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //create the List<>

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

            list.Add("Sunday");

            list.Add("Monday");

            list.Add("Tuesday");           

            //create the hashset<>

            HashSet<string> hashset = new HashSet<string>();

            hashset.Add("Wednesday");

            hashset.Add("Thusday");

            hashset.Add("Friday");

            hashset.Add("Saturday");

            //the nest line gives false because the sortedset is not the subset of list

            Console.WriteLine("SortedSet is the subset of List or not:"+sortedset.IsSubsetOf(list));

            //sortedset is the superset of list so next line gives output as true

            Console.WriteLine("SortedSet is the superset of List or not:"+sortedset.IsSupersetOf(list));

            //sortedset is the superset of hashset so the next lines gives output as true

            Console.WriteLine("SortedSet is the superset of Hashset or not:"+sortedset.IsSupersetOf(hashset));           

        }

    }

}

 

Output

IsSubsetOf.jpg

SetEquals : Determines whether the current SortedSet<T> object and the specified collection contain the same elements or not; if yes then its returns true otherwise false. For example:

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the sortedset<>

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

            sortedset.Add("Sunday");

            sortedset.Add("Monday");

            sortedset.Add("Tuesday");

            sortedset.Add("Wednesday");

            sortedset.Add("Thusday");

            sortedset.Add("Friday");

            sortedset.Add("Saturday");

            //create the List<>

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

            list.Add("Sunday");

            list.Add("Monday");

            list.Add("Tuesday");

            list.Add("Wednesday");

            list.Add("Thusday");

            list.Add("Friday");

            list.Add("Saturday");

            //create the hashset<>

            HashSet<string> hashset = new HashSet<string>();

            hashset.Add("Wednesday");

            hashset.Add("Thusday");

            hashset.Add("Friday");

            hashset.Add("Saturday");

            //apply SetEquals

            //The next line gives true as SortedSet and List elements are same

            Console.WriteLine("The collections SortedSet and List are equal or not :" + sortedset.SetEquals(list));

            //The next line gives false as SortedSet and hashset elements are not equal

            Console.WriteLine("The collections SortedSet and Hashset are equal or not :" + sortedset.SetEquals(hashset));                    

        }

    }

}

 

Output

SetEquals.jpg

Reverse : The Reverse method is used to reverse the elements of the SortedSet. When you add elements to your SortedSet, they are automatically stored in ascending order. In order to store the element in decending order we use the Reverse method. For example:

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

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

            sortedset.Add(5);

            sortedset.Add(7);

            sortedset.Add(8);

            sortedset.Add(9);

            sortedset.Add(4);

            Console.WriteLine("The element in the asending order are:");

            Console.WriteLine();

            foreach (int a in sortedset)

            {

                Console.Write(a);

                Console.Write(" ");

            }

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine("After apply reverse operation the elements in the decending order are :");

            Console.WriteLine();

            foreach (int a in sortedset.Reverse())

            {

                Console.Write(a);

                Console.Write(" ");

            }           

        }

    }

}

 

Output

Reverse.jpg

Create SortedSet From List

Now the next lines shows how can we create the SortedSet from the List. For this, first of all we create the List and give the reference of the List to the SortedSet. For example:

namespace SortedSet

{

    class Program

    {

        static void Main(string[] args)

        {

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

            list.Add("One");

            list.Add("Two");

            list.Add("Three");

            list.Add("Four");

            list.Add("five");

            Console.WriteLine("The list is as:");

            foreach (string s in list)

            {

                Console.WriteLine(s);

            }

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

            Console.WriteLine("The sortedset is as:");

            foreach (string str in sortedset)

            {

                Console.WriteLine(str);

            }

        }

    }

}

 

Output

CONVERSION.jpg

Summary

In this article I explained the advanced methods of the SortedSet<> class which is the class of System.Collections.Generic Namespace; also describes how to create the SortedSet from a list.

Up Next
    Ebook Download
    View all
    Learn
    View all