![]() |
VOOZH | about |
The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java.
If the map already contains the mapping for the specified key, the old value is replaced by the new specified value.
Output:
Map after adding elements: {One=1, Two=2}
Previous value for key 'Two': 2
Map after updating 'Two': {One=1, Two=22}
V put(K key, V value)This method has two arguments, key and value where the key is the left argument and the value is the corresponding value of the key in the map.
Returns the previous value associated with the key if present, else returns a null value.
Let's see some examples of how to use the map put() method:
How to insert/add elements in a map using the map put() method:
{1=One, 3=Three, 5=Five, 7=Seven, 9=Nine}Below is the code to show the implementation of put().
{1=One, 3=Three, 5=Five, 7=Seven, 9=Nine}Read More Map 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 map put function and its uses in Java.
The map put method in Java is a fundamental function for map manipulation. With this guide, you can easily add new key-value pairs using the map put function.