Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

WeakHashMap put() Method in Java

  • Last Updated : 20 Jul, 2018

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Weak_Hash_Map.put(key, value)

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

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




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

Program 2: When passing a new key.




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

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :