Java Hashtable
Hashtable is a legacy class in Java that stores data in the form of key-value pairs.
It implements the Map interface.
Key -> ValueFor example:
101 -> Ashish
102 -> Rahul
103 -> PriyaHashtable belongs to the java.util package.
import java.util.Hashtable;Why was Hashtable used?
Hashtable was introduced in early versions of Java for storing key-value pairs.
It provides synchronized methods, which means its basic operations are designed for synchronized access.
However, for most modern applications, Hashtable is usually not the first choice. Depending on the requirement, developers commonly use:
- HashMap for general-purpose non-concurrent use.
- ConcurrentHashMap for concurrent access.
- LinkedHashMap when predictable iteration order is required.
- TreeMap when sorted keys are required.
Creating a Hashtable
Hashtable<Integer, String> table =
new Hashtable<>();Adding Elements
Use put().
table.put(101, "Ashish");
table.put(102, "Rahul");
table.put(103, "Priya");Retrieving a Value
Use get().
System.out.println(table.get(101));Output
AshishDuplicate Keys
A Hashtable does not allow duplicate keys.
table.put(1, "Java");
table.put(1, "Python");The second value replaces the first value.
Final mapping:
1 -> PythonDuplicate Values
Duplicate values are allowed.
table.put(1, "Java");
table.put(2, "Java");This is valid.
Null Keys and Values
Unlike HashMap, Hashtable does not allow null keys or null values.
For example:
table.put(null, "Java");This results in an exception.
Similarly:
table.put(1, null);is not allowed.
This is an important difference between Hashtable and HashMap.
Hashtable Does Not Guarantee Sorted Order
Hashtable does not provide sorted key ordering.
It also should not be used when insertion order needs to be preserved.
If order matters, consider LinkedHashMap.
If sorted keys are required, consider TreeMap.
Iterating Through Hashtable
You can iterate through its entries using entrySet():
for (Map.Entry<Integer, String> entry :
table.entrySet()) {
System.out.println(
entry.getKey() + " -> " + entry.getValue()
);
}Important Hashtable Methods
Hashtable provides common Map methods such as:
| Method | Purpose |
|---|---|
| put() | Adds or updates an entry |
| get() | Retrieves a value |
| remove() | Removes an entry |
| containsKey() | Checks for a key |
| containsValue() | Checks for a value |
| size() | Returns number of entries |
| isEmpty() | Checks whether it is empty |
| clear() | Removes all entries |
| keySet() | Returns keys |
| values() | Returns values |
| entrySet() | Returns key-value entries |
Example
import java.util.Hashtable;
public class HelloWorld {
public static void main(String[] args) {
Hashtable<Integer, String> students =
new Hashtable<>();
students.put(101, "Ashish");
students.put(102, "Rahul");
students.put(103, "Priya");
System.out.println(students.get(102));
}
}Output
RahulHashtable and Synchronization
One historical characteristic of Hashtable is that its methods are synchronized.
This means its operations provide synchronized access at the method level.
However, synchronized methods do not automatically make every multi-step operation in an application atomic.
For modern concurrent applications, ConcurrentHashMap is generally a more appropriate choice when concurrent map access is required.
Hashtable vs HashMap
| Hashtable | HashMap |
|---|---|
| Legacy class | Modern general-purpose Map |
| Synchronized methods | Not synchronized |
| Does not allow null keys | Allows one null key |
| Does not allow null values | Allows multiple null values |
| No guaranteed ordering | No guaranteed ordering |
| Usually avoided in new code unless legacy compatibility requires it | Commonly used for general-purpose Map operations |
Hashtable vs ConcurrentHashMap
| Hashtable | ConcurrentHashMap |
|---|---|
| Legacy synchronized Map | Designed for concurrent applications |
| Synchronizes methods | Designed for better concurrent access |
| Does not allow null keys/values | Does not allow null keys/values |
| Older design | Modern concurrent collection |
Hashtable vs LinkedHashMap
| Hashtable | LinkedHashMap |
|---|---|
| Synchronized methods | Not synchronized |
| No guaranteed insertion order | Maintains insertion order by default |
| Does not allow null keys/values | Allows one null key and multiple null values |
| Legacy collection | Modern Map implementation |
Important Points
- Hashtable implements Map.
- It stores key-value pairs.
- Keys are unique.
- Duplicate values are allowed.
- It does not allow null keys.
- It does not allow null values.
- Its methods are synchronized.
- It does not guarantee insertion or sorted order.
- It is a legacy collection class.
- ConcurrentHashMap is generally preferred for modern concurrent applications.
Key Takeaway
Hashtable is an older synchronized Map implementation.
For new Java applications, choose the Map implementation based on the actual requirement:
General key-value storage
↓
HashMap
Need insertion order
↓
LinkedHashMapNeed sorted keys ↓ TreeMap
Need concurrent access ↓ ConcurrentHashMap
Understanding Hashtable is still important because it is part of the Java Collections Framework and is commonly encountered in older Java codebases.
See you in next page....🙂