close
The Wayback Machine - https://web.archive.org/web/20240704064433/https://www.geeksforgeeks.org/hashmap-put-method-in-java/
Open In App

HashMap put() Method in Java

Last Updated : 02 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.HashMap.put() method of HashMap 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: 

Hash_Map.put(key, value)

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

  • 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 java.util.HashMap.put() Method: 
Program 1: When passing an existing key. 

Java




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


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. 

Java




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


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.
 



Previous Article
Next Article

Similar Reads

HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
HashMap is a data structure that uses a hash function to map identifying values, known as keys, to their associated values. It contains "key-value" pairs and allows retrieving value by key. The most impressive feature is it's fast lookup of elements especially for large no. of elements. It is not synchronized by default but we can make it so by cal
6 min read
Java.util.TreeMap.put() and putAll() in Java
The insertion in TreeMap are handled using the variants of the put functions. There are two variants of put() in Java.util.TreeMap, both are discussed in this article. 1. put() : It associates the specified value with the specified key in the map. If a key is already present, then updating it results in updation of that key. Parameters: key : The k
4 min read
LinkedBlockingQueue put() method in Java with Examples
The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockingQueue. Syntax: public void put(E e) throws Interr
3 min read
Hashtable put() Method in Java
The java.util.Hashtable.put() method of Hashtable is used to insert a mapping into a table. This means we can insert a specific key and the value it is mapping to into a particular table. 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: Hash
3 min read
TreeMap put() Method in Java
The java.util.TreeMap.put() method of TreeMap 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: Tree_Map.put
3 min read
WeakHashMap put() Method in Java
The java.util.WeakHashMap.put() method of WeakHashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping, 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: Weak_H
3 min read
ConcurrentHashMap put() method in Java
The put() method in ConcurrentHashMap class in Java is used to associate a given value with a given key in the map. It has the following signature: V put(K key, V value) where: K is the type of key in the map.V is the type of value in the map.Key is the key to be associated with the given value.value is the value to be associated with the given key
5 min read
ArrayBlockingQueue put() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
2 min read
IdentityHashMap put() Method in Java
The java.util.IdentityHashMap.put() method of IdentityHashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping, 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
3 min read
PriorityBlockingQueue put() method in Java
The put(E e) method of PriorityBlockingQueue is used to add an element into this queue. This method inserts the specified element into this priority queue. Since the queue is unbounded, this method will be never be blocked.Syntax: public void put(E e) Parameter: This method accepts a mandatory parameter e which is the element to be inserted in Prio
2 min read
three90RightbarBannerImg