Java Hashtable ClassLast Updated : 12 Jan 2026 Hashtable in Java is a data structure in which each key is unique and is used to store key-value pairs. It belongs to Java.util package, which implements Map interface in order to work with the elements it includes. An important characteristic of a Hashtable is its capability of providing rapid lookup and insertion operations. Internally, a Hash Table uses a hash function to determine an index into an array of buckets or slots, where each bucket identifies a potential location where a key-value pair can be stored. When we add an entry to the Hashtable, the hash code of the key is calculated, and the particular bucket in which the entry will be stored is retrieved based on the hash code. If bucket already has an entry, collision resolution process occurs, where the new entry is either appended to the bucket or stored in another location. About usage, Hashtable is just like HashMap, but it is synchronized which, so it is thread-safe. This entails that several threads can operate a Hashtable concurrently without causing data corruption. On the other hand, the synchronization can slow down the performance, so if there is no requirement of synchronization, HashMap can be a better option. Hashtable Class DeclarationLet's see the declaration for java.util.Hashtable class. Hashtable Class ParametersLet's see the Parameters for java.util.Hashtable class.
Features of HashtableKey-Value Mapping: Contrary to other Map implementations, Hashtable stores key-value pairs. Its key-value store allows direct retrieval of a value by its key. Synchronization: Hashtable is synchronized because it is thread safe. Many threads can access and modify a Hashtable simultaneously without incurring data corruption. To the contrary, such synchronization might damage the workload, especially for the highly concurrent applications. No Null Keys or Values: However, Hashtable does not allow null keys or values. Trying to put a null key or value will generate a NullPointerException. Hashtable Iteration: One can traverse the items of a Hashtable using numerous ways, such as keySet(), values(), and entrySet(). They return collections to which you can apply various methods which will allow you to loop through the keys, values or key-value pairs respectively. Performance: A hashtable does constant time performance for both basic operations like get() and put() under normal circumstances. On the other hand, it can perform slowly under heavy concurrent access in substitute of unsyncronized collections like HashMap. Resizing: HashTable has a default resizing mechanism that automatically enlarges itself when more elements need to be stored. With the number of elements in the range of threshold (load factor), Hashtable internally multiplies its capacity and rehash the elements. Enumeration: The Hashtable class also implements the Enumeration interface for going over its elements. The interface is old and inferior to iterators or enhanced for loop features, but it is there for compatibility issues with old Java code. Legacy Class: A hashtable is a legacy class which has been part of Java since its early versions. Primarily, it has been replaced by the more robust HashMap and ConcurrentHashMap classes, which do the same thing, but with better performance and lots of extra functionality. Nevertheless, Hashtable can be found in legacy projects or in instances where thread safety is the primary concern. Constructors of Java Hashtable ClassHashtable class provides several constructors to create instances of a hashtable. Each constructor allows us to initialize the hashtable in different ways, providing flexibility based on your requirements.
Methods of Java Hashtable Class
ConstructorsHashtable()The Hashtable() constructor initializes a new empty hashtable. It sets the initial capacity to a default value (usually 11) and the load factor to the default value of 0.75. The capacity and load factor are internal parameters that affect the hashtable's performance and resizing behavior as elements are added. HashTableExample1.java Output:
Hashtable: {}
Hashtable(int initialCapacity)The Hashtable(int initialCapacity) constructor initializes a new empty hashtable with the specified initial capacity. The initial capacity is the number of buckets used to store key-value pairs, and it affects the hashtable's performance and memory usage. The default load factor of 0.75 determines when the hashtable should resize to accommodate more elements. HashTableExample2.java Output:
Hashtable: {}
Hashtable(int initialCapacity, float loadFactor)The Hashtable(int initialCapacity, float loadFactor) constructor initializes a new empty hashtable with the specified initial capacity and load factor. The initial capacity determines the number of buckets, while the load factor determines when the hashtable should resize. Specifying a custom load factor can influence the hashtable's resizing behavior based on the expected number of elements and memory constraints. HashTableExample3.java Output:
Hashtable: {}
Hashtable(Map<? extends K, ? extends V> t)The Hashtable(Map<? extends K, ? extends V> t) constructor initializes a new hashtable with the same mappings as the specified map. It copies all key-value pairs from the specified map into the new hashtable, effectively cloning its contents. This constructor provides a convenient way to create a hashtable with predefined mappings from an existing map. HashTableExample4.java Output:
Hashtable: {One=1, Three=3, Two=2}
Java Hashtable ExampleHashtable1.java Output: 103 Rahul 102 Ravi 101 Vijay 100 Amit Java Hashtable Example: remove()Hashtable2.java Output:
Before remove: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
After remove: {103=Rahul, 101=Vijay, 100=Amit}
Java Hashtable Example: getOrDefault()Hashtable3.java Output: Vijay Not Found Java Hashtable Example: putIfAbsent()Hashtable4.java Output:
Initial Map: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
Updated Map: {104=Gaurav, 103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
Updated Map: {104=Gaurav, 103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
Java Hashtable Example: BookHashtableExample.java Output: 3 Details: 103 Operating System Galvin Wiley 6 2 Details: 102 Data Communications & Networking Forouzan Mc Graw Hill 4 1 Details: 101 Let us C Yashwant Kanetkar BPB 8 Next TopicDifference between HashMap and Hashtable |
We request you to subscribe our newsletter for upcoming updates.