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:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Dictionary<Integer, String>
d = new Hashtable<Integer, String>();
d.put( 1 , "Geeks" );
d.put( 2 , "for" );
d.put( 3 , "Geeks" );
System.out.println( "\nDictionary: " + d);
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:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Dictionary<String, String>
d = new Hashtable<String, String>();
System.out.println( "\nDictionary: " + d);
if (d.isEmpty()) {
System.out.println( "Dictionary "
+ "is empty" );
}
else
System.out.println( "Dictionary "
+ "is not empty" );
d.put( "a" , "GFG" );
d.put( "b" , "gfg" );
System.out.println( "\nDictionary: " + d);
if (d.isEmpty()) {
System.out.println( "Dictionary "
+ "is empty" );
}
else
System.out.println( "Dictionary "
+ "is not empty" );
d.remove( "a" );
d.remove( "b" );
System.out.println( "\nDictionary: " + d);
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Oct, 2018
Like Article
Save Article