Dictionary get() Method in Java with Examples
The get() method of Dictionary class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the dictionary contains no such mapping for the key.
Syntax:
DICTIONARY.get(Object key_element)
Parameters: The method takes one parameter key_element of object type and refers to the key whose associated value is supposed to be fetched.
Return Value: The method returns the value associated with the key_element in the parameter.
Below programs are used to illustrate the working of java.util.Dictionary.get() Method:
Program 1:
// Java code to illustrate the get() method import java.util.*; public class Dictionary_Demo { public static void main(String[] args) { // Creating an empty Dictionary Dictionary<Integer, String> dict = new Hashtable<Integer, String>(); // Inserting the values into dictionary dict.put(10, "Geeks"); dict.put(15, "4"); dict.put(20, "Geeks"); dict.put(25, "Welcomes"); dict.put(30, "You"); // Displaying the Dictionary System.out.println("Initial Dictionary is: " + dict); // Getting the value of 25 System.out.println("The Value is: " + dict.get(25)); // Getting the value of 10 System.out.println("The Value is: " + dict.get(10)); } } |
Initial Dictionary is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
The Value is: Welcomes
The Value is: Geeks
Program 2:
// Java code to illustrate the get() method import java.util.*; public class Dictionary_Demo { public static void main(String[] args) { // Creating an empty Dictionary Dictionary<String, Integer> dict = new Hashtable<String, Integer>(); // Inserting the values into dictionary dict.put("Geeks", 10); dict.put("4", 15); dict.put("Geeks", 20); dict.put("Welcomes", 25); dict.put("You", 30); // Displaying the Dictionary System.out.println("Initial Dictionary is: " + dict); // Getting the value of 25 System.out.println("The Value is: " + dict.get("Geeks")); // Getting the value of 10 System.out.println("The Value is: " + dict.get(20)); } } |
Initial Dictionary is: {You=30, Welcomes=25, 4=15, Geeks=20}
The Value is: 20
The Value is: null
Recommended Posts:
- Dictionary put() Method in Java with Examples
- Dictionary elements() Method in Java with Examples
- Dictionary size() Method in Java with Examples
- Dictionary keys() Method in Java with Examples
- Dictionary isEmpty() Method in Java
- Dictionary remove() Method in Java
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


