字典類的isEmpty()方法檢查此字典是否具有任何鍵-值映射。僅當此詞典中沒有條目時,該函數才返回TRUE。
用法:
public abstract boolean isEmpty()
返回值:如果字典為空,則該函數返回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()
相關用法
- Java ConcurrentLinkedQueue isEmpty()用法及代碼示例
- Java CopyOnWriteArrayList isEmpty()用法及代碼示例
- Java AbstractSequentialList isEmpty()用法及代碼示例
- Java HashSet isEmpty()用法及代碼示例
- Java LinkedHashSet isEmpty()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java AbstractSet isEmpty()用法及代碼示例
- Java BitSet isEmpty()用法及代碼示例
- Java LinkedBlockingDeque isEmpty()用法及代碼示例
- Java Vector isEmpty()用法及代碼示例
- Java Set isEmpty()用法及代碼示例
- Java LinkedTransferQueue isEmpty()用法及代碼示例
- Java HashMap isEmpty()用法及代碼示例
- Java TreeSet isEmpty()用法及代碼示例
- Java Map isEmpty()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 Dictionary isEmpty() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。