1. Computing

Visual Basic .NET 2010 Express - Collections of Things

The Collection Relatives

By

ArrayList, Stack, Queue, Dictionary and Hashtable

Microsoft puts it this way at the MSDN site: "The properties and methods of the Collection object provide only the most basic services for collections." Here's the translation from Microsoft's documentation language: To really get the most from collections, you need to use the more specialized classes that inherit from the Collection object. And there's a bunch of them - too many to detail all of them here. But here's an introduction.

ArrayList

ArrayList, in spite of the name, actually inherits from the Collection object. (The next version of the Signature Block program uses it.) ArrayList might be thought of as a better array. One principal advantage is that it's size is dynamic. And you can sort it! (You can sort regular arrays too, but the code to do it is, again, different.)

Here's an example:


Dim myArrayList As New ArrayList
myArrayList.Add("Sam")
myArrayList.Add("George")
myArrayList.Add("Allen")

For Each name As String In myArrayList
   Debug.WriteLine(name)
Next name
' The result is in the same order they were added:
' Sam
' George
' Allen

myArrayList.Sort()

For Each name As String In myArrayList
   Debug.WriteLine(name)
Next name
' Now the result is sorted:
' Allen
' George
' Sam

Stacks and Queues

A Stack is a Last-In, First-Out (LIFO) collection. A Queue represents a First-In, First-Out (FIFO) collection. These are ideas that go way back in software. Some early mainframe computers were designed as "stack machines" because machine instructions were stored in stack arrays before being executed by the CPU. Changing the order of instructions in the stack was a key part of programming this type of computer and the historical roots of the methods in VB.NET come from that time.

Stack and Queue work pretty much like other collections except they have the Push, Peek, and Pop methods to manage the elements. Here's an example:


Dim myStack As New Stack
myStack.Push("George")
myStack.Push("Allen")
myStack.Push("Sam")
Debug.WriteLine(myStack.Peek)
Debug.WriteLine(myStack.Pop)
Debug.WriteLine(myStack.Pop)
' Here's the result. Peek doesn't change the Stack
' Sam
' Sam
' Allen

Dictionary and Hashtable

Dictionary collections are pretty much what you might expect. This technique is available in VB6 and even VBScript. The main thing that has changed is the number and sophistication of the methods and properties available.

The main benefit of Dictionary objects and Hashtable is performance in large collections. Consider a collection of, for example, 50,000 elements. Suppose you want the element that has the key "D479H431". If the elements in the collection are in random order, the only way to find a specific one is by starting at the beginning and looking at each element until the right one is found.

The idea behind Hashtable is to convert a key into a "hashcode" (explained in a few paragraphs). Then use the hashcode as the key to save values in the collection.

The hashcode gives the software a head start in finding the objects and speeds up the process considerably. (Consult volume 3 of Don Knuth's "Art of Computer Programming" for more detail than you thought was possible.)

The idea behind the Hashtable goes way back in software and has been used for a long time. A hashtable concept is basically a lookup table except that the key value used for looking up something else is a calculated value known as a hashcode that is based on the value being saved in the table. The hashcode value is calculated so that the probability that some other text string will result in the same hashcode value is very low. But the calculation guarantees that the same string will always produce the same hashcode value. Think of the hashcode as being a "nickname" for the entire string.

Here's an example of how the SortedDictionary object is used in code:


Dim myDict As New SortedDictionary(Of String, String)

myDict.Add("txt", "notepad.exe")
myDict.Add("bmp", "paint.exe")
myDict.Add("dib", "paint.exe")
myDict.Add("rtf", "wordpad.exe")

To drive home the concepts of collections, the Signature Block program is updated using the ArrayList object on the next page.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2010 Express - Collections of Things

©2014 About.com. All rights reserved.