remove(Object): This method is available in java.util.Dictionary class of Java.
Syntax:
Integer java.util.Dictionary.remove(Object key)
This method takes one argument of type Object as its parameter. This method removes the key (and its corresponding value) from this dictionary.
Note: This method does nothing if the key is not in this dictionary.
Parameters: One parameter is required for this method.
key: the key that needs to be removed.
Returns: the value to which the key had been mapped in this dictionary, or null if the key did not have a mapping.
Throws:
NullPointerException - if key is null.
Approach 1: When no exception
Java
import java.util.Dictionary;import java.util.Hashtable;public class Dictionaryremove {public static void main(String[] args) {Dictionary<String, Integer> dictionary =new Hashtable<String, Integer>();dictionary.put("Hello", 10);dictionary.put("Java", 4);dictionary.put("C++", 17);dictionary.remove("Hello");System.out.println(dictionary);}}
Output:
{Java=4, C++=17}
Approach 2: NullPointerException
Java
import java.util.Dictionary;import java.util.Hashtable;public class Dictionaryremove {public static void main(String[] args) {Dictionary<String, Integer> dictionary =new Hashtable<String, Integer>();dictionary.put("Hello", 10);dictionary.put("Java", 4);dictionary.put("C++", 17);dictionary.remove(null);System.out.println(dictionary);}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null at java.base/java.util.Hashtable.remove(Hashtable.java:508)
No comments:
Post a Comment