Remove Key-Value Mapping from HashMap Example in Java

In this tutorial we will see how to remove an entry object i.e. specific mapping from HashMap using any particular key of Key-value pair. We will be using remove(Object key) method to remove the mapping. I already shared how HashMap remove() method works internally in Java. In the last tutorial, we have seen how to check if a HashMap is empty or not.

Read Also :  Difference between HashMap and ConcurrentHashMap in Java

Remove Entry Object (Key-Value Mapping) from HashMap Example in Java

According to Oracle docs, it removes the mapping for the specified key from the map if present.

Syntax :


 public Value remove(Object key):


Parameters:

remove(Object key) method takes one parameter i.e. key whose mapping is to be removed from the HashMap.

Returns:

remove(Object key) method returns NULL if the map contains no key-value mappings otherwise returns the previous value associated with the key.

1. When passing an existing key

Program to Remove Mapping from HashMap 
 import java.util.HashMap;

public class HashMapRemoveExample {
  public static void main(String[] args) {
    // Creating a HashMap object with Integer keys and String values
    HashMap<Integer,String> map = new HashMap<>();
    
    // Putting Keys and Values to the HashMap object
    map.put(10, "Spade");
    map.put(20, "Heart");
    map.put(30, "Diamond");
    map.put(40, "Club");
    
    // Printing HashMap object elements
    System.out.println("Original HashMap elements are: "+ map);
    
    // Removing the existing key from the HashMap object
    String returnedValue = map.remove(20);
    
    // Verifying the returnedValue
    System.out.println("Returned value is: "+ returnedValue);
    
    // Printing the updated HashMap object
    System.out.println("Updated HashMap elements are: "+ map);
    
  }
}


Output:
Original HashMap elements are: {20=Heart, 40=Club, 10=Spade, 30=Diamond}
Returned value is: Heart
Updated HashMap elements are: {40=Club, 10=Spade, 30=Diamond}


2. When passing a new key in the remove() method


 import java.util.HashMap;

public class HashMapRemoveExample2 {
  public static void main(String[] args) {
    // Initializing a HashMap object with Integer keys and String values
    HashMap<Integer,String> map2 = new HashMap<>();
    
    // Adding Keys and Values to the HashMap object
    map2.put(50, "Laptop");
    map2.put(60, "Mobile");
    map2.put(70, "Tablet");
    map2.put(80, "Television");
    
    // Displaying HashMap object elements
    System.out.println("Original HashMap elements are: "+ map2);
    
    // Removing the new key from the HashMap object
    String returnedValue = map2.remove(100);
    
    // Checking the returnedValue
    System.out.println("Returned value is: "+ returnedValue);
    
    // Displaying the updated HashMap object
    System.out.println("Updated HashMap elements are: "+ map2);
    
  }
}


Output:
Original HashMap elements are: {80=Television, 50=Laptop, 70=Tablet, 60=Mobile}
Returned value is: null
Updated HashMap elements are: {80=Television, 50=Laptop, 70=Tablet, 60=Mobile}

That's all for today. Please mention in the comments if you have any questions related to how to remove key-value mapping from HashMap with example in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry