Hello Folks,
In the process of exploring new features added to C# 4.0 Class Library, I will discuss
about new SortedSet<T> collection.
SortedSet<T> is new collection
added under the System.Collection.Generic.
SortedSet<T> maintains the unique
list of elements and keep them sorted implicitly.
It uses the self-balancing red-black tree, providing O(logN) complexity for
insert, update or delete operation. It silently suppress the attempt to add
duplicate elements. It has numbers of handy method and property associated with
it. Let me take simple examples and discuss each one of them.
First let me take a simple example to demonstrate how it internally maintain's the
sorted order and removes the duplicate elements from the collection.
|
namespace SortedSetinCSharp4
{
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[]
args)
{
SortedSet<int> sortedInt =
new SortedSet<int>()
{ 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };
foreach (var item in
sortedInt)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Output: 1,2,3,4,5,7,8,9,10
|
As you can see from the output data is already sorted and duplicate element are
removed.
SortedSet<T>.Reverse()
The Reverse method on SortedSet as the name suggest reverse the sort order.
|
namespace SortedSetinCSharp4
{
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[]
args)
{
SortedSet<int> sortedInt =
new SortedSet<int>()
{ 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };
foreach (var item in
sortedInt.Reverse())
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Output: 10,9,8,7,5,4,3,2,1
|
SortedSet<T>.Remove()and SortedSet<T>.RemoveWhere
Remove method accepts an element of type
T as its parameter and returns a bool
value indicating the success or failure of the remove operation. While
RemoveWhere is used to remove a group of elements from the collection
satisfying the predicate defined. It return an integer value which is equal to the
count of number of elements removed from the collection. I will demo the code for
RemoveWhere. Remove you can try by yourself.
|
namespace SortedSetinCSharp4
{
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[]
args)
{
SortedSet<int> sortedInt =
new SortedSet<int>()
{ 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };
int deleteCount = sortedInt.RemoveWhere(st => st % 2 == 0);
Console.WriteLine(deleteCount);
foreach (var item in
sortedInt)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Output: 4
1,3,5,7,9
|
If you observe the sortedInt collection has five element that satisfy the predicate
(st => st % 2 == 0), but the output shows count as 4 and not 5. This is best
example to demonstrate that SortedSet<T> implicitly removes the duplicate
elements.
SortedSet<T>.Add(Titem) and SortedSet<T>Clear()
As the name suggest SortedSet<T>.Add(Titem)
method is used to insert new element to the collection. It returns a bool value
indicating if add operation was success or failure. If you try to add a duplicate
value to the collection, it will not be added, this is clear from the false as return
value while inserting duplicate value.
SortedSet<T>Clear() method
is used to clear out all the elements of the collection.
SortedSet<T>.Maxand SortedSet<T>.Min
SortedSet<T>.Max and SortedSet<T>.Min are two important property. As
the name suggest, they are used to get the maximum and minimum value of the collection.
|
namespace SortedSetinCSharp4
{
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[]
args)
{
SortedSet<int> sortedInt =
new SortedSet<int>()
{ 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };
Console.WriteLine("{0}, {1}",sortedInt.Max,
sortedInt.Min);
Console.ReadKey();
}
}
}
Output: 10, 1
|
SortedSet<T>.GetViewBetween(TlowerValue,
T upperValue)
This method returns a view of original collection, so any change that we make to
this view will be reflected in the original collection
|
namespace SortedSetinCSharp4
{
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[]
args)
{
SortedSet<int> sortedInt =
new SortedSet<int>()
{ 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };
var result = sortedInt.GetViewBetween(3, 7);
foreach (var item in
result)
{
Console.WriteLine(item);
}
result.Add(6);
foreach (var item in
sortedInt)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Output: 3,4,5,7
Output: 1,2,3,4,5,6,7,8,9,10
|
As you can see from the above example, we added 6 to the result and the same is
reflected in the original collection.
These are few of the main methods, properties associated with the
SotedSet<T>. Let me give a brief outline of other methods and
you can explore them in detail.
Contains : Used to search for given
element in the set
SortedSet<T>.CopyTo(T[] array, intindex,
int count): Copy the specifiednumber
of element from the original set to given array.
SortedSet<T>.Count: Count of element
present in the collection
Happylearning J
Related Blogs:
Optional and Named Parameters
in C# 4.0