In most programming languages, dictionaries are preferred over hashtables. What are the reasons behind that?
For what it's worth, a Dictionary is (conceptually) a hash table.
If you meant "why do we use the Dictionary<TKey, TValue>
class instead of the Hashtable
class?", then it's an easy answer: Dictionary<TKey, TValue>
is a generic type, Hashtable
is not. That means you get type safety with Dictionary<TKey, TValue>
, because you can't insert any random object into it, and you don't have to cast the values you take out.
Interestingly, the Dictionary<TKey, TValue>
implementation in the .NET Framework is based on the Hashtable
, as you can tell from this comment in its source code:
The generic Dictionary was copied from Hashtable's source
Source
Collections
& Generics
are useful for handling group of objects. In .NET, all the collections objects comes under the interface IEnumerable
, which in turn has ArrayList(Index-Value))
& HashTable(Key-Value)
. After .NET framework 2.0, ArrayList
& HashTable
were replaced with List
& Dictionary
. Now, the Arraylist
& HashTable
are no more used in nowadays projects.
Coming to the difference between HashTable
& Dictionary
, Dictionary
is generic where as Hastable
is not Generic. We can add any type of object to HashTable
, but while retrieving we need to cast it to the required type. So, it is not type safe. But to dictionary
, while declaring itself we can specify the type of key and value, so there is no need to cast while retrieving.
Let's look at an example:
HashTable
class HashTableProgram
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
foreach (DictionaryEntry de in ht)
{
int Key = (int)de.Key; //Casting
string value = de.Value.ToString(); //Casting
Console.WriteLine(Key + " " + value);
}
}
}
Dictionary,
class DictionaryProgram
{
static void Main(string[] args)
{
Dictionary<int, string> dt = new Dictionary<int, string>();
dt.Add(1, "One");
dt.Add(2, "Two");
dt.Add(3, "Three");
foreach (KeyValuePair<int, String> kv in dt)
{
Console.WriteLine(kv.Key + " " + kv.Value);
}
}
}
People are saying that a Dictionary is the same as a hash table.
This is not necessarily true. A hash table is one way to implement a dictionary. A typical one at that, and it may be the default one in .NET in the Dictionary
class, but it's not by definition the only one.
You could equally well implement a dictionary using a linked list or a search tree, it just wouldn't be as efficient (for some metric of efficient).
Because Dictionary
is a generic class ( Dictionary<TKey, TValue>
), so that accessing its content is type-safe (i.e. you do not need to cast from Object
, as you do with a Hashtable
).
Compare
var customers = new Dictionary<string, Customer>();
...
Customer customer = customers["Ali G"];
to
var customers = new Hashtable();
...
Customer customer = customers["Ali G"] as Customer;
However, Dictionary
is implemented as hash table internally, so technically it works the same way.
The Extensive Examination of Data Structures Using C# article on MSDN states that there is also a difference in the collision resolution strategy:
The Hashtable class uses a technique referred to as rehashing.
Rehashing works as follows: there is a set of hash different functions, H1 ... Hn, and when inserting or retrieving an item from the hash table, initially the H1 hash function is used. If this leads to a collision, H2 is tried instead, and onwards up to Hn if needed.
The Dictionary uses a technique referred to as chaining.
With rehashing, in the event of a collision the hash is recomputed, and the new slot corresponding to a hash is tried. With chaining, however, a secondary data structure is utilized to hold any collisions. Specifically, each slot in the Dictionary has an array of elements that map to that bucket. In the event of a collision, the colliding element is prepended to the bucket's list.
Dictionary<>
is a generic type and so it's type safe.
You can insert any value type in HashTable and this may sometimes throw an exception. But Dictionary<int>
will only accept integer values and similarly Dictionary<string>
will only accept strings.
So, it is better to use Dictionary<>
instead of HashTable
.