Map remove() Method in Java with Examples Last Updated : 11 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. Syntax: V remove(Object key)Parameters: This method has the only argument key, whose mapping is to be removed from the map.Returns: This method returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.Time Complexity: Average time complexity of O(1) and a worst-case time complexity of O(n).Map remove() Method in Java Example 1: The below programs show the implementation of the map.remove() method. Java // Java code to show the implementation of // remove method in Map interface import java.util.*; public class GfG { // Driver code public static void main(String[] args) { // Initializing a Map of type HashMap Map<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(3, "Three"); map.put(5, "Five"); map.put(7, "Seven"); map.put(9, "Nine"); System.out.println(map); map.remove(3); System.out.println(map); // If it doesn't exists, returns // null and does not affects the map map.remove(2); System.out.println(map); } } Output{1=One, 3=Three, 5=Five, 7=Seven, 9=Nine} {1=One, 5=Five, 7=Seven, 9=Nine} {1=One, 5=Five, 7=Seven, 9=Nine} Explanation of the Program:This example demonstrates the use of the remove method in the Map interface using a HashMap. It initializes a HashMap with integer keys and string values, then removes the entry with key 3, and attempts to remove a non-existent key 2. The output shows the map before and after the removals, illustrating how the remove method works and that attempting to remove a non-existent key has no effect on the map.Example 2: Below is the code to show implementation of put(). Java // Java code to show the implementation of // remove method in Map interface import java.util.*; public class GfG { // Driver code public static void main(String[] args) { // Initializing a Map of type HashMap Map<String, String> map = new HashMap<>(); map.put("1", "One"); map.put("3", "Three"); map.put("5", "Five"); map.put("7", "Seven"); map.put("9", "Nine"); System.out.println(map); map.remove("3"); System.out.println(map); } } Output{1=One, 3=Three, 5=Five, 7=Seven, 9=Nine} {1=One, 5=Five, 7=Seven, 9=Nine}Explanation of the Program:This program demonstrates the use of the remove method in the Map interface using a HashMap. It also initializes a HashMap with string keys and values, prints the map, removes the entry with key "3", and then prints the map again to show the result of the removal. The output illustrates how the remove method affects the map by removing the specified key-value pair. Comment More infoAdvertise with us Next Article Map clear() method in Java with Example B barykrg Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-map +1 More Practice Tags : JavaJava-Collections Similar Reads Map Interface in Java In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types.Key Features:No Duplicates in Keys: Keys should be un 11 min read Map put() Method in Java with Examples The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java. If the map already contains the mapping for the specified key, the old value is replaced by the new specified value. Example: Java / 3 min read Map remove() Method in Java with Examples The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. Syntax: V remove(Object key)Parameters: This method has the only argument key, whose mapping is to be removed from the map.Returns: This method returns the value to which this map previously as 2 min read Map clear() method in Java with Example The java.util.Map.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate 2 min read Map containsKey() method in Java with Examples The java.util.Map.containsKey() method is used to check whether a particular key is being mapped into the Map or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: boolean containsKey(key_element) Parameters: The method takes just one paramete 2 min read Map containsValue() method in Java with Examples The java.util.Map.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Map. It takes the value as a parameter and returns True if that value is mapped by any of the key in the map. Syntax: boolean containsValue(Object Value) Param 2 min read Map entrySet() method in Java with Examples The java.util.Map.entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or we can create a new set and store the map elements into them. Syntax: map.entrySet() Parameters: The method does not take any parameter. Re 2 min read Java Map equals() Method The equals() method of Map interface in Java is used to check if two maps are equal. Two maps are considered equal if they meet the following conditions. Both maps must have the same size.Both maps must contain identical key-value pairs. (It means every key in one map must be associated with the sam 2 min read Map get() method in Java with Examples The get() method of Map interface in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. So, let us check how to get value from the map in Java. Syntax of get() method in JavathisMap.get 3 min read Java Map hashCode() Method In Java, the hashCode() method is a part of the Object class and is used to generate a hash code value for an object. Example 1: Hash Code for Different ObjectsThe below Java program demonstrates that every object has a unique hashcode. Java// Java program to demonstrates hashCode() // for different 2 min read Like