get(Object): This method is available in java.util.Dictionary class of Java.
Syntax:
Integer java.util.Dictionary.get(Object key)
This method takes one argument of type Object as its parameter. This method returns the value to which the key is mapped in this dictionary.
Parameters: One parameter is required for this method.
key: a key in this dictionary. null if the key is not mapped to any value in this dictionary.
Returns: the value to which the key is mapped in this dictionary.
Throws:
1. NullPointerException - if the key is null.
Approach 1: When no exception
Java
import java.util.Dictionary;import java.util.Hashtable;public class Dictionaryeget {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);System.out.println(dictionary.get("Hello"));}}
Output:
10
Approach 2: NullPointerException
Java
import java.util.Dictionary;import java.util.Hashtable;public class Dictionaryeget {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);System.out.println(dictionary.get(null));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null at java.base/java.util.Hashtable.get(Hashtable.java:381)
No comments:
Post a Comment