AbstractMap remove() Method in Java with Examples
The AbstractMap.remove() is an inbuilt method of AbstractMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.
Syntax:
AbstractMap.remove(Object key)
Parameters: The method takes one parameter key whose mapping is to be removed from the Map.
Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL.
Below programs illustrates the working of AbstractMap.remove() method:
Program 1: When passing an existing key.
// Java code to illustrate the remove() method import java.util.*; public class Abstract_Map_Demo { public static void main(String[] args) { // Creating an empty AbstractMap AbstractMap<Integer, String> abs_map = new HashMap<Integer, String>(); // Mapping string values to int keys abs_map.put(10, "Geeks"); abs_map.put(15, "4"); abs_map.put(20, "Geeks"); abs_map.put(25, "Welcomes"); abs_map.put(30, "You"); // Displaying the AbstractMap System.out.println("Initial Mappings are: " + abs_map); // Removing the existing key mapping String returned_value = (String)abs_map.remove(20); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displayin the new map System.out.println("New map is: " + abs_map); } } |
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Returned value is: Geeks
New map is: {25=Welcomes, 10=Geeks, 30=You, 15=4}
Program 2: When passing a new key.
// Java code to illustrate the remove() method import java.util.*; public class Abstract_Map_Demo { public static void main(String[] args) { // Creating an empty AbstractMap AbstractMap<Integer, String> abs_map = new HashMap<Integer, String>(); // Mapping string values to int keys abs_map.put(10, "Geeks"); abs_map.put(15, "4"); abs_map.put(20, "Geeks"); abs_map.put(25, "Welcomes"); abs_map.put(30, "You"); // Displaying the AbstractMap System.out.println("Initial Mappings are: " + abs_map); // Removing the new key mapping String returned_value = (String)abs_map.remove(50); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displayin the new map System.out.println("New map is: " + abs_map); } } |
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Returned value is: null
New map is: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.
Recommended Posts:
- AbstractMap get() Method in Java with Examples
- AbstractMap put() Method in Java with Examples
- AbstractMap equals() Method in Java with Examples
- AbstractMap hashCode() Method in Java with Examples
- AbstractMap keySet() Method in Java with Examples
- AbstractMap containsValue() Method in Java with Examples
- AbstractMap isEmpty() Method in Java with Examples
- AbstractMap putAll() Method in Java with Examples
- AbstractMap values() Method in Java with Examples
- AbstractMap containsKey() Method in Java with Examples
- AbstractMap clear() Method in Java with Examples
- AbstractMap entrySet() Method in Java with Examples
- AbstractMap size() Method in Java with Examples
- AbstractMap.SimpleEntry setValue(V value) Method in Java with Examples
- AbstractMap.SimpleEntry getKey() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



