Home » Java programming language

Java Dictionary isEmpty() Method with Example

Dictionary Class isEmpty() method: Here, we are going to learn about the isEmpty() method of Dictionary Class with its syntax and example.
Submitted by Preeti Jain, on February 13, 2020

Dictionary Class isEmpty() method

  • isEmpty() method is available in java.util package.
  • isEmpty() method is used to check whether this dictionary has the key- pairs value or not.
  • isEmpty() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • isEmpty() method does not throw an exception at the time of checking the empty status of this dictionary.

Syntax:

    public abstract boolean isEmpty();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is boolean, it returns true when this Dictionary is empty otherwise it returns false.

Example:

// Java program is to demonstrate the example of
// isEmpty() method of Dictionary

import java.util.*;

public class IsEmptyOfDictionary {
    public static void main(String[] args) {
        // Instantiate a Hashtable object
        Dictionary dictionary = new Hashtable();

        // By using put() method is to
        // add an elements in Hashtable 
        dictionary.put(1, "C");
        dictionary.put(2, "C++");
        dictionary.put(3, "JAVA");
        dictionary.put(4, "PHP");
        dictionary.put(5, "SFDC");

        // Display Hashtable elements
        System.out.println("dictionary: " + dictionary);

        // By using isEmpty() method is to check
        // whether this Dictionary is empty or not 
        boolean status = dictionary.isEmpty();

        // Display status
        System.out.println("dictionary.isEmpty(): " + status);
    }
}

Output

dictionary: {5=SFDC, 4=PHP, 3=JAVA, 2=C++, 1=C}
dictionary.isEmpty(): false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.