The new SortedSet<T> Collection in .NET 4.0

With the newest release of .NET Framework, version 4.0 - the class library provides new collection set from System.Collections.Generic classes called SortedSet<T>.

The two main features of SortedSet<T> are:

Duplicate elements are not allowed
Maintains a sorted order as elements are inserted or deleted without affecting the performance
In order to make all this clearer, lets pass through the tests that I've made.

Test 1: Create sorted set and print all elements (the default behaviour showcase)

var sortedSet1 = new SortedSet<int> { 5, 9, 11, 1, 44, 21, 3, 2, 9}; 

foreach (int element in sortedSet1)
{ 
    lbl_Test1.Text += " " + element; 
} 

The result is: 1 2 3 5 9 11 21 44

Here, we can notice the #1 above mentioned feature. Number 9 appears twice in the sorted set, but in the result it' is returned only once.

Test 2: Get view between range of elements

var sortedSet1 = new SortedSet<int> { 5, 9, 11, 1, 44, 21, 3, 2, 9 }; 

foreach (int element in sortedSet1.GetViewBetween(5, 20))
{ 
    lbl_Test2.Text += " " + element;
} 

The result is: 5 9 11

In Test 2, we can see that there is quite useful method that can be used to find all the numbers between given lower and upper Values, in our test 5 is lower and 20 is upper value.

Test 3: Difference, Union and Intersection operations with SortedSet<T>

var sortedSet1 = new SortedSet<int> { 5, 9, 11, 1, 44, 21, 3, 2, 9 };
var sortedSet2 = new SortedSet<int> { 11, 7, 22, 21, 25, 30}; 
 
lbl_Test3.Text += "Difference:"; 

foreach (int element in sortedSet1.Except(sortedSet2))
{ 
    lbl_Test3.Text += " " + element; 
} 

lbl_Test3.Text += "; Union:";

foreach (int element in sortedSet1.Union(sortedSet2))
{
    lbl_Test3.Text += " " + element; 
} 

lbl_Test3.Text += "; Intersection:"; 

foreach (int element in sortedSet1.Intersect(sortedSet2)) 
{ 
    lbl_Test3.Text += " " + element; 
} 

The result is: Difference: 1 2 3 5 9 44; Union: 1 2 3 5 9 11 21 44 7 22 25 30; Intersection: 11 21;

In Test 3, we have two Sorted Sets. Using the above mentioned methods, we are performing various operations.

There are some more useful methods like

sortedSet1.UnionWith(sortedSet2); 

which will permanently merge both sorted sets excluding the duplicate elements.

Test 4: Remove Elements using Remove(int) and RemoveWhere(Predicate)

var sortedSet1 = new SortedSet<int> { 5, 9, 11, 1, 44, 21, 3, 2, 9 }; 

sortedSet1.Remove(5); 

lbl_Test4.Text = "Without Element number 5:"; 

foreach (int element in sortedSet1) 
{ 
    lbl_Test4.Text += " " + element; 
} 


sortedSet1.RemoveWhere(X => X % 2 == 0); //will remove the even elements 


lbl_Test4.Text += "; Only odd elements:"; 

foreach (int element in sortedSet1) 
{ 
    lbl_Test4.Text += " " + element; 
}

The result is: Without element number 5: 1 2 3 9 11 21 44; Only odd elements: 1 3 9 11 21.

NOTE: All results are displayed in <asp:Label /> controls which ID’s are: lbl_Test1, lbl_Test2, lbl_Test3, lbl_Test4 – created only for testing purpose with no affection to the entire blog post subject.

You can download the working source code: DOWNLOAD

I hope this was useful and informative.

Regards,
Hajan

Reference: I have published this blog post on MKDOT.NET Community 2 months ago – Link:http://mkdot.net/blogs/hajan/archive/2010/06/25/the-new-sortedset-lt-t-gt-collection-in-net-4-0.aspx

5 Comments

Comments have been disabled for this content.