VB.Net - SortedList


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:

Property Description
Capacity Gets or sets the capacity of the SortedList.
Count Gets the number of elements contained in the SortedList.
IsFixedSize Gets a value indicating whether the SortedList has a fixed size.
IsReadOnly Gets a value indicating whether the SortedList is read-only.
Item Gets and sets the value associated with a specific key in the SortedList.
Keys Gets the keys in the SortedList.
Values Gets the values in the SortedList.

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

S.N Method Name & Purpose
1

Public Overridable Sub Add (key As Object, value As Object)

Adds an element with the specified key and value into the SortedList.

2

Public Overridable Sub Clear

Removes all elements from the SortedList.

3

Public Overridable Function ContainsKey (key As Object) As Boolean

Determines whether the SortedList contains a specific key.

4

Public Overridable Function ContainsValue (value As Object) As Boolean

Determines whether the SortedList contains a specific value.

5

Public Overridable Function GetByIndex (index As Integer) As Object

Gets the value at the specified index of the SortedList.

6

Public Overridable Function GetKey (index As Integer) As Object

Gets the key at the specified index of the SortedList.

7

Public Overridable Function GetKeyList As IList

Gets the keys in the SortedList.

8

Public Overridable Function GetValueList As IList

Gets the values in the SortedList.

9

Public Overridable Function IndexOfKey (key As Object) As Integer

Returns the zero-based index of the specified key in the SortedList.

10

Public Overridable Function IndexOfValue (value As Object ) As Integer

Returns the zero-based index of the first occurrence of the specified value in the SortedList.

11

Public Overridable Sub Remove (key As Object)

Removes the element with the specified key from the SortedList.

12

Public Overridable Sub RemoveAt (index As Integer)

Removes the element at the specified index of SortedList.

13

Public 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

vb.net_collections.htm

Advertisements