VOOZH about

URL: https://www.geeksforgeeks.org/java/abstractmap-put-method-in-java-with-examples/

⇱ AbstractMap put() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

AbstractMap put() Method in Java with Examples

Last Updated : 11 Jul, 2025

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:

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).


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).


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.

Comment