System.Collections

A

a_pess

' i face the following problem while using different types of system
collection classes
while using Hashtables as shown in the example below, the week days
will be shown in combobox1 start with "Wed"
and will start with "Sat" while using Stack and other collections are
ok
I wonder if i miss something here, or what is going on Exactly

i.am using VS2008 if that help
-----------------------------------------

Case 1 - Hashtable
'
' Create a new Hashtable.
Dim Hweeks As New Hashtable()


' Add some elements to the Hashtable
Hweeks.Add("1", "Sun")
Hweeks.Add("2", "Mon")
Hweeks.Add("3", "Tue")
Hweeks.Add("4", "Wed")
Hweeks.Add("5", "Thu")
Hweeks.Add("6", "Fri")
Hweeks.Add("7", "Sat")
'
' Create a new DictionaryEntry.
Dim Hday As DictionaryEntry

For Each Hday In Hweeks

ComboBox1.Items.Add(Hday.Value)
Next


' Case 2 - Stack
'
' Create a new Stack.
Dim stackTable As New Stack()

' Add some elements to the Stack

stackTable.Push("Sun")
stackTable.Push("Mon")
stackTable.Push("Tue")
stackTable.Push("Wed")
stackTable.Push("Thu")
stackTable.Push("Fri")
stackTable.Push("Sat")

For s As Integer = 0 To stackTable.Count - 1

ComboBox2.Items.Add(stackTable.ToArray(s))

Next
------------------------------------------------------------------------------------------
' Case 3 - Queue
'
' Create a new Queue.
Dim queueList As New Queue()

' Add some elements to the Queue
queueList.Enqueue("Sun")
queueList.Enqueue("Mon")
queueList.Enqueue("Tue")
queueList.Enqueue("Wed")
queueList.Enqueue("Thu")
queueList.Enqueue("Fri")
queueList.Enqueue("Sat")

For q As Integer = 0 To queueList.Count - 1

ComboBox3.Items.Add(queueList.ToArray(q))
Next

' Case 4 - ArrayList
'
' Create a new ArrayList.
Dim ItemList As New ArrayList()

' Add some elements to the ArrayList
ItemList.Add("Sun")
ItemList.Add("Mon")
ItemList.Add("Tue")
ItemList.Add("Wed")
ItemList.Add("Thu")
ItemList.Add("Fri")
ItemList.Add("Sat")

For a As Integer = 0 To ItemList.Count - 1

ComboBox4.Items.Add(ItemList.Item(a))

Next

' Case 5 - DictionaryOfString
'
' Create a new dictionary of strings.
Dim DictionaryOfString As New Dictionary(Of String, String)

' Add some elements to the dictionary.
DictionaryOfString.Add("1", "Sun")
DictionaryOfString.Add("2", "Mon")
DictionaryOfString.Add("3", "Tue")
DictionaryOfString.Add("4", "Wed")
DictionaryOfString.Add("5", "Thu")
DictionaryOfString.Add("6", "Fri")
DictionaryOfString.Add("7", "Sat")

For Each kvp As KeyValuePair(Of String, String) In
DictionaryOfString
ComboBox5.Items.Add(kvp.Value)
Next


' Case 6 - SortedList
'
' Creates and initializes a new SortedList.
Dim SL As New SortedList()

' Add some elements to the SortedList.
SL.Add("1", "Sun")
SL.Add("2", "Mon")
SL.Add("3", "Tue")
SL.Add("4", "Wed")
SL.Add("5", "Thu")
SL.Add("6", "Fri")
SL.Add("7", "Sat")

For l = 0 To SL.Count - 1

ComboBox6.Items.Add(SL.GetByIndex(l))

Next

thanks
Omar
 
J

Jon Skeet [C# MVP]

a_pess said:
' i face the following problem while using different types of system
collection classes
while using Hashtables as shown in the example below, the week days
will be shown in combobox1 start with "Wed"
and will start with "Sat" while using Stack and other collections are
ok
I wonder if i miss something here, or what is going on Exactly

Hashtables aren't ordered - they're just maps from keys to values.

Stacks are "last in, first out" which is why you're seeing Sat first.
 
C

Ciaran O''Donnell

Hastables are actually ordered in a way, they have a tree like structure
underneath the covers which makes best attempts to be as balanced as
possible, thats why wednesday is first as it is the middle value, and
conceptually, the tree will spread out blow it.
 
J

Jon Skeet [C# MVP]

Ciaran O''Donnell said:
Hastables are actually ordered in a way, they have a tree like structure
underneath the covers which makes best attempts to be as balanced as
possible, thats why wednesday is first as it is the middle value, and
conceptually, the tree will spread out blow it.

I don't believe hashtables are particularly treelike actually.
Otherwise they'd have O(log n) performance instead of amortised O(1)
performance.

The fact that Wednesday comes out first is an artefact of the hash code
produced, and the number of entries and buckets that happened to be in
use at the time. (It's also the key that's relevant here, not the
value.)

For example, if you change the constructor call for Hashtable to be
new Hashtable(10, 0.5f);
then the ordering changed.

Likewise if you add an extra entry, the order changes again.

Basically you should never *rely* on the ordering that comes out of a
hashtable. While there is a reasoning behind it, it will be complex and
could even vary between versions of the framework.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top