NameValueCollection, ArrayList, Hashtable ...

  • Thread starter Thread starter John Grandy
  • Start date Start date
J

John Grandy

What are good rules of thumb to use when deciding between a
NameValueCollection, an ArrayList, or a Hashtable ?
 
John Grandy said:
What are good rules of thumb to use when deciding between a
NameValueCollection, an ArrayList, or a Hashtable ?

Well, they are different beasts. ArrayList is a straight list with no
association with a key, whereas the other two do associate with a key. Use
it when you just need a list, not a dictionary(a collction where you can
look up a key to find a value).

NameValueCollection only works with strings, so its pretty much limited to
strings. Hashtable will work with any object type and is my preference for a
dictionary.
 
I generally go by the properties of the data structures:

ArrayList: maintains insertion order, no keyed lookup

Hashtable: keyed lookup, no defined order

SortedList: keyed lookup, maintains sorted order

I've also listed them in order of frequency of use: I use ArrayList far
more than Hashtable (because I don't often want keyed lookup), and
SortedList even less frequently (because rarely do I need both keyed
lookup and sorted order, and can't just do Hashtable -> Array and
sort).

Generally, Hashtables and SortedLists point to specialized higher-level
data structures, so I tend to wrap classes around them that offer more
specific services.

ArrayLists get used all over the place in code, casually.
 
Back
Top