TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET SortedSet Examples

Use the SortedSet collection to keep items sorted while they are added.
SortedSet. Often we must create a List of items and then sort it. With a SortedSet, we can keep the elements sorted as we add them, potentially improving performance.Sort
And with this collection, we can ensure no duplicate elements are added. The SortedSet thus is a good way to have a collection of unique, ordered items.
Initial example. This program uses the SortedSet collection: it adds 3 strings to a SortedSet of Strings. It then uses the For-Each loop on the SortedSet.StringsFor Each, For

Contains: The code also uses Contains() to see if an element is in the set. We can quickly test to see whether the set contains something.

If Then
VB.NET program that uses SortedSet Module Module1 Sub Main() ' Create new SortedSet. Dim sorted = New SortedSet(Of String)() sorted.Add("carrot") sorted.Add("apple") sorted.Add("bird") ' Loop over items in SortedSet. For Each item As String In sorted Console.WriteLine("SORTEDSET: {0}", item) Next ' See if SortedSet contains a string. If sorted.Contains("bird") Then Console.WriteLine("CONTAINS bird") End If End Sub End Module Output SORTEDSET: apple SORTEDSET: bird SORTEDSET: carrot CONTAINS bird
Overlaps. Does any element in the SortedSet also occur in another collection? The Overlaps method will test a SortedSet against an IEnumerable—this means a List or array (usually).IEnumerableListArray

Program: We create a SortedSet, and a List. The value 20 occurs in both collections, so Overlaps returns true.

Finally: We create an empty array (where the maximum index is -1). No elements are in common, so Overlaps returns false.

Boolean
VB.NET program that uses SortedSet, Overlaps Module Module1 Sub Main() Dim sorted = New SortedSet(Of Integer)() sorted.Add(10) sorted.Add(20) Dim list = New List(Of Integer)() list.Add(20) list.Add(30) ' One element is the same, so Overlaps returns true. If sorted.Overlaps(list) Then Console.WriteLine("OVERLAPS = true") End If ' Create an empty array. ' ... The Overlaps method will then return false. Dim array(-1) As Integer If sorted.Overlaps(array) Then Console.WriteLine("NOT REACHED") End If End Sub End Module Output OVERLAPS = true
Notes, performance. It is possible to use SortedSet to improve performance. It lets us eliminate duplicates, and keep a collection sorted, as we go along.

So: There is no need to sort the items after we are done adding them to the collection.

And: There is no need to check for duplicates as we add them—the SortedSet is automatically restricted to distinct elements.

Duplicates
A summary. We explored the SortedSet generic collection in VB.NET. String elements, Integer elements, or many other types can be used in a SortedSet.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf