Hashtable in Java

In this tutorial, we will learn about Hashtable in Java.

Hashtable in Java stores key, value pairs. The keys and values cannot be null. The key in Hashtable is hashed, and the resulting hash code is used as the index at which the value is stored within the table. HashTable is similar to HashMap, but it is synchronized.

Hashtable can only store objects that override the hashCode( ) and equals( ) methods that are defined in Object class. The hashCode( ) method computes and returns the hash code for the object. equals( ) method compares two objects. Many of Java’s built-in classes already implement the hashCode( ) method.
For example, the most common type of Hashtable uses a String object as the key. String implements both hashCode( ) and equals( ).

Example:

Let’s run this program and check the output.

Output:

Related posts

Leave a Comment