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:
import java.util.*;
public class Dictionary_Demo {
public static void main(String[] args)
{
Dictionary<Integer, String> dict
= new Hashtable<Integer, String>();
dict.put( 10 , "Geeks" );
dict.put( 15 , "4" );
dict.put( 20 , "Geeks" );
dict.put( 25 , "Welcomes" );
dict.put( 30 , "You" );
System.out.println( "Initial Dictionary is: " + dict);
System.out.println( "The Value is: " + dict.get( 25 ));
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:
import java.util.*;
public class Dictionary_Demo {
public static void main(String[] args)
{
Dictionary<String, Integer> dict
= new Hashtable<String, Integer>();
dict.put( "Geeks" , 10 );
dict.put( "4" , 15 );
dict.put( "Geeks" , 20 );
dict.put( "Welcomes" , 25 );
dict.put( "You" , 30 );
System.out.println( "Initial Dictionary is: "
+ dict);
System.out.println( "The Value is: "
+ dict.get( "Geeks" ));
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
Share your thoughts in the comments
Please Login to comment...