Java 字典isEmpty()方法

Java 字典isEmpty()方法

Dictionary 类的 isEmpty() 方法检查这个字典是否有任何键值映射。只有当这个字典中没有条目时,该函数才返回 TRUE。

语法

public abstract boolean isEmpty()

返回值: 如果 dictionary 是空的,函数返回 TRUE,否则返回 FALSE。

异常: 该函数没有抛出异常。

下面的程序说明了 Dictionary.isEmpty() 方法的使用。

程序 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");
    }
}

输出。

Dictionary: {3=Geeks, 2=for, 1=Geeks}
Dictionary is not empty

程序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");
    }
}

输出。

Dictionary: {}
Dictionary is empty

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

Dictionary: {}
Dictionary is empty

**参考资料: ** https://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html#isEmpty()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程