Java Utility Library

Java Dictionary - isEmpty() Method



The java.util.Dictionary.isEmpty() method is used to check whether the dictionary maps no keys to value or not. It returns true if the dictionary contains no entries, else returns false.

Syntax

public abstract boolean isEmpty()

Parameters

No parameter is required.

Return Value

Returns true if this dictionary maps no keys to values, returns false otherwise.

Exception

NA

Example:

In the example below, the java.util.Dictionary.isEmpty() method is used to check whether the given dictionary is empty or not.

import java.util.*;

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

    //checking the dictionary for empty
    System.out.println("Is Dict empty?: " + Dict.isEmpty());

    //populating the dictionary
    Dict.put(101, "John");
    Dict.put(102, "Marry");
    Dict.put(103, "Kim");
    Dict.put(104, "Sam");
    Dict.put(105, "Jo");
    
    //checking the dictionary for empty
    System.out.println("Is Dict empty?: " + Dict.isEmpty());
  }
}

The output of the above code will be:

Is Dict empty?: true
Is Dict empty?: false

❮ Java.util - Dictionary