List vs ArrayList vs Dictionary vs Hashtable vs Stack vs Queue?
C#CollectionsC# Problem Overview
We can use any of these (includes List, ArrayList, Dictionary, Hashtable, Stack, Queue) to hold value or hold reference to other objects as a collection.
But, my question is which one is used when?
C# Solutions
Solution 1 - C#
Lists
Lists allow duplicate items, can be accessed by index, and support linear traversal.
-
ArrayList - An array-based list that doesn't support generic types. It does not enforce type safety and should generally be avoided.
-
List
- An array list that supports generic types and enforces type-safety. Since it is non-contiguous, it can grow in size without re-allocating memory for the entire list. This is the more commonly used list collection.
Hashes
Hashes are look-ups in which you give each item in a list a "key" which will be used to retrieve it later. Think of a hash like a table index where you can ask questions like "I'm going to find this object by this string value. Duplicate keys are not allowed.
-
HashTable - A basic key-value-pair map that functions like an indexed list.
-
Dictionary
- A hashtable that supports generic types and enforces type-safety.
Queues
Queues control how items in a list are accessed. You typically push/pop records from a queue in a particular direction (from either the front or back). Not used for random access in the middle.
-
Stack
- A LIFO (last in, first out) list where you push/pop records on top of each other. -
Queue
- A FIFO (first in, first out) list where you push records on top and pop them off the bottom.
Solution 2 - C#
-
List can hold duplicate objects
-
ArrayList is just for compatibility with older versions of the framework where IList didn't exist
-
Dictionary is used to store pairs of key/value. You cannot have duplicate keys.
-
Hashtable is basically a List with no possibility of duplicates (and better performance in some scenarios)
-
Stack stores objects in order they were added (through
Push()
), and when you retrieve an object (throughPop()
) it is removed from the stack in a LIFO manner. -
Queue quite similar to a Stack except it is FIFO.
Solution 3 - C#
Here are some uses for them.
List: If you just want a list and don't care about any duplicates, i.e list of people, shopping list, list of things to do in life.
Queues: If you want to simulate a queue for example, in a hospital you have a queue and also priority queue (in emergency departments). The triage would determine who is in critical condition and needs to be treated.
Another example is a shopping queue, first person in line is 'usually' the first one to checkout.
Stacks: Used in your internal memory to push and pop values as you pass them to functions/methods.
Another interesting use is, in video game inventory method, where you can pick up an item (push) onto the stack, and drop an item (pop) off the stack.
Hash/Dictionary: These are usually seen used in database, for look up and index.
Depending on what you want to simulate, I do agree with the others, it's handy to read up on data-structures. A book helps but the internet also has a wealth of information.