C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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 ThenVB.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
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.
BooleanVB.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
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