Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
mapping
map
remove
communitycreator

What is AbstractMap.remove() in Java?

Hammad Nasir

AbstractMap.remove() in Java is a method of the AbstractMap class. The method is used to remove the mapping of any particular key from the map if a mapping exists.

Syntax

public V remove(Object key)

Parameters

The function takes in one parameter, key, whose mapping needs to be removed from the map.

Return value

The method returns the value that was mapped to key before AbstractMap.remove() is called. If key does not contain any mapping, then remove returns null.

The method throws UnsupportedOperationException if the remove operation is not supported by this map.

Example

import java.util.*;
  
public class Abstract_Map_Class{
    public static void main(String[] args)
    {
        //empty AbstractMap is created
        AbstractMap<Integer, String> temp_absMap1 = new TreeMap<Integer, String>();
    
        // populating temp_absMap1
        temp_absMap1.put(1, "Hello");
        temp_absMap1.put(2, "from");
        temp_absMap1.put(3, "Educative");
        
        System.out.println("Displaying temp_absMap1 before temp_absMap1.remove(1) is called : " + temp_absMap1);
        System.out.println("the result of temp_absMap1.remove(1) is  "  + temp_absMap1.remove(1));
        System.out.println("Displaying temp_absMap1 after temp_absMap1.remove(1) is called : " + temp_absMap1);
    }
}

Explanation

In the code above, we first create an AbstractMap whose key is an Integer and value is a String in line 7.

Then, we add 3 key-value pairs to the AbstractMap.

Finally, we print the AbstractMap before we remove a key-value pair, and then after we remove the key-value pair. The remove function is used in line 15.

RELATED TAGS

java
mapping
map
remove
communitycreator
RELATED COURSES

View all Courses

Keep Exploring