AbstractMap put() Method in Java with Examples

The AbstractMap.put() method of AbstractMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to, into a particular map. If an existing key is passed, then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.

Syntax:

AbstractMap.put(key, value)

Parameters: The method takes two parameters, both are of the Object type of the AbstractMap:

  • key: This refers to the key element that needs to be inserted into the Map for mapping.
  • value: This refers to the value that the above key would map into.

Return Value: If an existing key is passed then the previous value gets returned. If a new pair is passed, then NULL is returned.

Below programs are used to illustrate the working of AbstractMap.put() Method:



Program 1: When passing an existing key.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the put() method
  
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abs_map = new HashMap<Integer, String>();
  
        // Mapping string values to int keys
        abs_map.put(10, "Geeks");
        abs_map.put(15, "4");
        abs_map.put(20, "Geeks");
        abs_map.put(25, "Welcomes");
        abs_map.put(30, "You");
  
        // Displaying the AbstractMap
        System.out.println("Initial Mappings are: "
                           + abs_map);
  
        // Inserting existing key along with new value
        String
            returned_value
            = (String)abs_map.put(20, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: "
                           + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: "
                           + abs_map);
    }
}

chevron_right


Output:

Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Returned value is: Geeks
New map is: {20=All, 25=Welcomes, 10=Geeks, 30=You, 15=4}

Program 2: When passing a new key.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the put() method
  
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abs_map = new HashMap<Integer, String>();
  
        // Mapping string values to int keys
        abs_map.put(10, "Geeks");
        abs_map.put(15, "4");
        abs_map.put(20, "Geeks");
        abs_map.put(25, "Welcomes");
        abs_map.put(30, "You");
  
        // Displaying the AbstractMap
        System.out.println("Initial Mappings are: "
                           + abs_map);
  
        // Inserting existing key along with new value
        String
            returned_value
            = (String)abs_map.put(50, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: "
                           + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + abs_map);
    }
}

chevron_right


Output:

Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Returned value is: null
New map is: {50=All, 20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.