Java Hashtable Class

Last 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 Declaration

Let's see the declaration for java.util.Hashtable class.

Hashtable Class Parameters

Let's see the Parameters for java.util.Hashtable class.

  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.

Features of Hashtable

Key-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 Class

Hashtable 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.

ConstructorDescription
Hashtable()It creates an empty hashtable having the initial default capacity and load factor.
Hashtable(int capacity)It accepts an integer parameter and creates a hash table that contains a specified initial capacity.
Hashtable(int capacity, float loadFactor)It is used to create a hash table having the specified initial capacity and loadFactor.
Hashtable(Map<? extends K,? extends V> t)It creates a new hash table with the same mappings as the given Map.

Methods of Java Hashtable Class

MethodDescription
void clear()It is used to reset the hash table.
Object clone()It returns a shallow copy of the Hashtable.
V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)It is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)It is used to compute its value using the given mapping function, if the specified key is not already associated with a value (or is mapped to null), and enters it into this map unless null.
V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)It is used to compute a new mapping given the key and its current mapped value if the value for the specified key is present and non-null.
Enumerationelements()It returns an enumeration of the values in the hash table.
Set<Map.Entry<K,V>> entrySet()It returns a set view of the mappings contained in the map.
boolean equals(Object o)It is used to compare the specified Object with the Map.
void forEach(BiConsumer<? super K,? super V> action)It performs the given action for each entry in the map until all entries have been processed or the action throws an exception.
V getOrDefault(Object key, V defaultValue)It returns the value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key.
int hashCode()It returns the hash code value for the Map
Enumeration<K> keys()It returns an enumeration of the keys in the hashtable.
Set<K> keySet()It returns a Set view of the keys contained in the map.
V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
V put(K key, V value)It inserts the specified value with the specified key in the hash table.
void putAll(Map<? extends K,? extends V> t))It is used to copy all the key-value pair from map to hashtable.
V putIfAbsent(K key, V value)If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
boolean remove(Object key, Object value)It removes the specified values with the associated specified keys from the hashtable.
V replace(K key, V value)It replaces the specified value for a specified key.
boolean replace(K key, V oldValue, V newValue)It replaces the old value with the new value for a specified key.
void replaceAll(BiFunction<? super K,? super V,? extends V> function)It replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
String toString()It returns a string representation of the Hashtable object.
Collectionvalues()It returns a collection view of the values contained in the map.
boolean contains(Object value)This method returns true if some value equal to the value exists within the hash table, else return false.
boolean containsValue(Object value)This method returns true if some value equal to the value exists within the hash table, else return false.
boolean containsKey(Object key)This method return true if some key equal to the key exists within the hash table, else return false.
boolean isEmpty()This method returns true if the hash table is empty; returns false if it contains at least one key.
protected void rehash()It is used to increase the size of the hash table and rehashes all of its keys.
V get(Object key)This method returns the object that contains the value associated with the key.
V remove(Object key)It is used to remove the key and its value. This method returns the value associated with the key.
int size()This method returns the number of entries in the hash table.

Constructors

Hashtable()

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 Example

Hashtable1.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: Book

HashtableExample.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