Java Hashtable

Java Hashtable Class

Last Updated : 29 Mar 2026

Java Hashtable is used for storing key-value pairs where each key is unique, and it provides fast data retrieval and insertion operations. It is commonly used in applications where thread-safe data storage is required.

In this chapter, you will learn about the Java Hashtable class, its working, features, and how it stores and retrieves data using a hash table mechanism.

What is Java Hashtable?

Java Hashtable is a class in the java.util package that implements the Map interface and is used to store key-value pairs with unique keys. It uses a hash table data structure internally, where a hash function determines the index of elements stored in buckets.

When a key-value pair is inserted, the hash code of the key is calculated to identify the appropriate bucket. If multiple elements map to the same bucket, a collision resolution technique is applied to store them properly.

Hashtable is similar to HashMap but is synchronized which makes it thread-safe. This allows multiple threads to access it without data inconsistency. However, due to synchronization, its performance may be slower compared to HashMap when thread safety is not required.

Hashtable Class Declaration

The following is the declaration of the java.util.Hashtable class:

Hashtable Class Parameters

The following are the parameters used in the java.util.Hashtable class.

  • K: It represents the type of keys maintained by the map.
  • V: It represents the type of values mapped to the keys.

Features of Hashtable

Several features of Hashtable are as follows:

  • Key-Value Mapping: Hashtable stores data in key-value pairs that allows direct access to values using their corresponding keys.
  • Synchronization: Hashtable is synchronized, which makes it thread-safe. Multiple threads can access and modify it without causing data inconsistency, although this may impact performance.
  • No Null Keys or Values: Hashtable does not allow null keys or null values. Attempting to insert them results in a NullPointerException.
  • Hashtable Iteration: Elements of a Hashtable can be traversed using methods like keySet(), values(), and entrySet(), which provide collections for iteration.
  • Performance: Hashtable generally provides constant-time performance for operations like get() and put(), but it may be slower compared to non-synchronized collections like HashMap in highly concurrent scenarios.
  • Resizing: Hashtable automatically resizes itself when the number of elements exceeds a certain threshold, increasing capacity and rehashing existing entries.
  • Enumeration: Hashtable supports the Enumeration interface for iterating over elements, mainly for compatibility with older Java code.

Constructors of Hashtable Class

Java Hashtable provides several constructors to create and initialize a hashtable in different ways.

1. Hashtable()

It creates an empty hashtable with the default initial capacity and load factor.

Here is syntax:

2. Hashtable(int capacity)

It creates a hashtable with the specified initial capacity.

Here is syntax:

3. Hashtable(int capacity, float loadFactor)

It creates a hashtable with the specified initial capacity and load factor.

Here is syntax:

4. Hashtable(Map t)

It creates a new hashtable containing the same mappings as the given map.

Here is syntax:

Methods of Hashtable Class

The Hashtable class provides various methods to perform operations such as inserting, deleting, searching, and iterating over key-value pairs in a thread-safe manner.

The following table lists the commonly used methods of the Java Hashtable class along with their descriptions:

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.

Examples of Java Hashtable Class

The following examples demonstrate how to use Java Hashtable for storing, removing, and performing operations on key-value pairs.

Example 1: Storing and Iterating Hashtable Elements

This example demonstrates how to store key-value pairs in a Hashtable and iterate through them.

Output:

103 Rahul
102 Ravi
101 Vijay
100 Amit

Example 2: Removing Elements using remove()

This example demonstrates how to remove an element from a Hashtable.

Output:

Before remove: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
After remove: {103=Rahul, 101=Vijay, 100=Amit}

Example 3: Using getOrDefault() Method

This example demonstrates how to use the getOrDefault() method.

Output:

Example 4: Using putIfAbsent() Method

This example demonstrates how to use the putIfAbsent() method.

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}

Example 5: Storing Custom Objects in Hashtable

This example demonstrates how to store custom objects in a Hashtable.

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