Get size of Hashtable in Java

In this tutorial, we will learn how to Get size of 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.

In we want to calculate the size or no. of elements in Hashtable, then this class in Java provides size() method to get size of Hashtable. The size method returns the size or the no. of elements in Hashtable. The syntax of size() method is:

Syntax of size() method:

Returns:
the number of elements in this hashtable

Example:

Output:

Related posts

Leave a Comment