Difference Between Hashtable and Dictionary

The main difference between Hashtable and Dictionary is that the Hashtable is a weakly typed data structure so it is possible to add keys and values of any type while the Dictionary is a strongly typed data structure so it is only possible to add the elements that satisfy the specified data types for both key and value.

Hashtable and dictionary are two main data structures. They both can hold data as key value pairs. A Hashtable or a Dictionary can only contain unique keys. The programmer can find or remove a specific value using the key. Hashtable is not strongly typed. Therefore, the programmer can add any element of key-value pair to the Hashtable. On the other hand, the Dictionary is strongly typed. The programmer should specify the data types of the keys and values. It is not possible to add elements that do not satisfy the given data types. For example, if the Dictionary has generic type <int, string>, the programmer can only add elements with int type keys and string type values.

Key Areas Covered

1. What is Generic Data Type
     – Definition
2. What is Hashtable
     – Definition, Functionality
3. What is Dictionary
     – Definition, Functionality
4. Difference Between Hashtable and Dictionary
     – Comparison of Key Differences

Key Terms

Dictionary, Hashtable, Data Structures

Difference Between Hashtable and Dictionary - Comparison Summary

What is Generic Data Type

A generic data type is a data type that allows the user to define classes and methods with placeholders. Compilers such as C# can replace the placeholders with specified data type at compile time. It is used to create general purpose classes and methods. When defining a generic class, the programmer should use angle brackets (<>). These brackets declare a class or method as generic type.

What is Hashtable

A Hashtable is a data structure that implements an associative array abstract data type, a structure that can map key to values. It  is not a generic data type.  Refer a piece of code as follows.

Difference Between Hashtable and Dictionary

Figure 1: C# program with Hashtable

In the above program, numbers is an object of type Hashtable. Key-value pairs are added to the Hashtable.  From statement 15 to 17, the keys are of type int and the values are of type string. In statement 18, the key and value are strings. A Hashtable is a weekly typed data structure. Therefore, the programmer can add keys and values of any type to the Hashtable.

Main Difference - Hashtable vs Dictionary

Figure 2: HashTable Program output

The output of the program is as above. The Hashtable does not print the records according to order in which the elements where added. Therefore, it does not maintain the inserted order.

What is a Dictionary

A dictionary uses the concept of Hashtable. It is a generic data type.  Refer the below program. names is a Dictionary type object. It has specific data types to store for keys and values. According to this program, the generic types are defined as <string,string>. Therefore, when adding elements to the dictionary, both keys and values should be of type string.

Difference Between Hashtable and Dictionary_Figure 3

Figure 3: C# program with Dictionary

All the elements added to the above dictionary have key and value pairs of type String.  Printing the key and values to the console will give an output similar to figure 4. Retrieving the items from Dictionary will print the records in the same order as the elements were added. Therefore, it maintains the inserted order.

Difference Between Hashtable and Dictionary _ Figure 4

Figure 4: Dictionary Program Output

Overall, Dictionary is fast in execution but it does not provide thread safety.

Difference Between Hashtable and Dictionary

Definition

A Hashtable is a data structure that implements an associative array abstract data type, a structure that can map key to values. A Dictionary is a data structure based on Hashtable that stores values on the basis of keys.

Adding Elements

Furthermore, Hashtable is a weakly typed data structure. Therefore, the programmer can add keys and values of any object type to the Hashtable. A Dictionary is strongly typed data structure. (<TKey, TValue). Therefore, the programmer can only add the elements that satisfy the specified data types for both key and value. This is the key difference between Hashtable and Dictionary.

Type

Also, Hashtable is not generic while Dictionary is generic type.

Execution Speed

In addition, there is boxing and unboxing in Hashtable but not in Dictionary. The Dictionary consumes minimum memory and executes faster than the Hashtable.

Retrieving the Elements

Retrieving the elements in the Hashtable does not maintain the inserted order. On the other hand, retrieving the elements from a Dictionary maintains the inserted order.

Thread Safety

Moreover, Hashtable provides more thread safety. It supports multiple reader threads and a single writer thread. Dictionary does not provide thread safety. To implement thread safety, the programmer should write his own synchronized code.

Example Syntax

Hashtable ht = new Hashtable();

Dictionary<string, string> dictionary = new Dictionary<string, string>();.

Conclusion

The difference between Hashtable and Dictionary is that the Hashtable is a weakly typed data structure so it is possible to add keys and values of any type while the Dictionary is a strongly typed data structure so it is only possible to add the elements that satisfied the specified data types for both key and value.

Reference:

1. “C# Hashtable Class.” Www.tutorialspoint.com, Tutorials Point, 21 July 2018, Available here.
2. “C# Hashtable.” Data Types in C#, Tutorials Teacher. Available here.
3. “C# Dictionary – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply