Java Hashtable isEmpty() Example

The isEmpty() method of java.util.Hashtable class will test if this hashtable maps no keys to values.

Signature

public boolean isEmpty() 

This method tests if this hashtable maps no keys to values.

Example

The following example shows the usage of java.util.Hashtable.isEmpty() method.

import java.util.Hashtable;

public class IsEmptyMethodHashtableExample 
{
    public static void main(String args[])
    {
        // create an empty Hashtable
        Hashtable<Integer,String>  hashtable1 = new Hashtable<Integer,String>();
        
        // use put() method to put elements to the Hashtable
        hashtable1.put(1,"Element1");
        hashtable1.put(2,"Element2");
        hashtable1.put(4,"Element4");
        hashtable1.put(5,"Element5");
        hashtable1.put(3,"Element3");
        
        //Check if hashtable1 is empty or not
        System.out.println("Is \"hashtable1\" empty : "+hashtable1.isEmpty());
        
        //Lets Clear out the elements of hashtable1
        hashtable1.clear();
        
        //Lets Check again if hashtable1 is empty or not
        System.out.println("Is \"hashtable1\" empty : "+hashtable1.isEmpty());
    }
}

Output

Is "hashtable1" empty : false
Is "hashtable1" empty : true

Leave a Reply

Your email address will not be published. Required fields are marked *