Signup/Sign In

How To Remove Element From a Java Map

In this tutorial, we will learn how to remove an element from a Java Map. In Java, Map is an interface and part of the Java Collection framework. It is used to collect elements into key and value pairs.

Example of Removing Element From Java Map

To modify elements from Java Map we are having two functions remove() and replace(). In the below code snippet, we are using the remove() method on the Map and it will return a deleted value from a given key element. In our case, it will return 68.

enlightenedMap.remove(Key); It removes the element and returns its corresponding value.

import java.util.*;
public class StudyTonight
{
	public static void main(String args[])
	{
		Map<String,Integer> map = new HashMap<>(); 
		
		map.put("A", 65);
		map.put("B", 66);
		map.put("C", 67);
		map.put("D", 68);
		map.put("E", 69);
		// removing
		int result = map.remove("D"); 
		System.out.println("Removed Element is : "+result);    
	}
}


Removed Element is : 68

Example of Removing Element From Java Map

In the given example we called Map.remove() method and this method is slightly different than what we did in the first example.

It will return true if the Key and Value of the element passed in the function exist in the map and this method will remove that element. But in a case corresponding value does not exist and the only key exists then also it will return false.

remove(Key, Value);

enlightenedIt removes the element and returns true if and only if both key and value exist in the map otherwise it will return false.

import java.util.*;
public class StudyTonight
{
    public static void main(String args[])
    {
        Map<String,Integer> map = new HashMap<>();            

        map.put("A", 65);
        map.put("B", 66);
        map.put("C", 67);
        map.put("D", 68);
        map.put("E", 69);         

        System.out.println(map.remove("D", 68));    
    }
}


true

If we are just concern about the time required for operations like adding and removing an element from Map in such cases we can directly perform replace operation by using Map.replace(Key,Value);

Example to Replace An Element From Map

In this example Map.replace(); method which will replace the old value of "D", 68 to 90. You can clearly verify that from the below output. This operation is useful when we want to add a new member to the Map just after removing the operation.

import java.util.*;
public class StudyTonight
{
    public static void main(String args[])
    {
        Map<String,Integer> map = new HashMap<>();            

        map.put("A", 65);
        map.put("B", 66);
        map.put("C", 67);
        map.put("D", 68);
        map.put("E", 69);     
        // replace
        map.replace("D", 90); 

        System.out.println(map);    
    }
}


{A=65, B=66, C=69, D=90}

Example of Map.replace(Key, oldValue, newValue) to Replace An Element From Map

In this example, we are replacing the existing value of the Map element if it is already present in the Map. That's why we mentioned parameter names as oldValue and newValue.

import java.util.*;
public class StudyTonight
{
    public static void main(String args[])
    {
        Map<String,Integer> dictionary = new HashMap<>();            

        dictionary.put("A", 65);
        dictionary.put("B", 66);
        dictionary.put("C", 67);
        dictionary.put("D", 68);
        dictionary.put("E", 69);     

        dictionary.replace("D", 68, 5); 

        System.out.println(dictionary);    
    }
}


{A=65, B=66, C=67, D=5, E=69}

Conclusion:

In this article, we have learned for removing an element from Map we can use Map.remove() or Map.replace() functions and these both functions come with specific variation to check whether the given value exists or not and this leads to more preciseness of the operation. All the variations of function are mentioned below:

  • Map.remove(Key);
  • Map.remove(Key, Value);
  • Map.replace(Key, Value);
  • Map.replace(Key, oldValue, newValue);



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.