Monday, October 6, 2025
HomeLanguagesJavaDictionary put() Method in Java with Examples

Dictionary put() Method in Java with Examples

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


Output:

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


Output:

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}
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6780 POSTS0 COMMENTS