Open In App

Dictionary get() Method in Java with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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));
    }
}


Output:

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));
    }
}


Output:

Initial Dictionary is: {You=30, Welcomes=25, 4=15, Geeks=20}
The Value is: 20
The Value is: null


Last Updated : 27 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads