字典類的remove()方法接受鍵作為參數,並刪除映射到該鍵的對應值。
用法:
public abstract V remove(Object key)
參數:該函數接受參數鍵,該參數鍵表示將與字典及其映射一起從字典中刪除的鍵。
返回值:該函數返回已映射到鍵的值,如果鍵沒有映射,則返回NULL。
異常:如果作為參數傳遞的鍵為NULL,則該函數不會引發NullPointerException。
以下程序說明了java.util.Dictionary.remove()方法的用法:
示例1:
// Java Program to illustrate
// java.util.Dictionary.remove() 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);
// Display the removed value
System.out.println(d.remove(3)
+ " has been removed");
// Print the Dictionary
System.out.println("\nDictionary: " + d);
}
}
輸出:
Dictionary: {3=Geeks, 2=for, 1=Geeks} Geeks has been removed Dictionary: {2=for, 1=Geeks}
示例2:
// Java Program to illustrate
// java.util.Dictionary.remove() method
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create a new hashtable
Dictionary<String, String> d = new Hashtable<String, String>();
// Insert elements in the hashtable
d.put("a", "GFG");
d.put("b", "gfg");
// Print the Dictionary
System.out.println("\nDictionary: " + d);
// Display the removed value
System.out.println(d.remove("a")
+ " has been removed");
System.out.println(d.remove("b")
+ " has been removed");
// Print the Dictionary
System.out.println("\nDictionary: " + d);
// returns true
if (d.isEmpty()) {
System.out.println("Dictionary "
+ "is empty");
}
else
System.out.println("Dictionary "
+ "is not empty");
}
}
輸出:
Dictionary: {b=gfg, a=GFG} GFG has been removed gfg has been removed Dictionary: {} Dictionary is empty
參考:https://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html#remove()
相關用法
- Java Dictionary isEmpty()用法及代碼示例
- Java Dictionary put()用法及代碼示例
- Java Dictionary get()用法及代碼示例
- Java Dictionary elements()用法及代碼示例
- Java Dictionary size()用法及代碼示例
- Java Dictionary keys()用法及代碼示例
- Java Map remove()用法及代碼示例
- Java LinkedBlockingDeque remove()用法及代碼示例
- Java IdentityHashMap remove()用法及代碼示例
- Java TreeMap remove()用法及代碼示例
- Java LinkedList remove()用法及代碼示例
- Java DelayQueue remove()用法及代碼示例
- Java Stack remove(int)用法及代碼示例
- Java HashSet remove()用法及代碼示例
- Java Vector remove()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 Dictionary remove() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。