Home » Core Java » util » Hashtable » Get size of Hashtable example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Get size of Hashtable example

This is an example of how to get the size of the Hashtable, that is the number of the key- value pairs that the Hashtable contains. Getting the size of the Hashtable implies that you should:

  • Create a new Hashtable.
  • Populate the hashtable with elements, using put(K key, V value) API method of Hashtable.
  • Invoke the size() API method of the Hashtable. It returns the number of keys of Hashtable.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
import java.util.Hashtable;
public class SizeHashtable {
  public static void main(String[] args) {
    // Create a Hashtable and populate it with elements
    Hashtable hashtable = new Hashtable();
    // int size() operation returns the number of key value pairs stored in Hashtable    
    System.out.println("Size of Hashtable : " + hashtable.size());


Size of Hashtable : 3

This was an example of how to get the size of the Hashtable in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

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

Inline Feedbacks
View all comments