Dictionary put() Method in Java with Examples - GeeksforGeeks

27 Dec, 2018
The put() method of Dictionary is used to insert a mapping into the dictionary. This means a specific key along with the value can be mapped into a particular dictionary.
Syntax:
DICTIONARY.put(key, value)
Parameters: The method takes two parameters, both are of the Object type of the Dictionary.
key: This refers to the key element that needs to be inserted into the dictionary for mapping.
value: This refers to the value that the above key would map into.
Return Value: The method returns the value to which the key is mapped. NULL is returned if the key is not mapped into any value.
Below programs are used to illustrate the working of java.util.Dictionary.put() Method:Program 1:
// Java code to illustrate the put() methodimport 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 values into the 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);          // Inserting existing key along with new value        String returned_value = (String)dict.put(20, "All");          // Verifying the returned value        System.out.println("Returned value is: " + returned_value);          // Displaying the new table        System.out.println("New Dictionary is: " + dict);    }}
Initial Dictionary is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: Geeks
New Dictionary is: {10=Geeks, 20=All, 30=You, 15=4, 25=Welcomes}

Program 2:
// Java code to illustrate the put() methodimport 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 values into the 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);          // Inserting existing key along with new value        String returned_value = (String)dict.put(50, "All");          // Verifying the returned value        System.out.println("Returned value is: " + returned_value);          // Displaying the new table        System.out.println("New Dictionary is: " + dict);    }}
Initial Dictionary is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: null
New Dictionary is: {10=Geeks, 20=Geeks, 30=You, 50=All, 15=4, 25=Welcomes}

Java - util package
Java-Collections
Java-Dictionary
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
HashMap containsKey() Method in Java
How to Create Array of Objects in Java?
Convert Double to Integer in Java
Iterating over ArrayLists in Java
Generating random numbers in Java