Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List vs ArrayList vs Dictionary vs Hashtable vs Stack vs Queue? [closed]

Tags:

c#

collections

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?

like image 606
Sujit Avatar asked Aug 10 '12 12:08

Sujit


People also ask

Is a Hashtable a List?

More accurately, a List is a collection of elements, and a Hashtable or Dictionary is a collection of elements along with a unique key to be used to access each one.

What is the difference between a Hashtable and a dictionary?

A dictionary is a data structure that maps keys to values. A hash table is a data structure that maps keys to values by taking the hash value of the key (by applying some hash function to it) and mapping that to a bucket where one or more values are stored.

What is the difference between dictionary and ArrayList in C#?

2. ArrayList is a non generic collection class to store items. The corresponding generic collection class is List<t> which is preferable for the same reason mentioned above. If the values are required to be accessed through their keys then Dictionary<KEY, VALUE> collection class is suitable.

What is the difference between Dictionary and Hashtable in C#?

In Hashtable, you can store key/value pairs of the same type or of the different type. In Dictionary, you can store key/value pairs of same type. In Hashtable, there is no need to specify the type of the key and value. In Dictionary, you must specify the type of key and value.


1 Answers

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.

like image 107
Jordan Parmer Avatar answered Sep 28 '22 05:09

Jordan Parmer