Thursday, November 20, 2025
HomeLanguagesJavaWeakHashMap remove() method in Java

WeakHashMap remove() method in Java

The java.util.WeakHashMap.remove() is an inbuilt method of WeakHashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.

Syntax:

Weak_Hash_Map.remove(Object key)

Parameters: The method takes one parameter key whose mapping is to be removed from the Map.

Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL.

Below programs illustrates the working of java.util.WeakHashMap.remove() method:
Program 1: When passing an existing key.




// Java code to illustrate the remove() 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);
  
        // Removing the existing key mapping
        String returned_value = 
                         (String)weak_hash.remove(20);
  
        // 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}

Program 2: When passing a new key.




// Java code to illustrate the remove() 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);
  
        // Removing the new key mapping
        String returned_value = 
                        (String)weak_hash.remove(50);
  
        // 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}

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

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS