Skip to content
Related Articles

Related Articles

Improve Article

Dictionary isEmpty() Method in Java

  • Last Updated : 11 Oct, 2018
Geek Week

The isEmpty() method of Dictionary Class checks whether this dictionary has any key-value mappings or not. The function returns TRUE only if there is no entry in this dictionary.

Syntax:

public abstract boolean isEmpty()

Return Value: The function returns TRUE if the dictionary is empty and FALSE otherwise.

Exception: The function throws no exception.

Below programs illustrate the use of Dictionary.isEmpty() method:



Program 1:




// Java Program to illustrate
// Dictionary.isEmpty() method
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a new hashtable
        Dictionary<Integer, String>
            d = new Hashtable<Integer, String>();
  
        // Insert elements in the hashtable
        d.put(1, "Geeks");
        d.put(2, "for");
        d.put(3, "Geeks");
  
        // Print the Dictionary
        System.out.println("\nDictionary: " + d);
  
        // check if this dictionary is empty
        // using isEmpty() method
        if (d.isEmpty()) {
            System.out.println("Dictionary "
                               + "is empty");
        }
        else
            System.out.println("Dictionary "
                               + "is not empty");
    }
}
Output:
Dictionary: {3=Geeks, 2=for, 1=Geeks}
Dictionary is not empty

Program 2:




// Java Program to illustrate
// Dictionary.isEmpty() method
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a new hashtable
        Dictionary<String, String>
            d = new Hashtable<String, String>();
  
        // Print the Dictionary
        System.out.println("\nDictionary: " + d);
  
        // check if this dictionary is empty
        // using isEmpty() method
        if (d.isEmpty()) {
            System.out.println("Dictionary "
                               + "is empty");
        }
        else
            System.out.println("Dictionary "
                               + "is not empty");
  
        // Insert elements in the hashtable
        d.put("a", "GFG");
        d.put("b", "gfg");
  
        // Print the Dictionary
        System.out.println("\nDictionary: " + d);
  
        // check if this dictionary is empty
        // using isEmpty() method
        if (d.isEmpty()) {
            System.out.println("Dictionary "
                               + "is empty");
        }
        else
            System.out.println("Dictionary "
                               + "is not empty");
  
        // Remove elements in the hashtable
        d.remove("a");
        d.remove("b");
  
        // Print the Dictionary
        System.out.println("\nDictionary: " + d);
  
        // check if this dictionary is empty
        // using isEmpty() method
        if (d.isEmpty()) {
            System.out.println("Dictionary "
                               + "is empty");
        }
        else
            System.out.println("Dictionary "
                               + "is not empty");
    }
}
Output:
Dictionary: {}
Dictionary is empty

Dictionary: {b=gfg, a=GFG}
Dictionary is not empty

Dictionary: {}
Dictionary is empty

Reference:https://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html#isEmpty()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :