AbstractMap put() Method in Java with Examples
Last Updated :
16 Feb, 2024
The AbstractMap put() method inserts a mapping into an AbstractMap. This means we can add a specific key and its value, 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.
Example:
Java
import java.util.*;
public class GFG {
public static void main(String[] args) {
AbstractMap<Integer, String> absMap = new HashMap<>();
absMap.put( 1 , "Java" );
absMap.put( 2 , "JavaScript" );
absMap.put( 3 , "Python" );
System.out.println( "Initial Mappings are: " + absMap);
String returnedValue = absMap.put( 3 , "Geeks" );
System.out.println( "Returned value is: " + returnedValue);
System.out.println( "New map is: " + absMap);
}
}
|
Output:
Initial Mappings are: {1=Java, 2=JavaScript, 3=Python}
Returned value is: Python
New map is: {1=Java, 2=JavaScript, 3=Geeks}
Syntax
AbstractMap.put(key, value)
Parameters
- 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.
Returns
- If an existing key is passed then the previous value gets returned.
- If a new pair is passed, then NULL is returned.
Exceptions
- NullPointerException– When the specified key is null.
AbstractMap put() Method Examples
Let’s see some examples of how to use the Java AbstractMap put() method in Java.
Example 1:
How to add a new element in AbstractMap with Java put() function (passing an existing key).
Java
import java.util.*;
public class AbstractMapDemo {
public static void main(String[] args) {
AbstractMap<Integer, String> absMap = new HashMap<>();
absMap.put( 10 , "Geeks" );
absMap.put( 15 , "4" );
absMap.put( 20 , "Geeks" );
absMap.put( 25 , "Welcomes" );
absMap.put( 30 , "You" );
System.out.println( "Initial Mappings are: " + absMap);
String returnedValue = absMap.put( 20 , "All" );
System.out.println( "Returned value is: " + returnedValue);
System.out.println( "New map is: " + absMap);
}
}
|
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}
Example 2:
Adding a new element in AbstractMap (passing a new key).
Java
import java.util.*;
public class AbstractMapDemo {
public static void main(String[] args) {
AbstractMap<Integer, String> absMap = new HashMap<>();
absMap.put( 10 , "Geeks" );
absMap.put( 15 , "4" );
absMap.put( 20 , "Geeks" );
absMap.put( 25 , "Welcomes" );
absMap.put( 30 , "You" );
System.out.println( "Initial Mappings are: " + absMap);
String returnedValue = absMap.put( 50 , "All" );
System.out.println( "Returned value is: " + returnedValue);
System.out.println( "New map is: " + absMap);
}
}
|
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.
Read More AbstractMap Methods
Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the AbstractMap put function and its uses in Java.
The AbstractMap put() method in Java is a fundamental function for AbstractMap manipulation. With this guide, you can easily add/put new elements to AbstractMap using the AbstractMap put function.