HomeLanguagesJavaDictionary get() Method in Java with Examples

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


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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32715 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7091 POSTS0 COMMENTS
Nicole Veronica
12231 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12313 POSTS0 COMMENTS
Shaida Kate Naidoo
7209 POSTS0 COMMENTS
Ted Musemwa
7473 POSTS0 COMMENTS
Thapelo Manthata
7192 POSTS0 COMMENTS
Umr Jansen
7173 POSTS0 COMMENTS