Example
In this example we are gonna see how to get the size of Hashtable. We will be using size() method of Hashtable class to perform this operation.
import java.util.Hashtable;
public class SizeExample{
public static void main(String[] args) {
// Creating a Hashtable instance
Hashtable<String, String> hashtable = new Hashtable<String, String>();
// Adding key-value pairs to Hashtable
hashtable.put("A", "Apple");
hashtable.put("B", "Orange");
hashtable.put("C", "Mango");
hashtable.put("D", "Banana");
hashtable.put("E", "Grapes");
/* int size(): method returns number of key value pairs present
* in this hashtable.
*/
System.out.println("Size of Hashtable: " + hashtable.size());
}
}
Output:
Size of Hashtable: 5
Leave a Reply