All IT Courses 50% Off
JAVA Tutorials

Hashtable in Java

A hash table that associates keys with values is implemented by the Hashtable class. You can use any non-null object as a value or as a key. The hashCode and equals methods must be implemented by the objects used as keys in order for them to be effectively stored and retrieved from a hashtable.  A hash table that associates keys with values is implemented by this class. You can use any non-null object as a value or as a key. Hashtable and HashMap are comparable, but Hashtable is synchronised.

Java offers a key-value data structure called java.util.Hashtable, which is comparable to the Map interface. It was first introduced in Java 1.0 and was a component of the original Java Collections framework.

But since then, the Hashtable class has been deemed outdated, and using it is usually discouraged. This is because it was created before the Collections framework was released and does not use the Map interface, which makes it challenging to integrate with other framework components. Furthermore, the Hashtable class is synchronised, which may cause it to run less quickly than other Map interface implementations.

The Hashtable class should generally be replaced with the Map interface or one of its implementations (such as HashMap or ConcurrentHashMap).

This tutorial will show you how to construct a hashtable, fill it with entries, and use enumeration to display the key-value pairs inside of it. To learn more, check out the free Java course online.

All IT Courses 50% Off

Methods of Hashtable Java class:

  • void clear(): it makes a hashtable empty and removes any key-value mappings. Make this hashtable key-free by clearing it.
  • Object clone(): Produces a hashtable shallow copy. The hashtable’s whole structure is duplicated, but its keys and values are not. This procedure is somewhat pricey.
  • boolean contains(Object value): This function determines whether a key in this hashtable matches to the given value. The containsKey technique is less expensive than this procedure. Keep in mind that the containsValue method, which is a component of the Map interface in the collections framework, functions just like this one.
  • boolean isEmpty(): Verifies if there are any keys to values in this hashtable.
  • Enumeration keys(): Provides an enumeration of the hash table’s keys.
  • Object put(Object key, Object value): maps the given value in this hashtable to the given key.
  • void rehash(): This function rehashes every key in the hash table and expands its size.
  • Object remove(Object key): This function clears this hashtable of the key and its associated value.
  • int size(): This function gives back the total amount of key-value mappings in the hashtable.
  • String toString(): gives back a hash table’s equivalent in string form.
  • boolean containsKey(Object key): Determines whether the given object is a hashtable key by checking for it.
  • boolean containsValue(Object value): Determines whether the object passed in is one of the values in this hashtable. returns true if there is a value in the hash table that matches the given value. If the value cannot be located, returns false.
  • Enumeration elements(): This function yields a list of all the values in the hash table.
  • Object get(Object key): Returns null if there is no mapping for the key in this map, or the value to which the given key is mapped.
Hashtable in Java

In conclusion, it is generally advised to utilise the Map interface or one of its implementations, even though the Hashtable class is still supported in Java.

Features of Hashtable in Java

  • Though synchronised, it is comparable to HashMap.
  • Key/value pairs are stored in hash tables via hashtables.
  • An object that serves as a key and the value we wish to assign to it are both specified in a hashtable. After the key has been hashed, the value is placed in the table at the index determined by the hashed hash code.
  • The loadFactor is 0.75 and the initial default capacity of the Hashtable class is 11.
  • Whereas hash tables do not offer fail-fast enumeration, hashmaps do not offer any.

The Hierarchy of Hashtable in Java

Hashtable extends Dictionary<K,V> and implements Serializable, Cloneable, and Map<K,V> interfaces. Properties and UIDefaults are the direct subclasses.


We must import java.util.Hashtable before we can create a hashtable. There are several methods available to us for building a hashtable.

  1. Hashtable(): This function generates a blank hashtable with an initial capacity of 11 and a default load factor of 0.75. 
  2. Hashtable(int initialCapacity): This generates a hash table with a default load factor of 0.75 and an initial size indicated by initialCapacity.
  3. Hashtable(float fillRatio, int size): This version builds a hash table with fillRatio defining the fill ratio and size defining the beginning size. ratio of fill: In essence, it establishes the maximum size of a hash table that it can have before being resized upward; its value ranges from 0.0 to 1.0.
  4. Hashtable(Map<? extends K,? extends V> m): This uses the entries in m as an initialization to generate a hash table.

Performing Various Operations on Hashtable                  

  1. Adding Elements: We can use the put() method to add an element to the hashtable. Nevertheless, the hashtable does not maintain the insertion order. To increase efficiency, an internal hash is created for each element, and the elements are indexed using this hash.  
  2. Changing Elements: We can use the put() function to add the element again if we want to edit it after it has been added. Since the keys are used to index the elements in the hashtable, all it takes to alter the value of a key is to insert the updated value for the key that needs to be modified.
  3. Removing Element: We can use the remove() method to take an element out of the Map. If a mapping for a key exists in this map, it is removed using the key value in this method.
  4. Traversal of a Hashtable: An enhanced for loop can be used to iterate the table. 

Internal Working of Hashtable

The key/value pairs are stored in an array of buckets that make up the hashtable data structure. The hashCode() function is used to identify the bucket to which the key-value combination should map.

The location of a specific key in the bucket list can be found with the use of the hash function.  A hashcode is often a non-negative integer that may or may not be equal for unequal objects and equal for equal objects. Hashtable uses the equals() method to detect if two objects are equal or not.

A hashcode shared by two unequal objects is conceivable. We refer to this as a collision. Hashtable makes use of an array of lists to resolve collisions. List references are kept in the array index, and pairs mapped to a single bucket (array index) are kept in a list.

Methods of Hashtable

K – The type of the keys in the map.

V – The type of values mapped in the map.

Advantages of Hashtable:

  • Thread-safe: Since the Hashtable class is thread-safe, it may be accessed by multiple threads at once without leading to synchronisation problems or data corruption.
  • Easy to use: For straightforward situations, the Hashtable class might be helpful as it offers basic key-value data structure capability.
Hashtable in Java

The disadvantages of Hashtable

  • Outdated: It is often discouraged to utilise the Hashtable class since it is thought to be outdated. This is because it was created before the Collections framework was released and does not use the Map interface, which makes it challenging to integrate with other framework components.
  • Limited functionality: The Map interface and its implementations offer a wider range of capabilities than the Hashtable class, which only offers basic key-value data structure capability.
  • Poor performance: Compared to other Map interface implementations like HashMap or ConcurrentHashMap, the Hashtable class may execute more slowly since it is synchronised.

Conclusion To learn more about HashTables in Java, check our Java training online.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button