Open In App

Java HashMap remove() Method

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The remove() method of the Java HashMap class is used to remove a key-value pair from the map based on the given key. If the key is found, the mapping associated with the key is returned, otherwise, it returns null.

Example 1: This example demonstrates how to use remove(Object key) to remove a key-value pair based on the key.

Java
// Java program to demonstrate the 
// remove(Object key) method
import java.util.HashMap;

public class Geeks {
    public static void main(String[] args) {
      
        // Creating a HashMap
        HashMap<Integer, String> hm1 = new HashMap<>();

        // Adding elements
        hm1.put(1, "Java");
        hm1.put(2, "C++");
        hm1.put(3, "Python");

        System.out.println("Original HashMap: " + hm1);

        // Removing an element
        String r = hm1.remove(2);

        // Displaying removed value and 
        // HashMap after removal
        System.out.println("Removed Value: " + r);
        System.out.println("HashMap after removal: " + hm1);
    }
}

Output
Original HashMap: {1=Java, 2=C++, 3=Python}
Removed Value: C++
HashMap after removal: {1=Java, 3=Python}

Syntax of HashMap remove() Method

There are two Syntax of remove() which are listed below:

// Removes the key-value pair for the specified key
V remove(Object key)

// Removes the key-value pair if the key exists and the value matches exactly
boolean remove(Object key, Object value)

Parameters:

  • Object key: The key whose mapping we want to remove.
  • Object value: The value expected to be associated with the specified key.

Return Types:

  • Returns value that was associated with the specified key.
  • Return null if the key does not exist in the HashMap.
  • Return true if the key-value pair is removed, otherwise return false.

Example 2: This example demonstrates the use of remove(Object key, Object value) method removes a key-value pair only if both matches exaclty.

Java
// Java program to demonstrates the 
// working of remove(Object key, Object value)
import java.util.HashMap;

public class Geeks {
    public static void main(String[] args)
    {
        HashMap<Integer, String> hm = new HashMap<>();

        // Adding elements to the HashMap
        hm.put(1, "Java");
        hm.put(2, "C++");
        hm.put(3, "Python");

        System.out.println("Original HashMap: " + hm);

        // trying to remove key-value pair (2, "C++")
        boolean r1 = hm.remove(2, "C++");
        System.out.println("Was the pair (2, C++) removed? "
                           + r1);

        System.out.println("HashMap after removal: " + hm);

        // trying to remove key-value 
        // pair (3,"JavaScript")
        boolean r2 = hm.remove(3, "JavaScript");
        System.out.println(
            "Was the pair (3, JavaScript) removed? " + r2);

        System.out.println("HashMap after second attempt: "
                           + hm);
    }
}

Output:

Output


Example 3: This example demonstrates the use of remove(Object key) method to remove key-value pairs and handle non-exisiting keys and null values.

Java
// Java program to demonstrates remove(Object key)
import java.util.HashMap;

public class Geeks {
    public static void main(String[] args)
    {
        // Create a HashMap with Integer 
        // keys and String values
        HashMap<Integer, String> hm = new HashMap<>();

        // Add some key-value pairs
        hm.put(1, "Java");
        hm.put(2, "C++");
        hm.put(3, null);

        // Remove key 2 and print 
        // the returned value
        String r = hm.remove(2);
        System.out.println("Removed Value for key 2: " + r);

        // Try removing a non-existent key
        String n = hm.remove(4);
        System.out.println(
            "Removed Value for non-existent key 4: " + n);

        // Remove key 3 (value was explicitly set to null)
        String nullValue = hm.remove(3);
        System.out.println("Removed Value for key 3: "
                           + nullValue);
    }
}

Output
Removed Value for key 2: C++
Removed Value for non-existent key 4: null
Removed Value for key 3: null


Next Article
Practice Tags :

Similar Reads