Java Utility Library

Java Hashtable - size() Method



The java.util.Hashtable.size() method returns the total number of keys present in the hashtable.

Syntax

public int size()

Parameters

No parameter is required.

Return Value

Returns the total number of keys present in the hashtable.

Exception

NA

Example:

In the example below, the java.util.Hashtable.size() method returns the total number of keys present in the given hashtable.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a hashtable
    Hashtable<Integer, String> Htable = new Hashtable<Integer, String>();

    //populating hashtable
    Htable.put(101, "John");
    Htable.put(102, "Marry");
    Htable.put(103, "Kim");
    Htable.put(104, "Jo");

    System.out.println("Size of Htable: "+ Htable.size());    

    //adding one more key
    Htable.put(105, "Ramesh"); 

    System.out.println("Size of Htable: "+ Htable.size());  
  }
}

The output of the above code will be:

Size of Htable: 4
Size of Htable: 5

❮ Java.util - Hashtable