|
|
How Do I....Choose a collection to use?This example discusses the storage procedures and capabilities of three different collections: Queue, SortedList, and Hashtable. The primary purpose of this demonstration is to help developers understand the functions of the different collection types, so that they can choose one that will meet their needs.
At first, you may find it difficult to determine which collection best suits your needs. With experience, you will become familiar with each of the different collections you can use, and even customize your own by extending ICollection or IList. The three collections discussed here are: Queue, SortList, and Hashtable. For more detailed information on Hashtable, see How Do I...Use a hashtable?. A Queue is an unsorted collection, which is meant to be used on a 'first-in, first-out' basis.
// don't forget to include those 'Imports' statements
Imports System
Imports System.Collections
' ... class and method declarations...
' the declaration for an array of information
Dim names() As String = New String() {"Simpson","Bristow","Walls", _
"Gilligan","Davidson","Laurence", _
"Jackson","Singh","O'Connor","Moynagh"}
' the declarations for each of the three collections
Dim q As Queue q = new Queue()
Dim sl As SortedList = New SortedList()
Dim ht As Hashtable = New Hashtable()
Dim s As String
' we use a foreach statement to populate our collections
For Each s In names
' to add an element to the queue, use Enqueue
' A queue only receives the value you want to add
q.Enqueue(s)
' Use Add of SortedList/Hashtable to add elements
' Note that you must add key-value pairs
sl.Add(s, s)
ht.Add(s, s)
Next s
VB
You can also remove items from collections, although different methods must be used depending on the collection in question. Many collections support the Remove method to remove a specified element. Both Hashtable and SortedList support this method, to remove the element you specify. RemoveAt is also supported by many collections; however, out of the three collections mentioned in this discussion, only SortedList supports the RemoveAt method. RemoveAt requires you to supply an index of the element to remove, which doesn't really make sense in the case of a Queue or Hashtable (which is why they don't support it). A Queue is a special case since removing elements in a Queue can be performed only on the top element off the Queue. Use the Dequeue method to achieve this, as in the following sample.
' to remove an element with a specific key, use the Remove method ' where the concept of a key is invalid, specify the object to remove sl.Remove(keyValue); ' removes the arbitrary keyvalue ht.Remove(keyValue); ' removes the arbitrary keyvalue ' to remove an element at a specific index, use the RemoveAt method ' This does not make sense for collections such as a Hashtable/Queue sl.RemoveAt(indexValue); ' removes the specified index ' A Queue does not have the concept of an arbitrary remove ' Use Dequeue to read, AND remove the top element off the Queue q.Dequeue(); ' remove the top value VB
After you create the collection, you can look at the information inside those collections by using a simple iteration statement to print the values out. It is difficult to show here what the results will be, so it requires you to test the output. Note that the Queue will not change the order of the items, the SortedList will sort them alphabetically, and the Hashtable will change the order, but not necessarily to anything that you or I could understand: it uses its own sorting mechanism.
Dim s As String
Console.WriteLine("The Entries In The Queue")
For Each s In q
Console.WriteLine(s)
Next s
' note that we have to parse the Values of the SortedList, rather than
' the actual SortedList itself
Console.WriteLine("The Entries In The SortedList")
For Each s In sl.Values
Console.WriteLine(s)
Next s
' note that we have to parse the Values of the Hashtable, rather than
' the actual Hashtable itself
Console.WriteLine("The Entries In The SortedList")
For Each s In ht.Values
Console.WriteLine(s)
Next s
VB
So far, you have looked at the code that demonstrates how each of the collections is organized, and how to retrieve their values. However, you also need to consider how to search for values inside each of the different collections. The following code example shows you the commands needed to find a value inside each collection. The methods to search by key and by value are displayed for both SortedList and Hashtable. Recall that hash tables are very good for searching, and sorted lists are fairly good, especially when searching by key. Queues also provide a searching capability, but this is not really a Queue's purpose, and the search mechanism is slower.
' use these lines of code to retrieve the time before the search... Dim startTime as Int64 startTime = Environment.TickCount ' use these lines of code to retrieve the time, after the search. ' You can subtract this value from the above, to get a duration. Dim endTime as Int64 endTime = Environment.TickCount ' use Contains to search a Queue for an entry, ' or a Hashtable/SortedList for a KEY ' Contains returns a boolean, indicating if value/key was found Dim foundValue As Boolean Dim valToFind as Int64 foundValue = q.Contains(valToFind) foundValue = sl.Contains(valToFind) foundValue = ht.Contains(valToFind) ' use ContainsValue to search a SortedList/Hashtable for a VALUE foundValue = sl.ContainsValue(valToFind) foundValue = ht.ContainsValue(valToFind) VB
SummaryWithin the Collections namespace, there are many collections that providing different functionality. It is a good idea to familiarize yourself with the different collections available, so that you know which collection to use in each situation you encounter. For information on more specialized collections, see the Collections.Specialized namespace.
|