VB.Net - SortedList Class

Advertisements


The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.

A sorted list is a combination of an array and a hashtable. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Properties and Methods of the SortedList Class

The following table lists some of the commonly used properties of the SortedList class:

PropertyDescription
Capacity Gets or sets the capacity of the SortedList.
CountGets the number of elements contained in the SortedList.
IsFixedSizeGets a value indicating whether the SortedList has a fixed size.
IsReadOnlyGets a value indicating whether the SortedList is read-only.
ItemGets and sets the value associated with a specific key in the SortedList.
KeysGets the keys in the SortedList.
ValuesGets the values in the SortedList.

The following table lists some of the commonly used methods of the SortedList class:

S.NMethod Name & Purpose
1Public Overridable Sub Add ( key As Object, value As Object )
Adds an element with the specified key and value into the SortedList.
2Public Overridable Sub Clear
Removes all elements from the SortedList.
3Public Overridable Function ContainsKey ( key As Object ) As Boolean
Determines whether the SortedList contains a specific key.
4Public Overridable Function ContainsValue ( value As Object ) As Boolean
Determines whether the SortedList contains a specific value.
5Public Overridable Function GetByIndex ( index As Integer ) As Object
Gets the value at the specified index of the SortedList.
6Public Overridable Function GetKey ( index As Integer ) As Object
Gets the key at the specified index of the SortedList.
7Public Overridable Function GetKeyList As IList
Gets the keys in the SortedList.
8Public Overridable Function GetValueList As IList
Gets the values in the SortedList.
9Public Overridable Function IndexOfKey ( key As Object ) As Integer
Returns the zero-based index of the specified key in the SortedList.
10Public Overridable Function IndexOfValue ( value As Object ) As Integer
Returns the zero-based index of the first occurrence of the specified value in the SortedList.
11Public Overridable Sub Remove ( key As Object )
Removes the element with the specified key from the SortedList.
12Public Overridable Sub RemoveAt ( index As Integer )
Removes the element at the specified index of SortedList.
13Public Overridable Sub TrimToSize
Sets the capacity to the actual number of elements in the SortedList.

Example:

The following example demonstrates the concept:

Module collections
   Sub Main()
      Dim sl As SortedList = New SortedList()
      sl.Add("001", "Zara Ali")
      sl.Add("002", "Abida Rehman")
      sl.Add("003", "Joe Holzner")
      sl.Add("004", "Mausam Benazir Nur")
      sl.Add("005", "M. Amlan")
      sl.Add("006", "M. Arif")
      sl.Add("007", "Ritesh Saikia")
      If (sl.ContainsValue("Nuha Ali")) Then
          Console.WriteLine("This student name is already in the list")
      Else
          sl.Add("008", "Nuha Ali")
      End If
       ' Get a collection of the keys. 
      Dim key As ICollection = sl.Keys
      Dim k As String
      For Each k In key
          Console.WriteLine(" {0} : {1}", k, sl(k))
      Next k
      Console.ReadKey()
   End Sub
End Module

When the above code is compiled and executed, it produces the following result:

001: Zara Ali
002: Abida Rehman
003: Joe Holzner
004: Mausam Banazir Nur
005: M. Amlan 
006: M. Arif
007: Ritesh Saikia
008: Nuha Ali


Advertisements
Advertisements