Hashtable
About
A Hashtable
in Java is a legacy class that implements the Map
interface and is part of the java.util
package. It is used to store key-value pairs, similar to HashMap
. However, it is synchronized and thread-safe, making it suitable for concurrent programming where multiple threads access the data simultaneously.
Introduced: Java 1.0
Thread Safety: Synchronized but less efficient than modern alternatives.
Null Handling: Does not allow
null
keys ornull
values.
Features
Key-Value Mapping: Stores key-value pairs where keys are unique.
Thread-Safe: All methods are synchronized, making it suitable for multithreaded environments.
No Null Keys or Values: Unlike
HashMap
,null
keys ornull
values are not allowed.Dynamic Resizing: Automatically resizes when the load factor exceeds the threshold.
Enumerations: Supports legacy
Enumeration
for traversing elements.Hashing Mechanism: Uses hashing to store and retrieve elements efficiently.
Internal Working
Data Storage
Data is stored in an internal array of buckets.
Each bucket is a linked list of key-value pairs, used for collision handling.
Hashing Mechanism
A key's
hashCode()
is computed to determine its bucket index in the array.The hash is modulated by the array size to fit the key into a valid index.
Collision Handling
Uses Separate Chaining: If two keys hash to the same index, they are stored as a linked list in the same bucket.
During retrieval, the list is traversed, and keys are compared using
equals()
.
Synchronization
All operations (like
get
,put
,remove
) are synchronized using the object-level lock on theHashtable
.This prevents simultaneous access by multiple threads but can cause contention and degrade performance.
Load Factor and Resizing
The default load factor is 0.75.
When the size exceeds the capacity × load factor, the table resizes by doubling its size and rehashing all entries.
Key Methods for Hashing
The
hashCode()
method is used to compute the hash.The
equals()
method checks if two keys are equal.
Key Methods
Method
Description
put(K key, V value)
Associates the specified value with the specified key.
get(Object key)
Returns the value associated with the specified key, or null
if the key is not present.
remove(Object key)
Removes the key-value pair for the specified key.
containsKey(Object key)
Checks if the table contains the specified key.
containsValue(Object value)
Checks if the table contains the specified value.
size()
Returns the number of key-value pairs.
isEmpty()
Checks if the table is empty.
clear()
Removes all key-value pairs.
elements()
Returns an enumeration of the values.
keys()
Returns an enumeration of the keys.
Big-O for Operations
Operation
Average Case
Worst Case
Put/Get (No Collision)
O(1)
O(n)
Remove
O(1)
O(n)
Contains Key/Value
O(1)
O(n)
Iteration
O(n)
O(n)
Limitations
Performance: Synchronization causes slower performance compared to non-synchronized collections like
HashMap
.No Null Keys/Values:
Hashtable
does not allownull
keys ornull
values, unlikeHashMap
.Legacy Class: Considered obsolete and rarely used in modern applications;
ConcurrentHashMap
is preferred for thread-safe operations.Iteration Mechanism: Does not support modern Java iteration mechanisms like the enhanced
for
loop or streams.
Real-World Usage
Thread-Safe Caches: Used in multithreaded applications where key-value pairs are cached.
Small-Scale Legacy Systems: Often found in older Java codebases written before Java 2 Collections Framework.
Configuration Management: Used for storing configuration settings in key-value format where thread safety is essential.
Examples
1. Basic Operations
2. Iteration Using Enumeration
3. Thread-Safe Usage
4. Attempting Null Key or Value
Last updated
Was this helpful?