Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HashMap remove() Method in Java

  • Difficulty Level : Basic
  • Last Updated : 03 May, 2021

The java.util.HashMap.remove() is an inbuilt method of HashMap 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: 
 

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.HashMap.remove() method: 
Program 1: When passing an existing key. 
 

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.

Java




// Java code to illustrate the remove() 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);
 
    // Removing the existing key mapping
    String returned_value = (String)hash_map.remove(20);
 
    // 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: {25=Welcomes, 10=Geeks, 30=You, 15=4}

 

Program 2: When passing a new key. 
 

Java




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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!