java.util.Hashtable
is a legacy class in the Java Collections Framework that provides a data structure to store key-value pairs. It was part of the original version of Java and is based on the principle of a hash table, where each key is hashed internally to determine its storage location. A key feature of Hashtable
is that it is synchronized, meaning it is thread-safe and can be safely used in multi-threaded environments without external synchronization. However, this also makes it slower compared to unsynchronized alternatives like HashMap
. Unlike HashMap
, Hashtable
does not allow null
keys or null
values—attempting to insert a null will result in a NullPointerException
. The class implements the Map
interface and inherits from the Dictionary
class, making it compatible with both old and new Java APIs. It provides essential methods such as put()
, get()
, remove()
, containsKey()
, containsValue()
, and size()
, along with enumerations to iterate over keys and values.
Although
Hashtable
is still available for backward compatibility, modern applications often preferHashMap
orConcurrentHashMap
for better performance and scalability.
For more related practice…
Hashtable